Added README file
[metux-java.git] / util / Decompressor.java
blobdba6836f5f9b8e07c5897f4fc4d386d059ad0800
2 package org.de.metux.util;
4 import java.io.*;
5 import java.net.*;
8 public class Decompressor
10 public boolean decompress(String archive, String target)
12 return decompress(archive,target,null);
15 public boolean decompress(String archive, String target, String format)
17 if ((format==null) || (format.length()==0))
19 /* try to detect format */
24 String buildroot = config.cf_get_str_n("@@buildroot");
25 String archiver = config.cf_get_str("archiver");
26 if (archiver.length()==0)
28 notice("no archiver given ... trying to detect" );
29 if (source.endsWith(".tar.gz") ||
30 source.endsWith(".tgz"))
31 config.cf_set("archiver", archiver = "tar.gz");
32 else if (source.endsWith(".tar.bz2") ||
33 source.endsWith(".tbz") ||
34 source.endsWith(".tbz2"))
35 config.cf_set("archiver", archiver = "tar.bz");
36 else
37 return error("cannot detect proper archiver for \""+source+"\"");
40 if (!mkdir(buildroot))
41 return false;
43 if (archiver.equals("tar.bz2"))
45 notice("decompressing tar.bz2: "+source+" ..." );
46 return exec ( "cd "+buildroot+" && tar -xjf "+source );
49 if (archiver.equals("tar.gz"))
51 notice("decompressing tar.gz: "+source+" ..." );
52 return exec ( "cd "+buildroot+" && tar -xzf "+source );
55 return error("unsupported archiver: "+archiver );
58 /* pre-execute something *before* we start decompressing
59 ie. for preparing patch sets */
61 boolean preexec()
63 /* prepare patches */
64 String preexec=config.cf_get_str_n("prebuild-patch-preexec");
65 if (preexec==null)
66 return true;
68 notice("Calling preexec: "+preexec);
69 if (!exec(preexec))
70 return error("preexec failed");
72 return true;
75 boolean cleanup()
77 String buildroot = config.cf_get_str_n("@@buildroot");
78 notice("cleaning up "+buildroot );
79 rm.remove_recursive(buildroot);
80 mkdir(buildroot);
81 return true;
84 boolean paranoia_check()
86 /* check for proper buildroot variable ... probably not really necessary */
87 String buildroot = config.cf_get_str("@@buildroot");
88 try
90 buildroot = new File(buildroot).getCanonicalPath();
92 catch (IOException e)
94 return error("exception: "+e);
97 if (buildroot.length()<5)
98 return error("corrupt @@buildroot variable - internal problem ?");
100 /* -- get the source file name / uri -- */
101 String source=config.cf_get_str_n("source-file");
102 if (source==null)
103 return error("missing package source option in configuration");
105 return true;
108 boolean decompress()
110 /* now run decompression */
111 String srclist[] = config.cf_get_list("source-file");
112 if (srclist.length==0)
113 return error("missing package source option in configuration");
115 for (int x=0; x<srclist.length; x++)
116 if (!decompress_archive(srclist[x]))
117 return false;
119 return true;
122 boolean prepare_sourcetree()
124 return
125 ( /* hope we've got shortcut evaluation! */
126 paranoia_check() &&
127 preexec() &&
128 cleanup() &&
129 decompress() &&
130 lookup_srcdir() &&
131 apply_patches()
135 /* right after decompression, we need to know our actual srcroot */
136 boolean lookup_srcdir()
138 String buildroot = config.cf_get_str_n("@@buildroot");
139 if (buildroot==null)
140 return error("lookup_srcdir: Internal error: @@buildroot not defined");
142 File handle = new File(buildroot);
143 String sublist[] = handle.list();
145 if (sublist==null)
146 return error("could not get directory listing!");
148 if (sublist.length==0)
149 return error("no srcdir found. seems like decompression failed");
151 if (sublist.length>1)
152 return error("more than one srcdir found. looks like trouble");
154 notice("srcdir is: "+sublist[0]);
156 String srcroot = buildroot+"/"+sublist[0];
157 notice("srcroot is: "+srcroot);
159 config.cf_set("@@srcroot", srcroot);
161 return true;
164 boolean apply_patches()
166 String patches[] = config.cf_get_list("prebuild-patch-file");
167 String patch_opts = config.cf_get_str("prebuild-patch-opts");
168 String srcroot = config.cf_get_str_n("@@srcroot");
170 if (patches.length==0)
171 return true;
173 for (int x=0; x<patches.length; x++)
175 if (!(new File(patches[x]).exists()))
176 return error("could not load patch file: "+patches[x]);
178 notice("applying patch: "+patches[x]+"("+patch_opts+")" );
179 if (!exec("cd "+srcroot+";pwd;patch "+patch_opts+"<"+patches[x]))
180 return false;
183 return true;