source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 10.9 KB
Line 
1/*
2 * Copyright 2001-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.optional;
18
19import java.io.BufferedOutputStream;
20import java.io.File;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.OutputStream;
24import java.io.PrintStream;
25import java.util.Enumeration;
26import java.util.Vector;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.Task;
30import org.apache.tools.ant.taskdefs.Execute;
31import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
32import org.apache.tools.ant.taskdefs.LogOutputStream;
33import org.apache.tools.ant.taskdefs.LogStreamHandler;
34import org.apache.tools.ant.taskdefs.PumpStreamHandler;
35import org.apache.tools.ant.taskdefs.condition.Os;
36import org.apache.tools.ant.types.Commandline;
37import org.apache.tools.ant.types.Path;
38
39/**
40 * Invokes the rpm tool to build a Linux installation file.
41 *
42 */
43public class Rpm extends Task {
44
45 /**
46 * the spec file
47 */
48 private String specFile;
49
50 /**
51 * the rpm top dir
52 */
53 private File topDir;
54
55 /**
56 * the rpm command to use
57 */
58 private String command = "-bb";
59
60 /**
61 * The executable to use for building the packages.
62 * @since Ant 1.6
63 */
64 private String rpmBuildCommand = null;
65
66 /**
67 * clean BUILD directory
68 */
69 private boolean cleanBuildDir = false;
70
71 /**
72 * remove spec file
73 */
74 private boolean removeSpec = false;
75
76 /**
77 * remove sources
78 */
79 private boolean removeSource = false;
80
81 /**
82 * the file to direct standard output from the command
83 */
84 private File output;
85
86 /**
87 * the file to direct standard error from the command
88 */
89 private File error;
90
91 /**
92 * Halt on error return value from rpm build.
93 */
94 private boolean failOnError = false;
95
96 /**
97 * Don't show output of RPM build command on console. This does not affect
98 * the printing of output and error messages to files.
99 */
100 private boolean quiet = false;
101
102 /**
103 * Execute the task
104 *
105 * @throws BuildException is there is a problem in the task execution.
106 */
107 public void execute() throws BuildException {
108
109 Commandline toExecute = new Commandline();
110
111 toExecute.setExecutable(rpmBuildCommand == null
112 ? guessRpmBuildCommand()
113 : rpmBuildCommand);
114 if (topDir != null) {
115 toExecute.createArgument().setValue("--define");
116 toExecute.createArgument().setValue("_topdir" + topDir);
117 }
118
119 toExecute.createArgument().setLine(command);
120
121 if (cleanBuildDir) {
122 toExecute.createArgument().setValue("--clean");
123 }
124 if (removeSpec) {
125 toExecute.createArgument().setValue("--rmspec");
126 }
127 if (removeSource) {
128 toExecute.createArgument().setValue("--rmsource");
129 }
130
131 toExecute.createArgument().setValue("SPECS/" + specFile);
132
133 ExecuteStreamHandler streamhandler = null;
134 OutputStream outputstream = null;
135 OutputStream errorstream = null;
136 if (error == null && output == null) {
137 if (!quiet) {
138 streamhandler = new LogStreamHandler(this, Project.MSG_INFO,
139 Project.MSG_WARN);
140 } else {
141 streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG,
142 Project.MSG_DEBUG);
143 }
144 } else {
145 if (output != null) {
146 try {
147 BufferedOutputStream bos
148 = new BufferedOutputStream(new FileOutputStream(output));
149 outputstream = new PrintStream(bos);
150 } catch (IOException e) {
151 throw new BuildException(e, getLocation());
152 }
153 } else if (!quiet) {
154 outputstream = new LogOutputStream(this, Project.MSG_INFO);
155 } else {
156 outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
157 }
158 if (error != null) {
159 try {
160 BufferedOutputStream bos
161 = new BufferedOutputStream(new FileOutputStream(error));
162 errorstream = new PrintStream(bos);
163 } catch (IOException e) {
164 throw new BuildException(e, getLocation());
165 }
166 } else if (!quiet) {
167 errorstream = new LogOutputStream(this, Project.MSG_WARN);
168 } else {
169 errorstream = new LogOutputStream(this, Project.MSG_DEBUG);
170 }
171 streamhandler = new PumpStreamHandler(outputstream, errorstream);
172 }
173
174 Execute exe = getExecute(toExecute, streamhandler);
175 try {
176 log("Building the RPM based on the " + specFile + " file");
177 int returncode = exe.execute();
178 if (Execute.isFailure(returncode)) {
179 String msg = "'" + toExecute.getExecutable()
180 + "' failed with exit code " + returncode;
181 if (failOnError) {
182 throw new BuildException(msg);
183 } else {
184 log(msg, Project.MSG_ERR);
185 }
186 }
187 } catch (IOException e) {
188 throw new BuildException(e, getLocation());
189 } finally {
190 if (output != null) {
191 try {
192 outputstream.close();
193 } catch (IOException e) {
194 // ignore any secondary error
195 }
196 }
197 if (error != null) {
198 try {
199 errorstream.close();
200 } catch (IOException e) {
201 // ignore any secondary error
202 }
203 }
204 }
205 }
206
207 /**
208 * The directory which will have the expected
209 * subdirectories, SPECS, SOURCES, BUILD, SRPMS ; optional.
210 * If this isn't specified,
211 * the <tt>baseDir</tt> value is used
212 *
213 * @param td the directory containing the normal RPM directories.
214 */
215 public void setTopDir(File td) {
216 this.topDir = td;
217 }
218
219 /**
220 * What command to issue to the rpm build tool; optional.
221 * The default is "-bb"
222 */
223 public void setCommand(String c) {
224 this.command = c;
225 }
226
227 /**
228 * The name of the spec File to use; required.
229 */
230 public void setSpecFile(String sf) {
231 if ((sf == null) || (sf.trim().equals(""))) {
232 throw new BuildException("You must specify a spec file", getLocation());
233 }
234 this.specFile = sf;
235 }
236
237 /**
238 * Flag (optional, default=false) to remove
239 * the generated files in the BUILD directory
240 */
241 public void setCleanBuildDir(boolean cbd) {
242 cleanBuildDir = cbd;
243 }
244
245 /**
246 * Flag (optional, default=false) to remove the spec file from SPECS
247 */
248 public void setRemoveSpec(boolean rs) {
249 removeSpec = rs;
250 }
251
252 /**
253 * Flag (optional, default=false)
254 * to remove the sources after the build.
255 * See the <tt>--rmsource</tt> option of rpmbuild.
256 */
257 public void setRemoveSource(boolean rs) {
258 removeSource = rs;
259 }
260
261 /**
262 * Optional file to save stdout to.
263 */
264 public void setOutput(File output) {
265 this.output = output;
266 }
267
268 /**
269 * Optional file to save stderr to
270 */
271 public void setError(File error) {
272 this.error = error;
273 }
274
275 /**
276 * The executable to run when building; optional.
277 * The default is <code>rpmbuild</code>.
278 *
279 * @since Ant 1.6
280 * @param c the rpm build executable
281 */
282 public void setRpmBuildCommand(String c) {
283 this.rpmBuildCommand = c;
284 }
285
286 /**
287 * If <code>true</code>, stop the build process when the rpmbuild command
288 * exits with an error status.
289 * @param value <code>true</code> if it should halt, otherwise
290 * <code>false</code>. The default is <code>false</code>.
291 *
292 * @since Ant 1.6.3
293 */
294 public void setFailOnError(boolean value) {
295 failOnError = value;
296 }
297
298 /**
299 * If true, output from the RPM build command will only be logged to DEBUG.
300 * @param value <code>false</code> if output should be logged, otherwise
301 * <code>true</code>. The default is <code>false</code>.
302 *
303 * @since Ant 1.6.3
304 */
305 public void setQuiet(boolean value) {
306 quiet = value;
307 }
308
309 /**
310 * Checks whether <code>rpmbuild</code> is on the PATH and returns
311 * the absolute path to it - falls back to <code>rpm</code>
312 * otherwise.
313 *
314 * @since 1.6
315 */
316 protected String guessRpmBuildCommand() {
317 Vector env = Execute.getProcEnvironment();
318 String path = null;
319 for (Enumeration e = env.elements(); e.hasMoreElements();) {
320 String var = (String) e.nextElement();
321 if (var.startsWith("PATH=") || var.startsWith("Path=")) {
322 path = var.substring(6 /* "PATH=".length() + 1 */);
323 break;
324 }
325 }
326
327 if (path != null) {
328 Path p = new Path(getProject(), path);
329 String[] pElements = p.list();
330 for (int i = 0; i < pElements.length; i++) {
331 File f = new File(pElements[i],
332 "rpmbuild"
333 + (Os.isFamily("dos") ? ".exe" : ""));
334 if (f.canRead()) {
335 return f.getAbsolutePath();
336 }
337 }
338 }
339
340 return "rpm";
341 }
342
343 /**
344 * @since Ant 1.6.3
345 */
346 protected Execute getExecute(Commandline toExecute,
347 ExecuteStreamHandler streamhandler) {
348 Execute exe = new Execute(streamhandler, null);
349
350 exe.setAntRun(getProject());
351 if (topDir == null) {
352 topDir = getProject().getBaseDir();
353 }
354 exe.setWorkingDirectory(topDir);
355
356 exe.setCommandline(toExecute.getCommandline());
357 return exe;
358 }
359}
Note: See TracBrowser for help on using the repository browser.