Skip to content

Commit

Permalink
Break out the compile method for use in subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
groves committed Jan 21, 2009
1 parent 9b57cd7 commit 1d090a5
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/org/python/util/JycompileAntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import org.python.core.imp;
import org.python.modules._py_compile;

/**
* Compiles all python files in a directory to bytecode, and writes them to another directory,
* possibly the same one.
*/
public class JycompileAntTask extends GlobMatchingTask {

@Override
Expand All @@ -31,19 +35,28 @@ public void process(Set<File> toCompile) throws BuildException {
compiledFilePath += "/__init__";
}
File compiled = new File(destDir, compiledFilePath + "$py.class");
byte[] bytes;
try {
bytes = imp.compileSource(name, src);
} catch (PyException pye) {
pye.printStackTrace();
throw new BuildException("Compile failed; see the compiler error output for details.");
}
File dir = compiled.getParentFile();
if (!dir.exists() && !compiled.getParentFile().mkdirs()) {
throw new BuildException("Unable to make directory for compiled file: " + compiled);
}
imp.cacheCompiledSource(src.getAbsolutePath(), compiled.getAbsolutePath(), bytes);
compile(src, compiled, name);
}
}

/**
* Compiles the python file <code>src</code> to bytecode filling in <code>moduleName</code> as
* its name, and stores it in <code>compiled</code>. This is called by process for every file
* that's compiled, so subclasses can override this method to affect or track the compilation.
*/
protected void compile(File src, File compiled, String moduleName) {
byte[] bytes;
try {
bytes = imp.compileSource(moduleName, src);
} catch (PyException pye) {
pye.printStackTrace();
throw new BuildException("Compile failed; see the compiler error output for details.");
}
File dir = compiled.getParentFile();
if (!dir.exists() && !compiled.getParentFile().mkdirs()) {
throw new BuildException("Unable to make directory for compiled file: " + compiled);
}
imp.cacheCompiledSource(src.getAbsolutePath(), compiled.getAbsolutePath(), bytes);
}

protected String getFrom() {
Expand Down

0 comments on commit 1d090a5

Please sign in to comment.