587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.core.ant.taskdefs;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.io.FileWriter;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.io.PrintWriter;
|
|
24 |
|
|
25 |
import org.apache.tools.ant.BuildException;
|
628
|
26 |
import org.apache.tools.ant.Project;
|
587
|
27 |
import org.apache.tools.ant.Task;
|
|
28 |
import org.apache.tools.ant.taskdefs.ExecTask;
|
|
29 |
import org.apache.tools.ant.types.Commandline;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Embed python code in ant, generates temporary python file and executes it.
|
|
33 |
*
|
|
34 |
* <pre>
|
|
35 |
* Usage: <hlm:python>
|
|
36 |
* .
|
|
37 |
* .
|
|
38 |
* .
|
|
39 |
* </hlm:python>
|
|
40 |
* </pre>
|
|
41 |
*
|
|
42 |
* @ant.task name="python" category="Core"
|
|
43 |
*/
|
|
44 |
public class PythonTask extends Task {
|
|
45 |
private String iText;
|
|
46 |
private String outputProperty;
|
|
47 |
private String resultProperty;
|
|
48 |
private File script;
|
|
49 |
|
|
50 |
private File dir;
|
|
51 |
private File output;
|
|
52 |
|
|
53 |
private boolean iFailonerror;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Sets the property name whose value should be set to the output of the
|
|
57 |
* process.
|
|
58 |
*
|
|
59 |
* @param propertyname
|
|
60 |
* name of property.
|
|
61 |
* @ant.not-required
|
|
62 |
*/
|
|
63 |
public void setOutputProperty(String propertyname) {
|
|
64 |
outputProperty = propertyname;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Sets the name of a property in which the return code of the command
|
|
69 |
* should be stored. Only of interest if failonerror=false.
|
|
70 |
*
|
|
71 |
* @param propertyname
|
|
72 |
* name of property.
|
|
73 |
* @ant.not-required .
|
|
74 |
*/
|
|
75 |
public void setResultProperty(String propertyname) {
|
|
76 |
resultProperty = propertyname;
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Add python statements to execute.
|
|
81 |
*
|
|
82 |
* @param text
|
|
83 |
* is the python statement to execute
|
|
84 |
* @ant.required If no script.
|
|
85 |
*/
|
|
86 |
public void addText(String text) {
|
|
87 |
iText = getProject().replaceProperties(text);
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Set the name of the python script to execute.
|
|
92 |
*
|
|
93 |
* @param scriptname
|
|
94 |
* name of the script to set.
|
|
95 |
* @ant.required If no text.
|
|
96 |
*/
|
|
97 |
public void setScript(File scriptname) {
|
|
98 |
script = scriptname;
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Fail if the command exits with a non-zero return code.
|
|
103 |
*
|
|
104 |
* @param failonerror
|
|
105 |
* if true fail the command on non-zero return code.
|
|
106 |
* @ant.not-required Default is no.
|
|
107 |
*/
|
|
108 |
public void setFailonerror(boolean failonerror) {
|
|
109 |
iFailonerror = failonerror;
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* File the output of the process is redirected to. If error is not
|
|
114 |
* redirected, it too will appear in the output.
|
|
115 |
*
|
|
116 |
* @param outputname
|
|
117 |
* name of a file to which output should be sent.
|
|
118 |
* @ant.not-required
|
|
119 |
*/
|
|
120 |
public void setOutput(File outputname) {
|
|
121 |
output = outputname;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Set the working directory of the process.
|
|
126 |
*
|
|
127 |
* @param dirname
|
|
128 |
* the working directory of the process.
|
|
129 |
* @ant.not-required
|
|
130 |
*/
|
|
131 |
public void setDir(File dirname) {
|
|
132 |
dir = dirname;
|
|
133 |
}
|
|
134 |
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Method executes current task.
|
|
138 |
*/
|
|
139 |
public void execute() {
|
|
140 |
if (script != null) {
|
|
141 |
if (!script.exists()) {
|
|
142 |
throw new BuildException("Could not find " + script);
|
|
143 |
}
|
|
144 |
// Run the Python script
|
|
145 |
ExecTask execTask = new ExecTask();
|
|
146 |
execTask.setProject(getProject());
|
|
147 |
execTask.setTaskName(getTaskName());
|
|
148 |
execTask.setExecutable("python");
|
|
149 |
execTask.setFailonerror(true);
|
|
150 |
execTask.createArg().setFile(script);
|
|
151 |
execTask.setOutputproperty(outputProperty);
|
|
152 |
execTask.setResultProperty(resultProperty);
|
|
153 |
execTask.setDir(dir);
|
|
154 |
execTask.setOutput(output);
|
|
155 |
try {
|
|
156 |
execTask.execute();
|
628
|
157 |
} catch (BuildException ex) {
|
|
158 |
if (iFailonerror) {
|
|
159 |
throw new BuildException(ex.getMessage(), ex);
|
|
160 |
} else {
|
|
161 |
log(ex.getMessage(), Project.MSG_ERR);
|
|
162 |
}
|
587
|
163 |
}
|
|
164 |
} else if (iText != null) {
|
|
165 |
// Write the content of the script using Echo task
|
|
166 |
File tempfile = null;
|
|
167 |
try {
|
|
168 |
// Create a temporary file to contain the script
|
|
169 |
tempfile = File.createTempFile("helium", null);
|
|
170 |
PrintWriter out = new PrintWriter(new FileWriter(tempfile));
|
|
171 |
out.write(iText);
|
|
172 |
out.close();
|
|
173 |
|
|
174 |
// Run the temporary Python script
|
|
175 |
ExecTask execTask = new ExecTask();
|
|
176 |
execTask.setProject(getProject());
|
|
177 |
execTask.setTaskName(getTaskName());
|
|
178 |
execTask.setExecutable("python");
|
|
179 |
execTask.setFailonerror(iFailonerror);
|
|
180 |
Commandline.Argument scriptArg = execTask.createArg();
|
|
181 |
scriptArg.setValue(tempfile.getAbsolutePath());
|
|
182 |
execTask.setOutputproperty(outputProperty);
|
|
183 |
execTask.setResultProperty(resultProperty);
|
|
184 |
execTask.setDir(dir);
|
|
185 |
execTask.setOutput(output);
|
|
186 |
execTask.execute();
|
|
187 |
|
|
188 |
// Delete temporary script file
|
|
189 |
boolean fileDeleted = tempfile.delete();
|
|
190 |
if (!fileDeleted && iFailonerror) {
|
|
191 |
throw new BuildException("Could not delete script file!");
|
|
192 |
}
|
628
|
193 |
} catch (IOException ex) {
|
587
|
194 |
if (iFailonerror) {
|
628
|
195 |
throw new BuildException(ex.getMessage(), ex);
|
587
|
196 |
}
|
628
|
197 |
log("Error while running python task: " + ex.getMessage(), Project.MSG_ERR);
|
587
|
198 |
} finally {
|
|
199 |
// make sure we delete the file anyway
|
628
|
200 |
if (tempfile != null) {
|
587
|
201 |
tempfile.delete();
|
628
|
202 |
}
|
587
|
203 |
}
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
} |