1 18 package org.apache.tools.ant.taskdefs.optional; 19 20 import java.io.BufferedOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintStream ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.util.FileUtils; 32 import org.apache.tools.ant.taskdefs.Execute; 33 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 34 import org.apache.tools.ant.taskdefs.LogOutputStream; 35 import org.apache.tools.ant.taskdefs.LogStreamHandler; 36 import org.apache.tools.ant.taskdefs.PumpStreamHandler; 37 import org.apache.tools.ant.taskdefs.condition.Os; 38 import org.apache.tools.ant.types.Commandline; 39 import org.apache.tools.ant.types.Path; 40 41 45 public class Rpm extends Task { 46 47 private static final String PATH1 = "PATH="; 48 private static final String PATH2 = "Path="; 49 private static final String PATH3 = "path="; 50 private static final int PATH_LEN = PATH1.length(); 51 52 55 private String specFile; 56 57 60 private File topDir; 61 62 65 private String command = "-bb"; 66 67 71 private String rpmBuildCommand = null; 72 73 76 private boolean cleanBuildDir = false; 77 78 81 private boolean removeSpec = false; 82 83 86 private boolean removeSource = false; 87 88 91 private File output; 92 93 96 private File error; 97 98 101 private boolean failOnError = false; 102 103 107 private boolean quiet = false; 108 109 114 public void execute() throws BuildException { 115 116 Commandline toExecute = new Commandline(); 117 118 toExecute.setExecutable(rpmBuildCommand == null 119 ? guessRpmBuildCommand() 120 : rpmBuildCommand); 121 if (topDir != null) { 122 toExecute.createArgument().setValue("--define"); 123 toExecute.createArgument().setValue("_topdir" + topDir); 124 } 125 126 toExecute.createArgument().setLine(command); 127 128 if (cleanBuildDir) { 129 toExecute.createArgument().setValue("--clean"); 130 } 131 if (removeSpec) { 132 toExecute.createArgument().setValue("--rmspec"); 133 } 134 if (removeSource) { 135 toExecute.createArgument().setValue("--rmsource"); 136 } 137 138 toExecute.createArgument().setValue("SPECS/" + specFile); 139 140 ExecuteStreamHandler streamhandler = null; 141 OutputStream outputstream = null; 142 OutputStream errorstream = null; 143 if (error == null && output == null) { 144 if (!quiet) { 145 streamhandler = new LogStreamHandler(this, Project.MSG_INFO, 146 Project.MSG_WARN); 147 } else { 148 streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG, 149 Project.MSG_DEBUG); 150 } 151 } else { 152 if (output != null) { 153 try { 154 BufferedOutputStream bos 155 = new BufferedOutputStream (new FileOutputStream (output)); 156 outputstream = new PrintStream (bos); 157 } catch (IOException e) { 158 throw new BuildException(e, getLocation()); 159 } 160 } else if (!quiet) { 161 outputstream = new LogOutputStream(this, Project.MSG_INFO); 162 } else { 163 outputstream = new LogOutputStream(this, Project.MSG_DEBUG); 164 } 165 if (error != null) { 166 try { 167 BufferedOutputStream bos 168 = new BufferedOutputStream (new FileOutputStream (error)); 169 errorstream = new PrintStream (bos); 170 } catch (IOException e) { 171 throw new BuildException(e, getLocation()); 172 } 173 } else if (!quiet) { 174 errorstream = new LogOutputStream(this, Project.MSG_WARN); 175 } else { 176 errorstream = new LogOutputStream(this, Project.MSG_DEBUG); 177 } 178 streamhandler = new PumpStreamHandler(outputstream, errorstream); 179 } 180 181 Execute exe = getExecute(toExecute, streamhandler); 182 try { 183 log("Building the RPM based on the " + specFile + " file"); 184 int returncode = exe.execute(); 185 if (Execute.isFailure(returncode)) { 186 String msg = "'" + toExecute.getExecutable() 187 + "' failed with exit code " + returncode; 188 if (failOnError) { 189 throw new BuildException(msg); 190 } else { 191 log(msg, Project.MSG_ERR); 192 } 193 } 194 } catch (IOException e) { 195 throw new BuildException(e, getLocation()); 196 } finally { 197 FileUtils.close(outputstream); 198 FileUtils.close(errorstream); 199 } 200 } 201 202 210 public void setTopDir(File td) { 211 this.topDir = td; 212 } 213 214 219 public void setCommand(String c) { 220 this.command = c; 221 } 222 223 227 public void setSpecFile(String sf) { 228 if ((sf == null) || (sf.trim().equals(""))) { 229 throw new BuildException("You must specify a spec file", getLocation()); 230 } 231 this.specFile = sf; 232 } 233 234 239 public void setCleanBuildDir(boolean cbd) { 240 cleanBuildDir = cbd; 241 } 242 243 247 public void setRemoveSpec(boolean rs) { 248 removeSpec = rs; 249 } 250 251 257 public void setRemoveSource(boolean rs) { 258 removeSource = rs; 259 } 260 261 265 public void setOutput(File output) { 266 this.output = output; 267 } 268 269 273 public void setError(File error) { 274 this.error = error; 275 } 276 277 284 public void setRpmBuildCommand(String c) { 285 this.rpmBuildCommand = c; 286 } 287 288 296 public void setFailOnError(boolean value) { 297 failOnError = value; 298 } 299 300 307 public void setQuiet(boolean value) { 308 quiet = value; 309 } 310 311 320 protected String guessRpmBuildCommand() { 321 Vector env = Execute.getProcEnvironment(); 322 String path = null; 323 for (Enumeration e = env.elements(); e.hasMoreElements();) { 324 String var = (String ) e.nextElement(); 325 if (var.startsWith(PATH1) || var.startsWith(PATH2) || var.startsWith(PATH3)) { 326 path = var.substring(PATH_LEN); 327 break; 328 } 329 } 330 331 if (path != null) { 332 Path p = new Path(getProject(), path); 333 String [] pElements = p.list(); 334 for (int i = 0; i < pElements.length; i++) { 335 File f = new File (pElements[i], 336 "rpmbuild" 337 + (Os.isFamily("dos") ? ".exe" : "")); 338 if (f.canRead()) { 339 return f.getAbsolutePath(); 340 } 341 } 342 } 343 344 return "rpm"; 345 } 346 347 354 protected Execute getExecute(Commandline toExecute, 355 ExecuteStreamHandler streamhandler) { 356 Execute exe = new Execute(streamhandler, null); 357 358 exe.setAntRun(getProject()); 359 if (topDir == null) { 360 topDir = getProject().getBaseDir(); 361 } 362 exe.setWorkingDirectory(topDir); 363 364 exe.setCommandline(toExecute.getCommandline()); 365 return exe; 366 } 367 } 368 | Popular Tags |