Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 264   Methods: 9
NCLOC: 123   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Rpm.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional;
 55   
 
 56   
 import java.io.BufferedOutputStream;
 57   
 import java.io.File;
 58   
 import java.io.FileOutputStream;
 59   
 import java.io.IOException;
 60   
 import java.io.OutputStream;
 61   
 import java.io.PrintStream;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.Project;
 64   
 import org.apache.tools.ant.Task;
 65   
 import org.apache.tools.ant.taskdefs.Execute;
 66   
 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
 67   
 import org.apache.tools.ant.taskdefs.LogOutputStream;
 68   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 69   
 import org.apache.tools.ant.taskdefs.PumpStreamHandler;
 70   
 import org.apache.tools.ant.types.Commandline;
 71   
 
 72   
 /**
 73   
  * Invokes the rpm tool to build a Linux installation file.
 74   
  *  @author lucas@collab.net
 75   
  */
 76   
 public class Rpm extends Task {
 77   
     
 78   
     /**
 79   
      * the spec file
 80   
      */
 81   
     private String specFile;
 82   
 
 83   
     /**
 84   
      * the rpm top dir
 85   
      */
 86   
     private File topDir;
 87   
 
 88   
     /**
 89   
      * the rpm command to use
 90   
      */
 91   
     private String command = "-bb";
 92   
 
 93   
     /**
 94   
      * clean BUILD directory
 95   
      */
 96   
     private boolean cleanBuildDir = false;
 97   
 
 98   
     /**
 99   
      * remove spec file
 100   
      */
 101   
     private boolean removeSpec = false;
 102   
 
 103   
     /**
 104   
      * remove sources
 105   
      */
 106   
     private boolean removeSource = false;
 107   
 
 108   
     /**
 109   
      * the file to direct standard output from the command
 110   
      */
 111   
     private File output;
 112   
 
 113   
     /**
 114   
      * the file to direct standard error from the command
 115   
      */
 116   
     private File error;
 117   
 
 118  0
     public void execute() throws BuildException {
 119   
         
 120  0
         Commandline toExecute = new Commandline();
 121   
 
 122  0
         toExecute.setExecutable("rpm");
 123  0
         if (topDir != null) {
 124  0
             toExecute.createArgument().setValue("--define");
 125  0
             toExecute.createArgument().setValue("_topdir" + topDir);
 126   
         }
 127   
 
 128  0
         toExecute.createArgument().setLine(command);
 129   
 
 130  0
         if (cleanBuildDir) {
 131  0
             toExecute.createArgument().setValue("--clean");
 132   
         }
 133  0
         if (removeSpec) {
 134  0
             toExecute.createArgument().setValue("--rmspec");
 135   
         }
 136  0
         if (removeSource) {
 137  0
             toExecute.createArgument().setValue("--rmsource");
 138   
         }
 139   
 
 140  0
         toExecute.createArgument().setValue("SPECS/" + specFile);
 141   
 
 142  0
         ExecuteStreamHandler streamhandler = null;
 143  0
         OutputStream outputstream = null;
 144  0
         OutputStream errorstream = null;
 145  0
         if (error == null && output == null) {
 146  0
             streamhandler = new LogStreamHandler(this, Project.MSG_INFO,
 147   
                                                  Project.MSG_WARN);
 148   
         } else {
 149  0
             if (output != null) {
 150  0
                 try {
 151  0
                     outputstream = new PrintStream(new BufferedOutputStream(new FileOutputStream(output)));
 152   
                 } catch (IOException e) {
 153  0
                     throw new BuildException(e, getLocation());
 154   
                 }
 155   
             } else {
 156  0
                 outputstream = new LogOutputStream(this, Project.MSG_INFO);
 157   
             }
 158  0
             if (error != null) {
 159  0
                 try {
 160  0
                     errorstream = new PrintStream(new BufferedOutputStream(new FileOutputStream(error)));
 161   
                 }  catch (IOException e) {
 162  0
                     throw new BuildException(e, getLocation());
 163   
                 }
 164   
             } else {
 165  0
                 errorstream = new LogOutputStream(this, Project.MSG_WARN);
 166   
             }
 167  0
             streamhandler = new PumpStreamHandler(outputstream, errorstream);
 168   
         }
 169   
 
 170  0
         Execute exe = new Execute(streamhandler, null);
 171   
 
 172  0
         exe.setAntRun(getProject());
 173  0
         if (topDir == null) {
 174  0
             topDir = getProject().getBaseDir();
 175   
         }
 176  0
         exe.setWorkingDirectory(topDir);
 177   
 
 178  0
         exe.setCommandline(toExecute.getCommandline());
 179  0
         try {
 180  0
             exe.execute();
 181  0
             log("Building the RPM based on the " + specFile + " file");
 182   
         } catch (IOException e) {
 183  0
             throw new BuildException(e, getLocation());
 184   
         } finally {
 185  0
             if (output != null) {
 186  0
                 try {
 187  0
                     outputstream.close();
 188   
                 } catch (IOException e) {}
 189   
             }
 190  0
             if (error != null) {
 191  0
                 try {
 192  0
                     errorstream.close();
 193   
                 } catch (IOException e) {}
 194   
             }
 195   
         }
 196   
     }
 197   
 
 198   
     /**
 199   
      * The directory which will have the expected
 200   
      * subdirectories, SPECS, SOURCES, BUILD, SRPMS ; optional. 
 201   
      * If this isn't specified,
 202   
      * the <tt>baseDir</tt> value is used
 203   
      */
 204  0
     public void setTopDir(File td) {
 205  0
         this.topDir = td;
 206   
     }
 207   
 
 208   
     /**
 209   
      * What command to issue to the rpm tool; optional.
 210   
      * The default is "-bb"
 211   
      */
 212  0
     public void setCommand(String c) {
 213  0
         this.command = c;
 214   
     }
 215   
 
 216   
     /**
 217   
      * The name of the spec File to use; required.
 218   
      */
 219  0
     public void setSpecFile(String sf) {
 220  0
         if ((sf == null) || (sf.trim().equals(""))) {
 221  0
             throw new BuildException("You must specify a spec file", getLocation());
 222   
         }
 223  0
         this.specFile = sf;
 224   
     }
 225   
 
 226   
     /**
 227   
      * Flag (optional, default=false) to remove 
 228   
      * the generated files in the BUILD directory
 229   
      */
 230  0
     public void setCleanBuildDir(boolean cbd) {
 231  0
         cleanBuildDir = cbd;
 232   
     }
 233   
 
 234   
     /**
 235   
      * Flag (optional, default=false) to remove the spec file from SPECS
 236   
      */
 237  0
     public void setRemoveSpec(boolean rs) {
 238  0
         removeSpec = rs;
 239   
     }
 240   
 
 241   
     /**
 242   
      * Flag (optional, default=false) 
 243   
      * to remove the sources after the build.
 244   
      * See the the <tt>--rmsource</tt>  option of rpmbuild.
 245   
      */
 246  0
     public void setRemoveSource(boolean rs) {
 247  0
         removeSource = rs;
 248   
     }
 249   
 
 250   
     /**
 251   
      * Optional file to save stdout to.
 252   
      */
 253  0
     public void setOutput(File output) {
 254  0
         this.output = output;
 255   
     }
 256   
 
 257   
     /**
 258   
      * Optional file to save stderr to
 259   
      */
 260  0
     public void setError(File error) {
 261  0
         this.error = error;
 262   
     }
 263   
 }
 264