3 # Maintained in full by:
4 # Eric Edgar <rocket@gentoo.org>
5 # Chris Gianelloni <wolf31o2@gentoo.org>
7 import os,sys,imp,string,getopt
10 __maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
11 __version__="2.0.5_pre3"
16 print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
17 print " -a --clear-autoresume clear autoresume flags"
18 print " -c --config use specified configuration file"
19 print " -C --cli catalyst commandline (MUST BE LAST OPTION)"
20 print " -d --debug enable debugging"
21 print " -f --file read specfile"
22 print " -F --fetchonly fetch files only"
23 print " -h --help print this help message"
24 print " -p --purge clear tmp dirs,package cache and autoresume flags"
25 print " -s --snapshot generate a Portage snapshot"
26 print " -V --version display version information"
27 print " -v --verbose verbose output"
29 print "Usage examples:"
31 print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
32 print "catalyst -C target=snapshot version_stamp=my_date"
34 print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
35 print "catalyst -s 20051208"
37 print "Using the specfile option (-f, --file) to build a stage target:"
38 print "catalyst -f stage1-specfile.spec"
41 print "Gentoo Catalyst, version "+__version__
42 print "Copyright 2003-2006 The Gentoo Foundation"
43 print "Distributed under the GNU General Public License version 2\n"
45 def parse_config(myconfig):
46 # search a couple of different areas for the main config file
50 confdefaults={ "storedir":"/var/tmp/catalyst",\
51 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
52 "portdir":"/usr/portage","options":"",\
53 "snapshot_cache":"/var/tmp/catalyst/snapshot_cache",\
54 "hash_function":"crc32"}
56 # first, try the one passed (presumably from the cmdline)
58 if os.path.exists(myconfig):
59 print "Using command line specified Catalyst configuration file, "+myconfig
63 print "!!! catalyst: Could not use specified configuration file "+\
67 # next, try the default location
68 elif os.path.exists("/etc/catalyst/catalyst.conf"):
69 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
70 config_file="/etc/catalyst/catalyst.conf"
72 # can't find a config file (we are screwed), so bail out
74 print "!!! catalyst: Could not find a suitable configuration file"
77 # now, try and parse the config file "config_file"
79 execfile(config_file, myconf, myconf)
82 print "!!! catalyst: Unable to parse configuration file, "+myconfig
85 # now, load up the values into conf_values so that we can use them
86 for x in confdefaults.keys():
88 print "Setting",x,"to config file value \""+myconf[x]+"\""
89 conf_values[x]=myconf[x]
91 print "Setting",x,"to default value \""+confdefaults[x]+"\""
92 conf_values[x]=confdefaults[x]
94 # parse out the rest of the options from the config file
95 if "autoresume" in string.split(conf_values["options"]):
96 print "Autoresuming support enabled."
97 conf_values["AUTORESUME"]="1"
99 if "ccache" in string.split(conf_values["options"]):
100 print "Compiler cache support enabled."
101 conf_values["CCACHE"]="1"
103 if "clear-autoresume" in string.split(conf_values["options"]):
104 print "Cleaning autoresume flags support enabled."
105 conf_values["CLEAR_AUTORESUME"]="1"
107 # if "compress" in string.split(conf_values["options"]):
108 # print "Compression enabled."
109 # conf_values["COMPRESS"]="1"
111 if "distcc" in string.split(conf_values["options"]):
112 print "Distcc support enabled."
113 conf_values["DISTCC"]="1"
115 if "kerncache" in string.split(conf_values["options"]):
116 print "Kernel cache support enabled."
117 conf_values["KERNCACHE"]="1"
119 if "pkgcache" in string.split(conf_values["options"]):
120 print "Package cache support enabled."
121 conf_values["PKGCACHE"]="1"
123 if "purge" in string.split(conf_values["options"]):
124 print "Purge support enabled."
125 conf_values["PURGE"]="1"
127 if "seedcache" in string.split(conf_values["options"]):
128 print "Seed cache support enabled."
129 conf_values["SEEDCACHE"]="1"
131 if "snapcache" in string.split(conf_values["options"]):
132 print "Snapshot cache support enabled."
133 conf_values["SNAPCACHE"]="1"
135 # if "tarball" in string.split(conf_values["options"]):
136 # print "Tarball creation enabled."
137 # conf_values["TARBALL"]="1"
139 if myconf.has_key("digests"):
140 conf_values["digests"]=myconf["digests"]
142 if myconf.has_key("envscript"):
143 print "Envscript support enabled."
144 conf_values["ENVSCRIPT"]=myconf["envscript"]
146 def import_modules():
147 # import catalyst's own modules (i.e. catalyst_support and the arch modules)
151 for x in required_build_targets:
153 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
154 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
158 raise CatalystError,"Can't find "+x+".py plugin in "+\
159 conf_values.settings["sharedir"]+"/modules/"
161 for x in valid_build_targets:
163 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
164 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
165 module.register(targetmap)
169 raise CatalystError,"Can't find "+x+".py plugin in "+\
170 conf_values.settings["sharedir"]+"/modules/"
173 print "!!! catalyst: Python modules not found in "+\
174 conf_values["sharedir"]+"/modules; exiting."
179 def do_spec(myspecfile):
181 addlargs=read_spec(myspecfile)
189 return arg_parse(cmdline)
191 except CatalystError:
192 print "!!! catalyst: Could not parse commandline, exiting."
195 def build_target(addlargs, targetmap):
197 if not targetmap.has_key(addlargs["target"]):
198 raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
200 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
205 # TODO: Capture traceback, so we can display this error after printing of the traceback
206 print "!!! catalyst: Error encountered during run of target " + addlargs["target"]
209 if __name__ == "__main__":
214 # catalyst cannot be run as a normal user due to chroots, mounts, etc
215 print "!!! catalyst: This script requires root privileges to operate"
218 # we need some options in order to work correctly
219 if len(sys.argv) < 2:
223 # parse out the command line arguments
225 opts,args = getopt.getopt(sys.argv[1:], "aphvdc:C:f:FVs:", ["purge","help", "version", "debug",\
226 "clear-autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
228 except getopt.GetoptError:
232 # defaults for commandline opts
241 # check preconditions
243 print "!!! catalyst: please specify one of either -f or -C\n"
248 if o in ("-h", "--help"):
252 if o in ("-V", "--version"):
253 print "Catalyst version "+__version__
256 if o in ("-d", "--debug"):
257 if len(sys.argv) < 3:
258 print "!!! catalyst: please specify one of either -f or -C\n"
262 conf_values["DEBUG"]="1"
264 if o in ("-c", "--config"):
265 if len(sys.argv) < 3:
266 print "!!! catalyst: please specify one of either -f or -C\n"
272 if o in ("-C", "--cli"):
274 x=sys.argv.index(o)+1
275 while x < len(sys.argv):
276 mycmdline.append(sys.argv[x])
279 if o in ("-f", "--file"):
283 if o in ("-F", "--fetchonly"):
284 if len(sys.argv) < 3:
285 print "!!! catalyst: please specify one of either -f or -C\n"
289 conf_values["FETCH"]="1"
291 if o in ("-v", "--verbose"):
292 if len(sys.argv) < 3:
293 print "!!! catalyst: please specify one of either -f or -C\n"
297 conf_values["VERBOSE"]="1"
299 if o in ("-s", "--snapshot"):
300 if len(sys.argv) < 3:
301 print "!!! catalyst: missing snapshot identifier\n"
306 mycmdline.append("target=snapshot")
307 mycmdline.append("version_stamp="+a)
309 if o in ("-p", "--purge"):
310 if len(sys.argv) < 3:
311 print "!!! catalyst: please specify one of either -f or -C\n"
315 conf_values["PURGE"]="1"
316 if o in ("-a", "--clear-autoresume"):
317 if len(sys.argv) < 3:
318 print "!!! catalyst: please specify one of either -f or -C\n"
322 conf_values["CLEAR_AUTORESUME"]="1"
324 print "!!! catalyst: please specify one of either -f or -C\n"
328 # import configuration file and import our main module using those settings
329 parse_config(myconfig)
330 sys.path.append(conf_values["sharedir"]+"/modules")
331 from catalyst_support import *
333 # Start checking that digests are valid now that the hash_map was imported from catalyst_support
334 if conf_values.has_key("digests"):
335 for i in conf_values["digests"].split():
336 if not hash_map.has_key(i):
338 print i+" is not a valid digest entry"
339 print "Valid digest entries:"
340 print hash_map.keys()
342 print "Catalyst aborting...."
344 if find_binary(hash_map[i][1]) == None:
347 print "\tThe "+hash_map[i][1]+\
348 " binary was not found. It needs to be in your system path"
350 print "Catalyst aborting...."
352 if conf_values.has_key("hash_function"):
353 if not hash_map.has_key(conf_values["hash_function"]):
355 print conf_values["hash_function"]+" is not a valid hash_function entry"
356 print "Valid hash_function entries:"
357 print hash_map.keys()
359 print "Catalyst aborting...."
361 if find_binary(hash_map[conf_values["hash_function"]][1]) == None:
363 print "hash_function="+conf_values["hash_function"]
364 print "\tThe "+hash_map[conf_values["hash_function"]][1]+\
365 " binary was not found. It needs to be in your system path"
367 print "Catalyst aborting...."
370 # import the rest of the catalyst modules
371 targetmap=import_modules()
376 addlargs.update(do_spec(myspecfile))
379 addlargs.update(do_cli(mycmdline))
381 if not addlargs.has_key("target"):
382 raise CatalystError, "Required value \"target\" not specified."
384 # everything is setup, so the build is a go
386 build_target(addlargs, targetmap)
388 except CatalystError:
390 print "Catalyst aborting...."
392 except KeyboardInterrupt:
393 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
395 print "Catalyst aborting...."
398 print "Catalyst aborting...."
401 print "Catalyst aborting...."
405 #except CatalystError:
407 # print "Catalyst aborting...."
410 # print "\nproblem with command line or spec file ( Key Error )"
411 # print "Key: "+str(sys.exc_value)+" was not found"
412 # print "Catalyst aborting...."
414 #except UnboundLocalError:
416 # print "UnboundLocalError: "+str(sys.exc_value)+" was not found"
419 # print "Catalyst aborting...."