2 # Copyright 1999-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/catalyst/catalyst,v 1.135 2006/01/29 08:32:05 wolf31o2 Exp $
6 # Maintained in full by:
7 # Eric Edgar <rocket@gentoo.org>
8 # Chris Gianelloni <wolf31o2@gentoo.org>
10 import os,sys,imp,string,getopt
13 __maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
14 __version__="2.0_rc27"
19 print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
20 print " -a --clear-autoresume clear autoresume flags"
21 print " -c --config use specified configuration file"
22 print " -C --cli catalyst commandline (MUST BE LAST OPTION)"
23 print " -d --debug enable debugging"
24 print " -f --file read specfile"
25 print " -F --fetchonly fetch files only"
26 print " -h --help print this help message"
27 print " -p --purge clear tmp dirs,package cache and autoresume flags"
28 print " -s --snapshot generate a Portage snapshot"
29 print " -V --version display version information"
30 print " -v --verbose verbose output"
32 print "Usage examples:"
34 print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
35 print "catalyst -C target=snapshot version_stamp=my_date"
37 print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
38 print "catalyst -s 20051208"
40 print "Using the specfile option (-f, --file) to build a stage target:"
41 print "catalyst -f stage1-specfile.spec"
44 print "Gentoo Catalyst, version "+__version__
45 print "Copyright 2003-2005 The Gentoo Foundation"
46 print "Distributed under the GNU General Public License version 2\n"
49 def parse_config(myconfig):
50 # search a couple of different areas for the main config file
54 confdefaults={ "storedir":"/var/tmp/catalyst",\
55 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
56 "portdir":"/usr/portage","options":"",\
57 "snapshot_cache":"/var/tmp/catalyst/snapshot_cache",\
58 "hash_function":"crc32"}
60 # first, try the one passed (presumably from the cmdline)
62 if os.path.exists(myconfig):
63 print "Using command line specified Catalyst configuration file, "+myconfig
67 print "!!! catalyst: Could not use specified configuration file "+\
71 # next, try the default location
72 elif os.path.exists("/etc/catalyst/catalyst.conf"):
73 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
74 config_file="/etc/catalyst/catalyst.conf"
76 # can't find a config file (we are screwed), so bail out
78 print "!!! catalyst: Could not find a suitable configuration file"
81 # now, try and parse the config file "config_file"
83 execfile(config_file, myconf, myconf)
86 print "!!! catalyst: Unable to parse configuration file, "+myconfig
89 # now, load up the values into conf_values so that we can use them
90 for x in confdefaults.keys():
92 print "Setting",x,"to config file value \""+myconf[x]+"\""
93 conf_values[x]=myconf[x]
95 print "Setting",x,"to default value \""+confdefaults[x]+"\""
96 conf_values[x]=confdefaults[x]
98 # parse out the rest of the options from the config file
99 if "ccache" in string.split(conf_values["options"]):
100 print "Compiler cache support enabled."
101 conf_values["CCACHE"]="1"
103 if "pkgcache" in string.split(conf_values["options"]):
104 print "Package cache support enabled."
105 conf_values["PKGCACHE"]="1"
107 if "snapcache" in string.split(conf_values["options"]):
108 print "Snapshot cache support enabled."
109 conf_values["SNAPCACHE"]="1"
111 if "seedcache" in string.split(conf_values["options"]):
112 print "Seed cache support enabled."
113 conf_values["SEEDCACHE"]="1"
115 if "kerncache" in string.split(conf_values["options"]):
116 print "Kernel cache support enabled."
117 conf_values["KERNCACHE"]="1"
119 if "distcc" in string.split(conf_values["options"]):
120 print "Distcc support enabled."
121 conf_values["DISTCC"]="1"
123 if "autoresume" in string.split(conf_values["options"]):
124 print "Autoresuming support enabled."
125 conf_values["AUTORESUME"]="1"
127 if "purge" in string.split(conf_values["options"]):
128 print "Purge support enabled."
129 conf_values["PURGE"]="1"
131 if "clear-autoresume" in string.split(conf_values["options"]):
132 print "Cleaning autoresume flags support enabled."
133 conf_values["CLEAR_AUTORESUME"]="1"
135 if myconf.has_key("envscript"):
136 print "Envscript support enabled."
137 conf_values["ENVSCRIPT"]=myconf["envscript"]
139 if myconf.has_key("digests"):
140 conf_values["digests"]=myconf["digests"]
142 def import_modules():
143 # import catalyst's own modules (i.e. catalyst_support and the arch modules)
147 for x in required_build_targets:
149 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
150 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
154 raise CatalystError,"Can't find "+x+".py plugin in "+\
155 conf_values.settings["sharedir"]+"/modules/"
157 for x in valid_build_targets:
159 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
160 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
161 module.register(targetmap)
165 raise CatalystError,"Can't find "+x+".py plugin in "+\
166 conf_values.settings["sharedir"]+"/modules/"
169 print "!!! catalyst: Python modules not found in "+\
170 conf_values["sharedir"]+"/modules; exiting."
175 def do_spec(myspecfile):
177 addlargs=read_spec(myspecfile)
185 return arg_parse(cmdline)
187 except CatalystError:
188 print "!!! catalyst: Could not parse commandline, exiting."
191 def build_target(addlargs, targetmap):
193 if not targetmap.has_key(addlargs["target"]):
194 raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
196 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
203 if __name__ == "__main__":
208 # catalyst cannot be run as a normal user due to chroots, mounts, etc
209 print "!!! catalyst: This script requires root privileges to operate"
212 # we need some options in order to work correctly
213 if len(sys.argv) < 2:
217 # parse out the command line arguments
219 opts,args = getopt.getopt(sys.argv[1:], "apxhvdc:C:f:FVs:", ["purge","help", "version", "debug",\
220 "clear_autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
222 except getopt.GetoptError:
226 # defaults for commandline opts
235 # check preconditions
237 print "!!! catalyst: please specify one of either -f or -C\n"
242 if o in ("-h", "--help"):
246 if o in ("-V", "--version"):
247 print "Catalyst version "+__version__
250 if o in ("-d", "--debug"):
251 if len(sys.argv) < 3:
252 print "!!! catalyst: please specify one of either -f or -C\n"
256 conf_values["DEBUG"]="1"
258 if o in ("-c", "--config"):
259 if len(sys.argv) < 3:
260 print "!!! catalyst: please specify one of either -f or -C\n"
266 if o in ("-C", "--cli"):
268 x=sys.argv.index(o)+1
269 while x < len(sys.argv):
270 mycmdline.append(sys.argv[x])
273 if o in ("-f", "--file"):
277 if o in ("-F", "--fetchonly"):
278 if len(sys.argv) < 3:
279 print "!!! catalyst: please specify one of either -f or -C\n"
283 conf_values["FETCH"]="1"
285 if o in ("-v", "--verbose"):
286 if len(sys.argv) < 3:
287 print "!!! catalyst: please specify one of either -f or -C\n"
291 conf_values["VERBOSE"]="1"
293 if o in ("-s", "--snapshot"):
294 if len(sys.argv) < 3:
295 print "!!! catalyst: missing snapshot identifier\n"
300 mycmdline.append("target=snapshot")
301 mycmdline.append("version_stamp="+a)
303 if o in ("-p", "--purge"):
304 if len(sys.argv) < 3:
305 print "!!! catalyst: please specify one of either -f or -C\n"
309 conf_values["PURGE"]="1"
310 if o in ("-a", "--clear-autoresume"):
311 if len(sys.argv) < 3:
312 print "!!! catalyst: please specify one of either -f or -C\n"
316 conf_values["CLEAR_AUTORESUME"]="1"
318 print "!!! catalyst: please specify one of either -f or -C\n"
322 # import configuration file and import our main module using those settings
323 parse_config(myconfig)
324 sys.path.append(conf_values["sharedir"]+"/modules")
325 from catalyst_support import *
327 # Start checking that digests are valid now that the hash_map was imported from catalyst_support
328 if conf_values.has_key("digests"):
329 for i in conf_values["digests"].split():
330 if not hash_map.has_key(i):
332 print i+" is not a valid digest entry"
333 print "Valid digest entries:"
334 print hash_map.keys()
336 print "Catalyst aborting...."
338 if find_binary(hash_map[i][1]) == None:
341 print "\tThe "+hash_map[i][1]+\
342 " binary was not found. It needs to be in your system path"
344 print "Catalyst aborting...."
346 if conf_values.has_key("hash_function"):
347 if not hash_map.has_key(conf_values["hash_function"]):
349 print conf_values["hash_function"]+" is not a valid hash_function entry"
350 print "Valid hash_function entries:"
351 print hash_map.keys()
353 print "Catalyst aborting...."
355 if find_binary(hash_map[conf_values["hash_function"]][1]) == None:
357 print "hash_function="+conf_values["hash_function"]
358 print "\tThe "+hash_map[conf_values["hash_function"]][1]+\
359 " binary was not found. It needs to be in your system path"
361 print "Catalyst aborting...."
364 # import the rest of the catalyst modules
365 targetmap=import_modules()
370 addlargs.update(do_spec(myspecfile))
373 addlargs.update(do_cli(mycmdline))
375 if not addlargs.has_key("target"):
376 raise CatalystError, "Required value \"target\" not specified."
378 # everything is setup, so the build is a go
380 build_target(addlargs, targetmap)
382 except CatalystError:
384 print "Catalyst aborting...."
386 except KeyboardInterrupt:
387 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
389 print "Catalyst aborting...."
392 print "Catalyst aborting...."
395 print "Catalyst aborting...."
399 #except CatalystError:
401 # print "Catalyst aborting...."
404 # print "\nproblem with command line or spec file ( Key Error )"
405 # print "Key: "+str(sys.exc_value)+" was not found"
406 # print "Catalyst aborting...."
408 #except UnboundLocalError:
410 # print "UnboundLocalError: "+str(sys.exc_value)+" was not found"
413 # print "Catalyst aborting...."