001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2002 The Apache Software Foundation. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright notice,
010: * this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright notice,
013: * this list of conditions and the following disclaimer in the documentation
014: * and/or other materials provided with the distribution.
015: *
016: * 3. The end-user documentation included with the redistribution, if any, must
017: * include the following acknowlegement: "This product includes software
018: * developed by the Apache Software Foundation (http://www.apache.org/)."
019: * Alternately, this acknowlegement may appear in the software itself, if and
020: * wherever such third-party acknowlegements normally appear.
021: *
022: * 4. The names "The Jakarta Project", "Ant", and "Apache Software Foundation"
023: * must not be used to endorse or promote products derived from this software
024: * without prior written permission. For written permission, please contact
025: * apache@apache.org.
026: *
027: * 5. Products derived from this software may not be called "Apache" nor may
028: * "Apache" appear in their names without prior written permission of the Apache
029: * Group.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
032: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
033: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE
034: * SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
035: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
036: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
037: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
038: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
039: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
040: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
041: * ====================================================================
042: *
043: * This software consists of voluntary contributions made by many individuals on
044: * behalf of the Apache Software Foundation. For more information on the Apache
045: * Software Foundation, please see <http://www.apache.org/>.
046: */
047: package org.python.util;
048:
049: import org.apache.tools.ant.taskdefs.MatchingTask;
050: import org.apache.tools.ant.taskdefs.Execute;
051: import org.apache.tools.ant.BuildException;
052: import org.apache.tools.ant.taskdefs.Java;
053: import org.apache.tools.ant.types.Path;
054: import org.apache.tools.ant.DirectoryScanner;
055: import java.io.File;
056: import java.io.BufferedReader;
057: import java.io.InputStreamReader;
058: import java.io.FileInputStream;
059: import java.io.Reader;
060: import java.io.IOException;
061: import java.util.Map;
062: import java.util.HashMap;
063:
064: /**
065: * Template is an Ant task for generating new-style object definitions based on
066: * template files. These template files come in two flavors; *.expose and
067: * *.derived, both are supported by this task.
068: *
069: * @author Matt Small - msmall@activegrid.com
070: * @version 1.0
071: */
072: public class TemplateAntTask extends MatchingTask {
073:
074: /**
075: * Specifies the Python interpreter.
076: */
077: protected String python;
078:
079: /**
080: * Specifies the Python interpreter.
081: */
082: public void setPython(String aPE) {
083: python = aPE;
084: }
085:
086: /**
087: * Source paths.
088: */
089: private File srcDir;
090:
091: /**
092: * Source paths.
093: */
094: public void setSrcdir(String in) {
095: srcDir = new File(getProject().replaceProperties(in));
096: }
097:
098: /**
099: * Verbose flag.
100: */
101: protected boolean verbose = false;
102:
103: /**
104: * Verbose flag.
105: */
106: public void setVerbose(String in) {
107: verbose = (new Boolean(getProject().replaceProperties(in)))
108: .booleanValue();
109: }
110:
111: /**
112: * Lazy flag.
113: */
114: protected boolean lazy = false;
115:
116: /**
117: * Lazy flag.
118: */
119: public void setLazy(String in) {
120: lazy = (new Boolean(getProject().replaceProperties(in)))
121: .booleanValue();
122: }
123:
124: public void execute() {
125: if (null == srcDir) {
126: throw new BuildException("no srcdir specified");
127: } else if (!srcDir.exists()) {
128: throw new BuildException("srcdir '" + srcDir
129: + "' doesn't exist");
130: }
131: File gexposeScript = new File(srcDir.getAbsolutePath()
132: + File.separator + "gexpose.py");
133: File gderiveScript = new File(srcDir.getAbsolutePath()
134: + File.separator + "gderived.py");
135: if (!gexposeScript.exists()) {
136: throw new BuildException("no gexpose.py script found at: "
137: + gexposeScript);
138: }
139: if (!gderiveScript.exists()) {
140: throw new BuildException("no gderive.py script found at: "
141: + gderiveScript);
142: }
143: runPythonScript(gexposeScript.getAbsolutePath());
144: runPythonScript(gderiveScript.getAbsolutePath());
145: }
146:
147: private void runPythonScript(String script) throws BuildException {
148: if (null == python) {
149: python = "python";
150: }
151: Execute e = new Execute();
152: e.setWorkingDirectory(srcDir);
153: String[] command;
154: if (lazy) {
155: command = new String[] { python, script, "--lazy" };
156: } else {
157: command = new String[] { python, script };
158: }
159: e.setCommandline(command);
160: if (verbose) {
161: String out = "";
162: for (int k = 0; k < e.getCommandline().length; k++) {
163: out += (e.getCommandline()[k] + " ");
164: }
165: log("executing: " + out);
166: }
167: try {
168: e.execute();
169: } catch (IOException e2) {
170: throw new BuildException(e2.toString(), e2);
171: }
172: }
173: }
|