Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 46 additions & 70 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -255,40 +255,29 @@ configurations {
}

dependencies {
// The expose (Ant) task depends on classes compiled to here:
// The expose task depends on classes compiled to here:
expose files(unexposedDir)
}

// A (Gradle) task to run the Ant task 'expose'.
task expose (group: 'Custom', dependsOn: compileJava) {
// A (Gradle) task to run 'expose'.
task expose (type: JavaExec, group: 'Custom') {

description = 'Expose Java types to Python using their annotations.'

// Allow Gradle to infer the need to regenreate the outputs
dependsOn compileJava

// Allow Gradle to infer the need to regenerate the outputs
inputs.files(fileTree("${project.ext.unexposedDir}/org/python"))
inputs.file('CoreExposed.includes')
outputs.dir(project.ext.exposedDir)

def exposedDir = project.ext.exposedDir
def unexposedDir = project.ext.unexposedDir

doLast {
/*
* Define an Ant task called 'expose' in the project's AntBuilder.
* We can't define it until ExposeTask has been compiled.
*/
ant.taskdef(
name: 'expose',
classname: 'org.python.expose.generate.ExposeTask',
classpath: configurations.expose.asPath
)

// Use the Gradle-conventional directory structure (not the legacy one).
ant.expose(
srcdir: file(unexposedDir),
destdir: mkdir(file(exposedDir)),
includesfile: file('CoreExposed.includes')
)
}
classpath = configurations.expose
mainClass = 'org.python.expose.generate.ExposeTask'
args = [
'--srcdir', project.ext.unexposedDir,
'--destdir', project.ext.exposedDir,
'--includesfile', 'CoreExposed.includes'
]
}

// Task to merge the exposed and unexposed classes
Expand Down Expand Up @@ -548,13 +537,15 @@ configurations {
}

dependencies {
// Jython as built so far should be on the path of the jycompile (Ant) task
// Jython as built so far should be on the path of the jycompile task
pycompile files("$buildDir/classes/java/main")
pycompile files("$buildDir/resources/main")
}

// A (Gradle) task to run the Ant task 'jycompile' (not pycompile).
task pycompile(group: 'Custom', description: 'Compile the Python modules to .class files for the JAR') {
// A (Gradle) task to run 'jycompile' (not pycompile).
task pycompile(type: JavaExec, group: 'Custom') {

description = 'Compile the Python modules to .class files for the JAR'

// Compiler depends on rest of Jython being fully assembled in 'classes'
dependsOn classes
Expand All @@ -564,48 +555,33 @@ task pycompile(group: 'Custom', description: 'Compile the Python modules to .cla
inputs.dir project.ext.buildLibDir
outputs.dir project.ext.compiledLibDir

def inputsDir = project.ext.buildLibDir
def outputsDir = project.ext.compiledLibDir

doFirst {
/*
* Define an Ant task called 'jycompile' in the project's AntBuilder.
* We can't define it until JythoncAntTask has been compiled, so this
* must happen during the execution of the task (early).
*/
ant.taskdef(
name: 'jycompile',
classname: 'org.python.util.JycompileAntTask',
classpath: configurations.pycompile.asPath
)
}

doLast {
/*
* Now use the 'jycompile' Ant task to compile the Python source we
* supply to users. The exclusions have been copied from build.xml,
* and also this comment:
<!-- One might think that the exclusion of lib2to3/tests/** is
recursive, but one would be wrong ;) It's actually only
two levels, so for now the workaround is also to include
data/myfixes/**

This exclusion for lib2to3/tests/ in general is necessary
because data/infinite_recursion.py is not compileable by
Jython - it's too large and will exceed Java method
limits for the top level script; nor is
data/py3_test_grammar.py - it's Python 3. Meanwhile
refactor.get_all_fix_names depends on having *.py, not
exclusively $py.class, files available in
lib2to3/tests/data/myfixes/**. -->
*/
def exclusions = ['test/**', 'lib2to3/tests/**', 'lib2to3/tests/data/myfixes/**']
ant.jycompile(
srcdir: inputsDir,
destdir: outputsDir,
excludes: exclusions.join(',') // Yes, it's that way round :o
)
}
classpath = configurations.pycompile
mainClass = 'org.python.util.JycompileAntTask'

/*
* Now use JycompileAntTask to compile the Python source we
* supply to users. The exclusions have been copied from build.xml,
* and also this comment:
<!-- One might think that the exclusion of lib2to3/tests/** is
recursive, but one would be wrong ;) It's actually only
two levels, so for now the workaround is also to include
data/myfixes/**

This exclusion for lib2to3/tests/ in general is necessary
because data/infinite_recursion.py is not compileable by
Jython - it's too large and will exceed Java method
limits for the top level script; nor is
data/py3_test_grammar.py - it's Python 3. Meanwhile
refactor.get_all_fix_names depends on having *.py, not
exclusively $py.class, files available in
lib2to3/tests/data/myfixes/**. -->
*/
def exclusions = ['test/**', 'lib2to3/tests/**', 'lib2to3/tests/data/myfixes/**']
args = [
'--srcdir', project.ext.buildLibDir,
'--destdir', project.ext.compiledLibDir,
'--excludes', exclusions.join(',')
]
}


Expand Down
4 changes: 4 additions & 0 deletions src/org/python/expose/generate/ExposeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ private void write(String destClass, byte[] newClassfile) {
}
}
}

public static void main(String[] args) {
new ExposeTask().runFromCommandLine(args);
}
}
84 changes: 84 additions & 0 deletions src/org/python/util/GlobMatchingTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Set;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.GlobPatternMapper;
Expand Down Expand Up @@ -98,4 +100,86 @@ protected void checkParameters() throws BuildException {
+ "or is not a directory", getLocation());
}
}

public void runFromCommandLine(String[] args) {
Project project = new Project();
project.init();
project.setBaseDir(new File("."));
DefaultLogger logger = new DefaultLogger();
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
logger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(logger);
setProject(project);
if (getTaskName() == null || getTaskName().isEmpty()) {
setTaskName(getClass().getSimpleName());
}

for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--srcdir".equals(arg) || "-srcdir".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setSrcdir(new Path(project, args[i]));
} else if ("--destdir".equals(arg) || "-destdir".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setDestdir(new File(args[i]));
} else if ("--includesfile".equals(arg) || "-includesfile".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setIncludesfile(new File(args[i]));
} else if ("--includes".equals(arg) || "-includes".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setIncludes(args[i]);
} else if ("--excludesfile".equals(arg) || "-excludesfile".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setExcludesfile(new File(args[i]));
} else if ("--excludes".equals(arg) || "-excludes".equals(arg)) {
if (++i >= args.length) {
throw new IllegalArgumentException("Missing value for " + arg);
}
setExcludes(args[i]);
} else if (arg.startsWith("--srcdir=")) {
setSrcdir(new Path(project, arg.substring("--srcdir=".length())));
} else if (arg.startsWith("-srcdir=")) {
setSrcdir(new Path(project, arg.substring("-srcdir=".length())));
} else if (arg.startsWith("--destdir=")) {
setDestdir(new File(arg.substring("--destdir=".length())));
} else if (arg.startsWith("-destdir=")) {
setDestdir(new File(arg.substring("-destdir=".length())));
} else if (arg.startsWith("--includesfile=")) {
setIncludesfile(new File(arg.substring("--includesfile=".length())));
} else if (arg.startsWith("-includesfile=")) {
setIncludesfile(new File(arg.substring("-includesfile=".length())));
} else if (arg.startsWith("--includes=")) {
setIncludes(arg.substring("--includes=".length()));
} else if (arg.startsWith("-includes=")) {
setIncludes(arg.substring("-includes=".length()));
} else if (arg.startsWith("--excludesfile=")) {
setExcludesfile(new File(arg.substring("--excludesfile=".length())));
} else if (arg.startsWith("-excludesfile=")) {
setExcludesfile(new File(arg.substring("-excludesfile=".length())));
} else if (arg.startsWith("--excludes=")) {
setExcludes(arg.substring("--excludes=".length()));
} else if (arg.startsWith("-excludes=")) {
setExcludes(arg.substring("-excludes=".length()));
} else {
throw new IllegalArgumentException("Unknown argument: " + arg);
}
}

if (destDir != null && !destDir.exists()) {
destDir.mkdirs();
}

execute();
}
}
4 changes: 4 additions & 0 deletions src/org/python/util/JycompileAntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ protected String getFrom() {
protected String getTo() {
return imp.makeCompiledFilename(getFrom());
}

public static void main(String[] args) {
new JycompileAntTask().runFromCommandLine(args);
}
}
129 changes: 129 additions & 0 deletions tests/java/org/python/util/GlobMatchingTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.python.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

import org.apache.tools.ant.BuildException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.python.expose.generate.ExposeTask;

class GlobMatchingTaskTest {

static class DummyGlobMatchingTask extends GlobMatchingTask {
boolean processed = false;
Set<File> receivedMatches;

@Override
protected void process(Set<File> matches) {
this.processed = true;
this.receivedMatches = matches;
}

@Override
protected String getFrom() {
return "*.txt";
}

@Override
protected String getTo() {
return "*.txt";
}
}

@Test
void testCommandLineOptions(@TempDir Path tempDir) throws IOException {
Path src = tempDir.resolve("src");
Path dest = tempDir.resolve("dest");
Files.createDirectories(src);

File f1 = Files.createFile(src.resolve("a.txt")).toFile();
File f2 = Files.createFile(src.resolve("b.txt")).toFile();

DummyGlobMatchingTask task = new DummyGlobMatchingTask();
task.runFromCommandLine(new String[] {
"--srcdir", src.toString(),
"--destdir", dest.toString()
});

assertTrue(task.processed);
assertNotNull(task.receivedMatches);
assertEquals(2, task.receivedMatches.size());
assertTrue(task.receivedMatches.contains(f1));
assertTrue(task.receivedMatches.contains(f2));
assertTrue(Files.exists(dest));
}

@Test
void testCommandLineEqualsOptions(@TempDir Path tempDir) throws IOException {
Path src = tempDir.resolve("src");
Path dest = tempDir.resolve("dest");
Files.createDirectories(src);

File f1 = Files.createFile(src.resolve("a.txt")).toFile();
File f2 = Files.createFile(src.resolve("b.txt")).toFile();

DummyGlobMatchingTask task = new DummyGlobMatchingTask();
task.runFromCommandLine(new String[] {
"--srcdir=" + src.toString(),
"--destdir=" + dest.toString(),
"--excludes=b.txt"
});

assertTrue(task.processed);
assertNotNull(task.receivedMatches);
assertEquals(1, task.receivedMatches.size());
assertTrue(task.receivedMatches.contains(f1));
assertFalse(task.receivedMatches.contains(f2));
}

@Test
void testUnknownArgument() {
DummyGlobMatchingTask task = new DummyGlobMatchingTask();
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
task.runFromCommandLine(new String[] {"--unknown"});
});
assertTrue(ex.getMessage().contains("Unknown argument"));
}

@Test
void testMissingArgumentValue() {
DummyGlobMatchingTask task = new DummyGlobMatchingTask();
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
task.runFromCommandLine(new String[] {"--srcdir"});
});
assertTrue(ex.getMessage().contains("Missing value"));
}

@Test
void testMissingRequiredParameters() {
DummyGlobMatchingTask task = new DummyGlobMatchingTask();
BuildException ex = assertThrows(BuildException.class, () -> {
task.runFromCommandLine(new String[] {});
});
assertTrue(ex.getMessage().contains("srcdir attribute must be set"));
}

@Test
void testExposeTaskMainMissingArgs() {
assertThrows(BuildException.class, () -> {
ExposeTask.main(new String[] {});
});
}

@Test
void testJycompileAntTaskMainMissingArgs() {
assertThrows(BuildException.class, () -> {
JycompileAntTask.main(new String[] {});
});
}
}
Loading