Index: jython/src/org/python/util/JythonTaskVar.java =================================================================== --- jython/src/org/python/util/JythonTaskVar.java (revision 0) +++ jython/src/org/python/util/JythonTaskVar.java (revision 0) @@ -0,0 +1,87 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowlegement: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, if and + * wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software Foundation" + * must not be used to endorse or promote products derived from this software + * without prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" nor may + * "Apache" appear in their names without prior written permission of the Apache + * Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE + * SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many individuals on + * behalf of the Apache Software Foundation. For more information on the Apache + * Software Foundation, please see . + */ +package org.python.util; + +/** + * We use this class to read in variable inner elements + * of the JythonTask + * @author Byron Foster (Base2 Corporation ) + */ +public class JythonTaskVar { + + private String name = null; + private String value = null; + /** + * @return String + */ + public String getName() { + return name; + } + + /** + * Sets the name. + * @param name The name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return String + */ + public String getValue() { + return value; + } + + /** + * Sets the value. + * @param value The value to set + */ + public void setValue(String value) { + this.value = value; + } +} Index: jython/src/org/python/util/JythonTaskArg.java =================================================================== --- jython/src/org/python/util/JythonTaskArg.java (revision 0) +++ jython/src/org/python/util/JythonTaskArg.java (revision 0) @@ -0,0 +1,65 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowlegement: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, if and + * wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software Foundation" + * must not be used to endorse or promote products derived from this software + * without prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" nor may + * "Apache" appear in their names without prior written permission of the Apache + * Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE + * SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many individuals on + * behalf of the Apache Software Foundation. For more information on the Apache + * Software Foundation, please see . + */ +package org.python.util; + +/** + * We use this class to read in variable inner elements + * of the JythonTask + * @author Byron Foster (Base2 Corporation ) + */ +public class JythonTaskArg { + + private String arg = null; + + public String getValue() { + return arg; + } + + public void setValue(String arg) { + this.arg = arg; + } +} Index: jython/src/org/python/util/JythonTask.java =================================================================== --- jython/src/org/python/util/JythonTask.java (revision 0) +++ jython/src/org/python/util/JythonTask.java (revision 0) @@ -0,0 +1,247 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The end-user documentation included with the redistribution, if any, must + * include the following acknowlegement: "This product includes software + * developed by the Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, if and + * wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software Foundation" + * must not be used to endorse or promote products derived from this software + * without prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" nor may + * "Apache" appear in their names without prior written permission of the Apache + * Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE + * SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many individuals on + * behalf of the Apache Software Foundation. For more information on the Apache + * Software Foundation, please see . + */ +package org.python.util; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +import org.python.util.PythonInterpreter; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Properties; +import java.lang.System; +import org.apache.tools.ant.types.Reference; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.Project; + + +/** + * The JythonTask is an Ant task + * that executes a single jython script. To use the task you must + * first define the task with the taskdef element within the + * build xml file: + *
+ *   <taskdef name="jython" classname="org.python.util.JythonTask"
+ *           classpath="/foo/bar/jython/jython.jar" />
+ * 
+ * The simplest way to run a python script is with the below example + * target. This will execute the foo.py file. Also, the python + * variables 'var1' and 'var2" will be initialized with the value of + * '123' and 'abc' respectively. + *
+ *    <target name="run-foo">
+ *      <jython pythonFile="foo.py">
+ *        <defvar name="var1" value="123" />
+ *        <defvar name="var2" value="abc" />
+ *        ...
+ *      </jython>
+ *    </target>
+ * 
+ * The arg element passes command line arguents to the script. In + * this example The arg elements will pass the values '123' and '456' + * as command line parameters to the script as sys.argv[1] and + * sys.argv[2] respectively. + *
+ *      <jython pythonFile="foo.py">
+ *        <arg value="123" />
+ *        <arg value="456" />
+ *        ...
+ *      </jython>
+ * 
+ * Using pythonPath will provide jython a path to search for python + * libraries referenced in the script (either .py or .pyc files). + * pythonPath can also be use to point to the standard python library + * files if they are not contained within jython.jar defined in the + * taskdef derective. + *
+ *      <jython pythonFile="foo.py" pythonPath="/foo/bar/mypythonlib">
+ *        <defvar name="var1" value="123" />
+ *      </jython>
+ * 
+ * + * @author Byron Foster (Base2 Corporation ) + */ +public class JythonTask extends Task { + + private File pythonFile = null; + private File jythonHome = null; + private Path pythonPath = null; + + private ArrayList varList = new ArrayList(); + private ArrayList argList = new ArrayList(); + private Reference pathRef = null; + + public void execute() throws BuildException { + // Do some param checking + + if (pythonFile == null) { + throw new BuildException("Property pythonFile must be set"); + } + else if (!pythonFile.exists()) { + throw new BuildException("pythonFile: " + pythonFile + + "' does not exist"); + } + else if (!pythonFile.canRead()) { + throw new BuildException("pythonFile '" + pythonFile + + "' exists but it is not readable"); + } + + if (pythonPath != null && pathRef != null) { + throw new BuildException("Attributes pythonPath and pathRef cannot both be set"); + } + + FileInputStream fileStream = null; + try { + fileStream = new FileInputStream(pythonFile); + } + catch (FileNotFoundException e) { + // We've already tested above if the file exists, + // and if it is readable so we should never get here! + throw new BuildException(e); + } + + log("Running python script: " + pythonFile, Project.MSG_DEBUG); + log("With arguments:", Project.MSG_DEBUG); + + Properties jyprops = new Properties(); + + //convert list to array of strings. + String argv[] = new String[argList.size()+1]; + argv[0] = pythonFile.getPath(); + for (int i=argList.size()-1; i >= 0; i--) { + JythonTaskArg arg = (JythonTaskArg)argList.get(i); + argv[i+1] = arg.getValue(); + log("arg " + i + ": " + argv[i], Project.MSG_DEBUG); + } + + // If the user has defined PythonPathRef then use it for the + // python.path, else use the system classpath. + if (pathRef != null) { + Path pypath = new Path(getProject()); + pypath.setRefid(pathRef); + jyprops.put("python.path", pypath.toString()); + } + else if (pythonPath != null) { + jyprops.put("python.path", pythonPath.toString()); + } + else { + jyprops.put("python.path", System.getProperty("java.class.path")); + } + + log("property python.path: " + + jyprops.getProperty("python.path"), Project.MSG_DEBUG); + + PythonInterpreter.initialize(System.getProperties(), jyprops, argv); + + PythonInterpreter interpreter = new PythonInterpreter(); + + // read the defined variables specified in the ant script and + // add them to the interpreter. + Iterator itr = varList.iterator(); + while (itr.hasNext()) { + JythonTaskVar element = (JythonTaskVar) itr.next(); + log("defined var name: " + + element.getName() + + " value: " + + element.getValue(), Project.MSG_DEBUG); + interpreter.set(element.getName(), element.getValue()); + } + + interpreter.execfile(fileStream); + } + + /** + * Path to the python script to run. Required + */ + public void setPythonFile(File file) { + pythonFile = file; + } + + /** + * Python path consisting either of directories or jar files that + * contain python files to load. The pythonPath and pythonPathRef + * attributes cannot both be set. Not Required + */ + public void setPythonPath(Path path) { + pythonPath = path; + } + + + /** + * For the nested <defvar name="foo" value="bar"> element. The defvar + * element Defines initial variable names and values for the python + * script. See the class documentation for examples. + */ + public void addDefvar(JythonTaskVar variable) { + varList.add(variable); + } + + /** + * For the nested <arg value="foo"> element. The arg element + * defines arguments that are passed to the python script as command + * line arguments. These argument can then be read from within the + * script with the sys.argv list. + */ + public void addArg(JythonTaskArg arg) { + argList.add(arg); + } + + /** + * A path reference to be used as the python path. The python path + * consists of directories or jar files that contain python files to + * load. The pythonPath and pythonPathRef attributes cannot both be + * set. Not Required + */ + public void setPythonPathRef(Reference r) { + pathRef = r; + } + +}