From: <cg...@us...> - 2009-07-19 08:15:59
|
Revision: 6548 http://jython.svn.sourceforge.net/jython/?rev=6548&view=rev Author: cgroves Date: 2009-07-19 08:15:53 +0000 (Sun, 19 Jul 2009) Log Message: ----------- Add an ant task to compile proxies This works by naively importing the modules with the proxy output directory set to the destination configured in the ant task. Eventually it's going to need to work with our Lib directory to generate proxies for all of our Java subclasses, but it currently fails miserably on various modules that aren't expecting to be straight-up imported. Modified Paths: -------------- trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/expose/generate/ExposeTask.java trunk/jython/src/org/python/util/GlobMatchingTask.java trunk/jython/src/org/python/util/JycompileAntTask.java Added Paths: ----------- trunk/jython/src/org/python/util/CompileProxiesTask.java trunk/jython/src/org/python/util/FileNameMatchingTask.java Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-07-19 06:49:49 UTC (rev 6547) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -186,7 +186,7 @@ underlying_class = forClass; computeLinearMro(baseClass); } else { - needsInners.add(this); + needsInners.add(this); javaProxy = forClass; objtype = PyType.fromClassSkippingInners(Class.class, needsInners); // Wrapped Java types fill in their mro first using all of their interfaces then their Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-07-19 06:49:49 UTC (rev 6547) +++ trunk/jython/src/org/python/core/PyType.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -1051,8 +1051,9 @@ PyObject dict = element.fastGetDict(); if (dict != null) { PyObject obj = dict.__finditem__(name); - if (obj != null) + if (obj != null) { return obj; + } } } return null; Modified: trunk/jython/src/org/python/expose/generate/ExposeTask.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-07-19 06:49:49 UTC (rev 6547) +++ trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -8,8 +8,8 @@ import org.apache.tools.ant.BuildException; import org.objectweb.asm.ClassWriter; +import org.python.core.Options; import org.python.core.Py; -import org.python.core.Options; import org.python.util.GlobMatchingTask; public class ExposeTask extends GlobMatchingTask { Added: trunk/jython/src/org/python/util/CompileProxiesTask.java =================================================================== --- trunk/jython/src/org/python/util/CompileProxiesTask.java (rev 0) +++ trunk/jython/src/org/python/util/CompileProxiesTask.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -0,0 +1,68 @@ +package org.python.util; + +import java.io.File; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.apache.tools.ant.BuildException; +import org.python.core.Py; +import org.python.core.PySystemState; + +/** + * Compiles the Java proxies for Python classes in the Python modules in a given directory tree to + * another directory. + */ +public class CompileProxiesTask extends JycompileAntTask { + + @Override + public void process(Set<File> toCompile) throws BuildException { + // Run our superclass' compile first to check that everything has valid syntax before + // attempting to import it and to keep the imports from generating class files in the source + // directory + super.process(toCompile); + Properties props = new Properties(); + props.setProperty(PySystemState.PYTHON_CACHEDIR_SKIP, "true"); + PySystemState.initialize(props, null); + PySystemState sys = Py.getSystemState(); + // Part 2 of not spewing compilation in the source directory: import our compiled files + sys.path.insert(0, Py.newString(destDir.getAbsolutePath())); + sys.javaproxy_dir = destDir.getAbsolutePath(); + PythonInterpreter interp = new PythonInterpreter(); + for (String module : compiledModuleNames) { + try { + interp.exec("import " + module); + } catch (RuntimeException t) { + // We didn't get to import any of these files, so their compiled form can't hang + // around or we won't pick them up as needing compilation next time. + for (File f : compiledModuleFiles) { + f.delete(); + } + throw t; + } + // This module was successfully imported, so its compiled file can hang around + compiledModuleFiles.remove(0); + } + } + + @Override + protected void compile(File src, File compiled, String moduleName) { + try { + super.compile(src, compiled, moduleName); + } catch (BuildException ex) { + // This depends on the modtime of the source being newer than that of the compiled file + // to decide to do the import in process, so even though these files compiled properly, + // they need to be deleted to allow them to be imported in process next time around. + for (File f : compiledModuleFiles) { + f.delete(); + } + throw ex; + } + compiledModuleNames.add(moduleName); + compiledModuleFiles.add(compiled); + } + + private List<String> compiledModuleNames = Generic.list(); + + private List<File> compiledModuleFiles = Generic.list(); +} Added: trunk/jython/src/org/python/util/FileNameMatchingTask.java =================================================================== --- trunk/jython/src/org/python/util/FileNameMatchingTask.java (rev 0) +++ trunk/jython/src/org/python/util/FileNameMatchingTask.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -0,0 +1,95 @@ +package org.python.util; + +import java.io.File; +import java.util.Set; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.MatchingTask; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.FileNameMapper; +import org.apache.tools.ant.util.SourceFileScanner; + +public abstract class FileNameMatchingTask extends MatchingTask { + + private Path src; + + protected File destDir; + + private Set<File> toProcess = Generic.set(); + + /** + * Set the source directories to find the class files to be exposed. + */ + public void setSrcdir(Path srcDir) { + if (src == null) { + src = srcDir; + } else { + src.append(srcDir); + } + } + + /** + * Gets the source dirs to find the class files to be exposed. + */ + public Path getSrcdir() { + return src; + } + + /** + * Set the destination directory into which the Java source files should be compiled. + * + * @param destDir + * the destination director + */ + public void setDestdir(File destDir) { + this.destDir = destDir; + } + + /** + * Gets the destination directory into which the java source files should be compiled. + * + * @return the destination directory + */ + public File getDestdir() { + return destDir; + } + + @Override + public void execute() throws BuildException { + checkParameters(); + toProcess.clear(); + for (String srcEntry : src.list()) { + File srcDir = getProject().resolveFile(srcEntry); + if (!srcDir.exists()) { + throw new BuildException("srcdir '" + srcDir.getPath() + "' does not exist!", + getLocation()); + } + String[] files = getDirectoryScanner(srcDir).getIncludedFiles(); + scanDir(srcDir, destDir != null ? destDir : srcDir, files); + } + process(toProcess); + } + + protected abstract void process(Set<File> matches); + + protected abstract FileNameMapper createMapper(); + + protected void scanDir(File srcDir, File destDir, String[] files) { + SourceFileScanner sfs = new SourceFileScanner(this); + for (File file : sfs.restrictAsFiles(files, srcDir, destDir, createMapper())) { + toProcess.add(file); + } + } + /** + * Check that all required attributes have been set and nothing silly has been entered. + */ + protected void checkParameters() throws BuildException { + if (src == null || src.size() == 0) { + throw new BuildException("srcdir attribute must be set!", getLocation()); + } + if (destDir != null && !destDir.isDirectory()) { + throw new BuildException("destination directory '" + destDir + "' does not exist " + + "or is not a directory", getLocation()); + } + } +} \ No newline at end of file Modified: trunk/jython/src/org/python/util/GlobMatchingTask.java =================================================================== --- trunk/jython/src/org/python/util/GlobMatchingTask.java 2009-07-19 06:49:49 UTC (rev 6547) +++ trunk/jython/src/org/python/util/GlobMatchingTask.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -1,101 +1,19 @@ package org.python.util; -import java.io.File; -import java.util.Set; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.taskdefs.MatchingTask; -import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.FileNameMapper; import org.apache.tools.ant.util.GlobPatternMapper; -import org.apache.tools.ant.util.SourceFileScanner; -public abstract class GlobMatchingTask extends MatchingTask { +public abstract class GlobMatchingTask extends FileNameMatchingTask { - private Path src; - - protected File destDir; - - private Set<File> toExpose = Generic.set(); - - /** - * Set the source directories to find the class files to be exposed. - */ - public void setSrcdir(Path srcDir) { - if (src == null) { - src = srcDir; - } else { - src.append(srcDir); - } - } - - /** - * Gets the source dirs to find the class files to be exposed. - */ - public Path getSrcdir() { - return src; - } - - /** - * Set the destination directory into which the Java source files should be compiled. - * - * @param destDir - * the destination director - */ - public void setDestdir(File destDir) { - this.destDir = destDir; - } - - /** - * Gets the destination directory into which the java source files should be compiled. - * - * @return the destination directory - */ - public File getDestdir() { - return destDir; - } - @Override - public void execute() throws BuildException { - checkParameters(); - toExpose.clear(); - for (String srcEntry : src.list()) { - File srcDir = getProject().resolveFile(srcEntry); - if (!srcDir.exists()) { - throw new BuildException("srcdir '" + srcDir.getPath() + "' does not exist!", - getLocation()); - } - String[] files = getDirectoryScanner(srcDir).getIncludedFiles(); - scanDir(srcDir, destDir != null ? destDir : srcDir, files); - } - process(toExpose); + protected FileNameMapper createMapper() { + FileNameMapper mapper = new GlobPatternMapper(); + mapper.setFrom(getFrom()); + mapper.setTo(getTo()); + return mapper; } - protected abstract void process(Set<File> matches); - protected abstract String getFrom(); protected abstract String getTo(); - - protected void scanDir(File srcDir, File destDir, String[] files) { - GlobPatternMapper m = new GlobPatternMapper(); - m.setFrom(getFrom()); - m.setTo(getTo()); - SourceFileScanner sfs = new SourceFileScanner(this); - for (File file : sfs.restrictAsFiles(files, srcDir, destDir, m)) { - toExpose.add(file); - } - } - - /** - * Check that all required attributes have been set and nothing silly has been entered. - */ - protected void checkParameters() throws BuildException { - if (src == null || src.size() == 0) { - throw new BuildException("srcdir attribute must be set!", getLocation()); - } - if (destDir != null && !destDir.isDirectory()) { - throw new BuildException("destination directory '" + destDir + "' does not exist " - + "or is not a directory", getLocation()); - } - } -} \ No newline at end of file +} Modified: trunk/jython/src/org/python/util/JycompileAntTask.java =================================================================== --- trunk/jython/src/org/python/util/JycompileAntTask.java 2009-07-19 06:49:49 UTC (rev 6547) +++ trunk/jython/src/org/python/util/JycompileAntTask.java 2009-07-19 08:15:53 UTC (rev 6548) @@ -59,10 +59,12 @@ imp.cacheCompiledSource(src.getAbsolutePath(), compiled.getAbsolutePath(), bytes); } + @Override protected String getFrom() { return "*.py"; } + @Override protected String getTo() { return "*$py.class"; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |