01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.modules;
03:
04: import java.io.File;
05:
06: import org.python.core.PyList;
07: import org.python.core.PyString;
08:
09: public class py_compile {
10: public static PyList __all__ = new PyList(
11: new PyString[] { new PyString("compile") });
12:
13: public static boolean compile(String filename, String cfile) {
14: return compile(filename, cfile, null);
15: }
16:
17: public static boolean compile(String filename) {
18: return compile(filename, null, null);
19: }
20:
21: public static boolean compile(String filename, String cfile,
22: String dfile) {
23: File file = new File(filename);
24: String name = file.getName();
25: int dot = name.lastIndexOf('.');
26: if (dot != -1) {
27: name = name.substring(0, dot);
28: }
29: // Make the compiled classfile's name the fully qualified with a package by
30: // walking up the directory tree looking for __init__.py files. Don't
31: // check for __init__$py.class since we're compiling source here and the
32: // existence of a class file without corresponding source probably doesn't
33: // indicate a package.
34: File dir = file.getParentFile();
35: while (dir != null && (new File(dir, "__init__.py").exists())) {
36: name = dir.getName() + "." + name;
37: dir = dir.getParentFile();
38: }
39: byte[] bytes = org.python.core.imp.compileSource(name, file,
40: dfile, cfile);
41: org.python.core.imp.cacheCompiledSource(filename, cfile, bytes);
42:
43: return bytes.length > 0;
44: }
45:
46: }
|