Skip to content

Instantly share code, notes, and snippets.

@daniel42
Created February 3, 2010 06:31
Show Gist options
  • Save daniel42/293392 to your computer and use it in GitHub Desktop.
Save daniel42/293392 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;
import org.python.core.PyException;
import org.python.core.PySystemState;
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 compile {
public class BuildException extends RuntimeException {
public BuildException(String message) {
}
}
public void log(String message) {
System.out.println(message);
}
public static void main(String[] args) {
String filename = args[0];
Set<File> fileSet = new HashSet<File>();
File file = new File(filename);
fileSet.add(file);
compile c = new compile();
c.process(fileSet);
}
public compile() {
}
public void process(Set<File> toCompile) throws BuildException {
String destDir = "./tmp";
if (toCompile.size() == 0) {
return;
} else if (toCompile.size() > 1) {
log("Compiling " + toCompile.size() + " files");
} else if (toCompile.size() == 1) {
log("Compiling 1 file");
}
Properties props = new Properties();
props.setProperty(PySystemState.PYTHON_CACHEDIR_SKIP, "true");
PySystemState.initialize(System.getProperties(), props);
for (File src : toCompile) {
String name = _py_compile.getModuleName(src);
String compiledFilePath = name.replace('.', '/');
if (src.getName().endsWith("__init__.py")) {
compiledFilePath += "/__init__";
}
File compiled = new File(destDir, compiledFilePath + "$py.class");
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() {
return "*.py";
}
protected String getTo() {
return "*$py.class";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment