View Javadoc

1    /*
2     * This is free software; you can redistribute it and/or modify it
3     * under the terms of the GNU Lesser General Public License as
4     * published by the Free Software Foundation; either version 2.1 of
5     * the License, or (at your option) any later version.
6     *
7     * This software is distributed in the hope that it will be useful,
8     * but WITHOUT ANY WARRANTY; without even the implied warranty of
9     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10    * Lesser General Public License for more details.
11    *
12    * You should have received a copy of the GNU Lesser General Public
13    * License along with this software; if not, write to the Free
14    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
15    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
16    */
17  
18  package installtoolkit.deb;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Task;
27  import org.apache.tools.ant.taskdefs.Execute;
28  import org.apache.tools.ant.taskdefs.PumpStreamHandler;
29  import org.apache.tools.ant.types.Commandline;
30  
31  /**
32   * Task to execute a list of debhelper scripts and additional commands 
33   * in an fakeroot environment.
34   * 
35   * @author Christian Elberfeld <elberfeld@web.de>
36   *
37   */
38  public class DebhelperTask extends Task {
39  
40  	protected boolean debug = false;
41  	protected File destdir;
42  	protected File srcdir;
43  	protected String architecture = null;
44  	protected String buildprog = "dh_builddeb";
45  	protected List<DebhelperType> debhelper = new ArrayList<DebhelperType>();	
46  	protected List<CommandType> commands = new ArrayList<CommandType>();	
47  	
48  	/**
49  	 * @see Task#execute()
50  	 */
51  	@Override
52  	public void execute() throws BuildException {
53  
54  		try {
55  			
56  			if (this.destdir == null) throw new BuildException("Attribute \"builddir\" must be set.");
57  			if (this.srcdir == null) throw new BuildException("Attribute \"srcdir\" must be set.");
58  
59  			if (this.architecture == "all") this.architecture = null;
60  			if (this.architecture != null) handleOutput("Target Debian Architecture: "+this.architecture);
61  			
62  			Commandline cmdl1 = new Commandline();
63  			
64  			cmdl1.setExecutable("fakeroot");						
65  			cmdl1.createArgument().setValue("-s");
66  			cmdl1.createArgument().setValue("fakeroot.save");
67  			cmdl1.createArgument().setValue("--");
68  			cmdl1.createArgument().setValue("echo");
69  			cmdl1.createArgument().setValue("");
70  			
71  			Execute execute1 = new Execute();
72  			execute1.setWorkingDirectory(this.srcdir);
73  			execute1.setCommandline(cmdl1.getCommandline());
74  			execute1.setStreamHandler(new PumpStreamHandler());
75  			
76  			handleOutput("Running fakeroot to initialize the fakeroot environment.");
77  			int ret1 = execute1.execute();
78  
79  			if (debug) handleOutput("Return vaue: "+ret1);
80  			if (Execute.isFailure(ret1)) throw new BuildException("Execution of fakeroot failed");
81  			
82  			if (debug) handleOutput("running "+this.debhelper.size()+" debhelper scripts");
83  
84  			for (DebhelperType dh : this.debhelper) {
85  			
86  				Commandline cmdl2 = new Commandline();
87  				
88  				cmdl2.setExecutable("fakeroot");							
89  				cmdl2.createArgument().setValue("-i");
90  				cmdl2.createArgument().setValue("fakeroot.save");
91  				cmdl2.createArgument().setValue("-s");
92  				cmdl2.createArgument().setValue("fakeroot.save");
93  				cmdl2.createArgument().setValue("--");
94  				cmdl2.createArgument().setValue("dpkg-architecture");
95  				if (this.architecture != null) cmdl2.createArgument().setValue("-a"+this.architecture);
96  				cmdl2.createArgument().setValue("-c");
97  				cmdl2.createArgument().setValue(dh.getName());
98  				if (debug) cmdl2.createArgument().setValue("-v");
99  				cmdl2.createArgument().setValue("-Pdebian/tmp");
100 				
101 				Execute execute2 = new Execute();
102 				execute2.setWorkingDirectory(this.srcdir);
103 				execute2.setCommandline(cmdl2.getCommandline());
104 				execute2.setStreamHandler(new PumpStreamHandler());
105 				
106 				handleOutput("Running debhelper Script "+dh.getName()+" in fakeroot environment.");
107 				int ret2 = execute2.execute();
108 
109 				if (debug) handleOutput("Return vaue: "+ret2);
110 				if (Execute.isFailure(ret2)) {
111 					
112 					
113 					throw new BuildException("Execution of "+dh.getName()+" failed");
114 				}
115 								
116 			} // for
117 			
118 			if (debug) handleOutput("running "+this.debhelper.size()+" debhelper scripts finished");
119 
120 			if (debug) handleOutput("running "+this.commands.size()+" additional commands");
121 
122 			for (CommandType command : this.commands) {
123 			
124 				Commandline cmdl2 = new Commandline();
125 				
126 				cmdl2.setExecutable("fakeroot");							
127 				cmdl2.createArgument().setValue("-i");
128 				cmdl2.createArgument().setValue("fakeroot.save");
129 				cmdl2.createArgument().setValue("-s");
130 				cmdl2.createArgument().setValue("fakeroot.save");
131 				cmdl2.createArgument().setValue("--");
132 				cmdl2.createArgument().setLine(command.getLine());
133 				
134 				Execute execute2 = new Execute();
135 				execute2.setWorkingDirectory(this.srcdir);
136 				execute2.setCommandline(cmdl2.getCommandline());
137 				execute2.setStreamHandler(new PumpStreamHandler());
138 				
139 				handleOutput("Running command ["+command.getLine()+"] in fakeroot environment.");
140 				int ret2 = execute2.execute();
141 
142 				if (debug) handleOutput("Return vaue: "+ret2);
143 				if (Execute.isFailure(ret2)) {
144 					
145 					
146 					throw new BuildException("Execution of ["+command.getLine()+"] failed");
147 				}
148 								
149 			} // for
150 			
151 			if (debug) handleOutput("running "+this.commands.size()+" additional commands finished");
152 
153 			
154 			Commandline cmdl3 = new Commandline();			
155 			cmdl3.setExecutable("fakeroot");						
156 			cmdl3.createArgument().setValue("-i");
157 			cmdl3.createArgument().setValue("fakeroot.save");
158 			cmdl3.createArgument().setValue("-s");
159 			cmdl3.createArgument().setValue("fakeroot.save");
160 			cmdl3.createArgument().setValue("--");		
161 			cmdl3.createArgument().setValue("dpkg-architecture");
162 			if (this.architecture != null) cmdl3.createArgument().setValue("-a"+this.architecture);
163 			cmdl3.createArgument().setValue("-c");			
164 			cmdl3.createArgument().setValue(this.buildprog);
165 			if (debug) cmdl3.createArgument().setValue("-v");
166 			cmdl3.createArgument().setValue("-Pdebian/tmp");
167 			cmdl3.createArgument().setValue("--destdir="+this.destdir.getAbsolutePath());
168 			
169 			Execute execute3 = new Execute();
170 			execute3.setWorkingDirectory(this.srcdir);
171 			execute3.setCommandline(cmdl3.getCommandline());
172 			execute3.setStreamHandler(new PumpStreamHandler());
173 			
174 			handleOutput("Running debhelper Script "+this.buildprog+" in fakeroot environment.");
175 			int ret3 = execute3.execute();
176 
177 			if (debug) handleOutput("Return vaue: "+ret3);
178 
179 		} catch (IOException e) {
180 			
181 			throw new BuildException(e.getMessage(),e);
182 		}
183 		
184 	}
185 		
186 	/**
187 	 * The debhelper scripts to execute
188 	 * @param dh
189 	 */
190 	public void addDh(DebhelperType dh) {
191 		
192 		this.debhelper.add(dh);
193 	}
194 
195 	/**
196 	 * The additional commands to execute
197 	 * @param cmd
198 	 */
199 	public void addCommand(CommandType cmd) {	
200 		this.commands.add(cmd);
201 	}
202 
203 	/**
204 	 * The directory to place the package
205 	 * @param destdir
206 	 */
207 	public void setDestdir(File destdir) {
208 		this.destdir = destdir;
209 	}
210 
211 	/**
212 	 * The directory to build the package from
213 	 * @param srcdir
214 	 */
215 	public void setSrcdir(File srcdir) {
216 		this.srcdir = srcdir;
217 	}
218 
219 	/**
220 	 * Enable debug output
221 	 * @param debug
222 	 */
223 	public void setDebug(boolean debug) {
224 		this.debug = debug;
225 	}
226 
227 	/**
228 	 * The Debian architecture to build the package for
229 	 * @param architecture
230 	 */
231 	public void setArchitecture(String architecture) {
232 		this.architecture = architecture;
233 	}
234 
235 	
236 	
237 }