From: <cg...@us...> - 2009-01-18 10:04:32
|
Revision: 5945 http://jython.svn.sourceforge.net/jython/?rev=5945&view=rev Author: cgroves Date: 2009-01-18 10:04:29 +0000 (Sun, 18 Jan 2009) Log Message: ----------- Add an Ant task to compile .py files to $py.class. It runs on Lib as part of developer-build to exercise it a little bit. Also, only rename the xerces parts of org.apache. Otherwise our ant imports get renamed when jarjaring. Modified Paths: -------------- trunk/jython/build.xml trunk/jython/src/org/python/compiler/Future.java trunk/jython/src/org/python/compiler/Module.java trunk/jython/src/org/python/compiler/ScopesCompiler.java trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/expose/generate/ExposeTask.java trunk/jython/src/org/python/modules/_py_compile.java Added Paths: ----------- trunk/jython/src/org/python/util/GlobMatchingTask.java trunk/jython/src/org/python/util/JycompileAntTask.java Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/build.xml 2009-01-18 10:04:29 UTC (rev 5945) @@ -101,9 +101,9 @@ </echo> </target> - <target name="jarless" depends="compile, copy-lib"/> + <target name="jarless" depends="compile, pycompile"/> - <target name="developer-build" depends="prepare-output, jar, copy-lib" description="a local build for developers" /> + <target name="developer-build" depends="prepare-output, pycompile" description="a local build for developers" /> <target name="full-build" depends="full-check, install" description="a full build with svn checkout" /> @@ -364,7 +364,7 @@ <!-- skip-brand can be set in ant.properties or as a system property to keep from updating the version.properties file and making the jar on every developer build. --> - <target name="brand-version" depends="version-init, svnversion" unless="skip-brand"> + <target name="brand-version" depends="init, svnversion" unless="skip-brand"> <condition property="build.svn.revision" value=""> <not> <isset property="build.svn.revision"/> @@ -522,8 +522,10 @@ <rule pattern="org.jruby.ext.posix.**" result="org.python.posix.@1"/> <zipfileset src="extlibs/constantine-0.4.jar"/> <rule pattern="com.kenai.constantine.**" result="org.python.constantine.@1"/> - <zipfileset src="extlibs/xercesImpl-2.9.1.jar"/> - <rule pattern="org.apache.**" result="org.python.apache.@1"/> + <rule pattern="org.apache.xml.**" result="org.python.apache.xml.@1"/> + <rule pattern="org.apache.xerces.**" result="org.python.apache.xerces.@1"/> + <rule pattern="org.apache.wml.**" result="org.python.apache.wml.@1"/> + <rule pattern="org.apache.html.**" result="org.python.apache.html.@1"/> <manifest> <attribute name="Main-Class" value="org.python.util.jython" /> <attribute name="Built-By" value="${user.name}" /> @@ -637,6 +639,15 @@ </copy> </target> + <target name="pycompile" depends="jar,copy-lib"> + <taskdef name="jycompile" classname="org.python.util.JycompileAntTask"> + <classpath path="${dist.dir}/Lib"/> + <classpath path="${dist.dir}/${jython.dev.jar}" /> + <classpath refid="main.classpath" /> + </taskdef> + <jycompile srcdir="${dist.dir}/Lib" destdir="${dist.dir}/Lib" excludes="test/**"/> + </target> + <target name="copy-lib" depends="init, copy-javalib, copy-cpythonlib"> <!-- XXX untested and most likely broken in 2.5 <copy todir="${dist.dir}" preservelastmodified="true"> Modified: trunk/jython/src/org/python/compiler/Future.java =================================================================== --- trunk/jython/src/org/python/compiler/Future.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/compiler/Future.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -26,8 +26,7 @@ return false; int n = cand.getInternalNames().size(); if (n == 0) { - throw new ParseException( - "future statement does not support import *",cand); + throw new ParseException("future statement does not support import *", cand); } for (int i = 0; i < n; i++) { String feature = cand.getInternalNames().get(i).getInternalName(); @@ -56,8 +55,7 @@ if (feature.equals("GIL") || feature.equals("global_interpreter_lock")) { throw new ParseException("Never going to happen!", cand); } - throw new ParseException("future feature "+feature+ - " is not defined",cand); + throw new ParseException("future feature " + feature + " is not defined", cand); } return true; } @@ -90,7 +88,7 @@ if (!(s instanceof ImportFrom)) break; s.from_future_checked = true; - if (!check((ImportFrom) s)) + if (!check((ImportFrom)s)) break; } @@ -119,7 +117,7 @@ public boolean areDivisionOn() { return division; } - + public boolean withStatementSupported() { return with_statement; } Modified: trunk/jython/src/org/python/compiler/Module.java =================================================================== --- trunk/jython/src/org/python/compiler/Module.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/compiler/Module.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -435,12 +435,12 @@ c.invokevirtual("org/python/core/PyFrame", "getname_or_null", "(" + $str + ")" + $pyObj); c.dup(); c.ifnonnull(label_got_name); - + c.pop(); c.aload(1); c.ldc("__name__"); c.invokevirtual("org/python/core/PyFrame", "getname_or_null", "(" + $str + ")" + $pyObj); - + c.label(label_got_name); c.astore(module_tmp); c.aload(1); @@ -448,7 +448,7 @@ c.aload(module_tmp); c.invokevirtual("org/python/core/PyFrame", "setlocal", "(" + $str + $pyObj + ")V"); } - + Label genswitch = new Label(); if (scope.generator) { c.goto_(genswitch); @@ -476,13 +476,13 @@ compiler.parse(tree, c, fast_locals, className, classBody, scope, cflags); - + // similar to visitResume code in pyasm.py if (scope.generator) { c.label(genswitch); - + c.aload(1); - c.getfield("org/python/core/PyFrame", "f_lasti", "I"); + c.getfield("org/python/core/PyFrame", "f_lasti", "I"); Label[] yields = new Label[compiler.yields.size()+1]; yields[0] = start; @@ -637,7 +637,7 @@ public static void compile(mod node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, - org.python.core.CompilerFlags cflags) + CompilerFlags cflags) throws Exception { Module module = new Module(name, filename, linenumbers); Modified: trunk/jython/src/org/python/compiler/ScopesCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/ScopesCompiler.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/compiler/ScopesCompiler.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -19,11 +19,11 @@ import org.python.antlr.ast.Return; import org.python.antlr.ast.With; import org.python.antlr.ast.Yield; -import org.python.antlr.ast.comprehension; import org.python.antlr.ast.arguments; import org.python.antlr.ast.expr_contextType; import org.python.antlr.base.expr; import org.python.antlr.base.stmt; +import org.python.core.ParserFacade; import java.util.ArrayList; import java.util.Hashtable; @@ -85,8 +85,7 @@ try { visit(node); } catch (Throwable t) { - throw org.python.core.ParserFacade.fixParseError(null, t, - code_compiler.getFilename()); + throw ParserFacade.fixParseError(null, t, code_compiler.getFilename()); } } @@ -138,10 +137,10 @@ beginScope(node.getInternalName(), FUNCSCOPE, node, ac); int n = ac.names.size(); for (int i = 0; i < n; i++) { - cur.addParam((String) ac.names.get(i)); + cur.addParam(ac.names.get(i)); } for (int i = 0; i < ac.init_code.size(); i++) { - visit((stmt) ac.init_code.get(i)); + visit(ac.init_code.get(i)); } cur.markFromParam(); suite(node.getInternalBody()); @@ -291,7 +290,7 @@ traverse(node); return null; } - + @Override public Object visitReturn(Return node) throws Exception { if (node.getInternalValue() != null) { Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -495,23 +495,23 @@ jarIndex = lowerCaseClasspath.indexOf(JYTHON_DEV_JAR); } if (jarIndex >= 0) { - int start = classpath.lastIndexOf(java.io.File.pathSeparator, jarIndex) + 1; + int start = classpath.lastIndexOf(File.pathSeparator, jarIndex) + 1; root = classpath.substring(start, jarIndex); } else { // in case JYTHON_JAR is referenced from a MANIFEST inside another jar on the classpath - root = jarFileName; + root = new File(jarFileName).getParent(); } } } - if (root != null) { - File rootFile = new File(root); - try { - root = rootFile.getCanonicalPath(); - } catch (IOException ioe) { - root = rootFile.getAbsolutePath(); - } + if (root == null) { + return null; } - return root; + File rootFile = new File(root); + try { + return rootFile.getCanonicalPath(); + } catch (IOException ioe) { + return rootFile.getAbsolutePath(); + } } public static void determinePlatform(Properties props) { @@ -554,8 +554,7 @@ } try { addRegistryFile(new File(prefix, "registry")); - File homeFile = new File(registry.getProperty("user.home"), - ".jython"); + File homeFile = new File(registry.getProperty("user.home"), ".jython"); addRegistryFile(homeFile); } catch (Exception exc) { } @@ -615,21 +614,23 @@ } public static synchronized void initialize() { - initialize(null, null, new String[] {""}); + initialize(null, null); } + public static synchronized void initialize(Properties preProperties, Properties postProperties) { + initialize(preProperties, postProperties, new String[] {""}); + } + public static synchronized void initialize(Properties preProperties, Properties postProperties, - String[] argv) - { + String[] argv) { initialize(preProperties, postProperties, argv, null); } public static synchronized void initialize(Properties preProperties, Properties postProperties, String[] argv, - ClassLoader classLoader) - { + ClassLoader classLoader) { initialize(preProperties, postProperties, argv, classLoader, new ClassicPyObjectAdapter()); } Modified: trunk/jython/src/org/python/core/imp.java =================================================================== --- trunk/jython/src/org/python/core/imp.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/core/imp.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -133,6 +133,14 @@ return data; } + public static byte[] compileSource(String name, File file) { + return compileSource(name, file, null); + } + + public static byte[] compileSource(String name, File file, String sourceFilename) { + return compileSource(name, file, sourceFilename, null); + } + public static byte[] compileSource(String name, File file, String sourceFilename, String compiledFilename) { if (sourceFilename == null) { @@ -209,8 +217,7 @@ } } - public static PyObject createFromSource(String name, InputStream fp, - String filename) { + public static PyObject createFromSource(String name, InputStream fp, String filename) { return createFromSource(name, fp, filename, null); } Modified: trunk/jython/src/org/python/expose/generate/ExposeTask.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/expose/generate/ExposeTask.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -7,93 +7,44 @@ 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.GlobPatternMapper; -import org.apache.tools.ant.util.SourceFileScanner; import org.objectweb.asm.ClassWriter; -import org.python.util.Generic; +import org.python.util.GlobMatchingTask; -public class ExposeTask extends MatchingTask { +public class ExposeTask extends GlobMatchingTask { - private Path src; - - private 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); - } + @Override + protected String getFrom() { + return "*.class"; } - /** - * Gets the source dirs to find the class files to be exposed. - */ - public Path getSrcdir() { - return src; + @Override + protected String getTo() { + return "*.class"; } - /** - * 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 - // documentation inherited - 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); - } - if(toExpose.size() > 1) { + public void process(Set<File> toExpose) throws BuildException { + if (toExpose.size() > 1) { log("Exposing " + toExpose.size() + " classes"); - } else if(toExpose.size() == 1) { + } else if (toExpose.size() == 1) { log("Exposing 1 class"); } - for(File f : toExpose) { + for (File f : toExpose) { ExposedTypeProcessor etp; try { etp = new ExposedTypeProcessor(new FileInputStream(f)); - } catch(IOException e) { + } catch (IOException e) { throw new BuildException("Unable to read '" + f + "' to expose it", e); - } catch(InvalidExposingException iee) { + } catch (InvalidExposingException iee) { throw new BuildException(iee.getMessage()); } - for(MethodExposer exposer : etp.getMethodExposers()) { + for (MethodExposer exposer : etp.getMethodExposers()) { generate(exposer); } - for(DescriptorExposer exposer : etp.getDescriptorExposers()) { + for (DescriptorExposer exposer : etp.getDescriptorExposers()) { generate(exposer); } - if(etp.getNewExposer() != null) { + if (etp.getNewExposer() != null) { generate(etp.getNewExposer()); } generate(etp.getTypeExposer()); @@ -114,39 +65,16 @@ try { out = new FileOutputStream(dest); out.write(newClassfile); - } catch(IOException e) { + } catch (IOException e) { throw new BuildException("Unable to write to '" + dest + "'", e); } finally { - if(out != null) { + if (out != null) { try { out.close(); - } catch(IOException e) { + } catch (IOException e) { // Le sigh... } } } } - - protected void scanDir(File srcDir, File destDir, String[] files) { - GlobPatternMapper m = new GlobPatternMapper(); - m.setFrom("*.class"); - m.setTo("*.class"); - 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()); - } - } } Modified: trunk/jython/src/org/python/modules/_py_compile.java =================================================================== --- trunk/jython/src/org/python/modules/_py_compile.java 2009-01-18 00:53:10 UTC (rev 5944) +++ trunk/jython/src/org/python/modules/_py_compile.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -13,41 +13,46 @@ public static PyList __all__ = new PyList(new PyString[] { new PyString("compile") }); public static boolean compile(String filename, String cfile, String dfile) { - // Resolve relative path names. dfile is only used for error - // messages and should not be resolved + // Resolve relative path names. dfile is only used for error messages and should not be + // resolved PySystemState sys = Py.getSystemState(); filename = sys.getPath(filename); cfile = sys.getPath(cfile); - + File file = new File(filename); if (!file.exists()) { throw Py.IOError(Errno.ENOENT, filename); } - String name = file.getName(); + String name = getModuleName(file); + + byte[] bytes = org.python.core.imp.compileSource(name, file, dfile, cfile); + org.python.core.imp.cacheCompiledSource(filename, cfile, bytes); + + return bytes.length > 0; + } + + public static final String getModuleName(File f) { + String name = f.getName(); int dot = name.lastIndexOf('.'); if (dot != -1) { name = name.substring(0, dot); } // name the __init__ module after its package - File dir = file.getParentFile(); - if (dir != null && name.equals("__init__")) { + File dir = f.getParentFile(); + if (name.equals("__init__")) { name = dir.getName(); dir = dir.getParentFile(); } - // Make the compiled classfile's name the fully qualified with a package by - // walking up the directory tree looking for __init__.py files. Don't - // check for __init__$py.class since we're compiling source here and the - // existence of a class file without corresponding source probably doesn't - // indicate a package. + // Make the compiled classfile's name fully qualified with a package by walking up the + // directory tree looking for __init__.py files. Don't check for __init__$py.class since + // we're compiling source here and the existence of a class file without corresponding + // source probably doesn't indicate a package. while (dir != null && (new File(dir, "__init__.py").exists())) { name = dir.getName() + "." + name; dir = dir.getParentFile(); } - byte[] bytes = org.python.core.imp.compileSource(name, file, dfile, cfile); - org.python.core.imp.cacheCompiledSource(filename, cfile, bytes); - - return bytes.length > 0; + return name; } } Added: trunk/jython/src/org/python/util/GlobMatchingTask.java =================================================================== --- trunk/jython/src/org/python/util/GlobMatchingTask.java (rev 0) +++ trunk/jython/src/org/python/util/GlobMatchingTask.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -0,0 +1,101 @@ +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.GlobPatternMapper; +import org.apache.tools.ant.util.SourceFileScanner; + +public abstract class GlobMatchingTask extends MatchingTask { + + 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 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 Added: trunk/jython/src/org/python/util/JycompileAntTask.java =================================================================== --- trunk/jython/src/org/python/util/JycompileAntTask.java (rev 0) +++ trunk/jython/src/org/python/util/JycompileAntTask.java 2009-01-18 10:04:29 UTC (rev 5945) @@ -0,0 +1,56 @@ +package org.python.util; + +import java.io.File; +import java.util.Properties; +import java.util.Set; + +import org.apache.tools.ant.BuildException; +import org.python.core.PyException; +import org.python.core.PySystemState; +import org.python.core.imp; +import org.python.modules._py_compile; + +public class JycompileAntTask extends GlobMatchingTask { + + @Override + public void process(Set<File> toCompile) throws BuildException { + 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"); + 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); + } + } + + protected String getFrom() { + return "*.py"; + } + + protected String getTo() { + return "*$py.class"; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |