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.103 2005/11/30 21:42:19 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
12 __maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
18 print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
19 print " -a --clear-autoresume clear autoresume flags"
20 print " -c --config use specified configuration file"
21 print " -C --cli catalyst commandline (MUST BE LAST OPTION)"
22 print " -d --debug enable debugging"
23 print " -f --file read specfile"
24 print " -F --fetchonly fetch files only"
25 print " -h --help print this help message"
26 print " -p --purge clear tmp dirs,package cache and autoresume flags"
27 print " -s --snapshot generate a Portage snapshot"
28 print " -v --version display version information"
29 print " -V --verbose verbose output"
31 print "Usage examples:"
33 print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
34 print "catalyst -C target=snapshot version_stamp=my_date"
36 print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
37 print "catalyst -s 2005.0"
39 print "Using the specfile option (-f, --file) to build a stage target:"
40 print "catalyst -f stage1-specfile.spec"
43 print "Gentoo Catalyst, version "+__version__
44 print "Copyright 2003-2005 The Gentoo Foundation"
45 print "Distributed under the GNU General Public License version 2\n"
48 def parse_config(myconfig):
49 # search a couple of different areas for the main config file
53 confdefaults={ "storedir":"/var/tmp/catalyst",\
54 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
55 "portdir":"/usr/portage","options":"",\
56 "snapshot_cache":"/var/tmp/catalyst/snapshot_cache" }
58 # first, try the one passed (presumably from the cmdline)
60 if os.path.exists(myconfig):
61 print "Using command line specified Catalyst configuration file, "+myconfig
65 print "!!! catalyst: Could not use specified configuration file "+\
69 # next, try the default location
70 elif os.path.exists("/etc/catalyst/catalyst.conf"):
71 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
72 config_file="/etc/catalyst/catalyst.conf"
74 # can't find a config file (we are screwed), so bail out
76 print "!!! catalyst: Could not find a suitable configuration file"
79 # now, try and parse the config file "config_file"
81 execfile(config_file, myconf, myconf)
84 print "!!! catalyst: Unable to parse configuration file, "+myconfig
87 # now, load up the values into conf_values so that we can use them
88 for x in confdefaults.keys():
90 print "Setting",x,"to config file value \""+myconf[x]+"\""
91 conf_values[x]=myconf[x]
93 print "Setting",x,"to default value \""+confdefaults[x]+"\""
94 conf_values[x]=confdefaults[x]
96 # parse out the rest of the options from the config file
97 if "ccache" in string.split(conf_values["options"]):
98 print "Compiler cache support enabled."
99 conf_values["CCACHE"]="1"
101 if "pkgcache" in string.split(conf_values["options"]):
102 print "Package cache support enabled."
103 conf_values["PKGCACHE"]="1"
105 if "snapcache" in string.split(conf_values["options"]):
106 print "Snapshot cache support enabled."
107 conf_values["SNAPCACHE"]="1"
109 if "seedcache" in string.split(conf_values["options"]):
110 print "Seed cache support enabled."
111 conf_values["SEEDCACHE"]="1"
113 if "kerncache" in string.split(conf_values["options"]):
114 print "Kernel cache support enabled."
115 conf_values["KERNCACHE"]="1"
117 if "distcc" in string.split(conf_values["options"]):
118 print "Distcc support enabled."
119 conf_values["DISTCC"]="1"
121 if "autoresume" in string.split(conf_values["options"]):
122 print "Autoresuming support enabled."
123 conf_values["AUTORESUME"]="1"
125 if "purge" in string.split(conf_values["options"]):
126 print "Purge support enabled."
127 conf_values["PURGE"]="1"
129 if "clear-autoresume" in string.split(conf_values["options"]):
130 print "Cleaning autoresume flags support enabled."
131 conf_values["CLEAR_AUTORESUME"]="1"
133 if myconf.has_key("envscript"):
134 print "Envscript support enabled."
135 conf_values["ENVSCRIPT"]=myconf["envscript"]
137 def import_modules():
138 # import catalyst's own modules (i.e. catalyst_support and the arch modules)
142 for x in required_build_targets:
144 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
145 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
149 raise CatalystError,"Can't find "+x+".py plugin in "+\
150 conf_values.settings["sharedir"]+"/modules/"
152 for x in valid_build_targets:
154 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
155 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
156 module.register(targetmap)
160 raise CatalystError,"Can't find "+x+".py plugin in "+\
161 conf_values.settings["sharedir"]+"/modules/"
164 print "!!! catalyst: Python modules not found in "+\
165 conf_values["sharedir"]+"/modules; exiting."
170 def do_spec(myspecfile):
172 addlargs=read_spec(myspecfile)
180 return arg_parse(cmdline)
182 except CatalystError:
183 print "!!! catalyst: Could not parse commandline, exiting."
186 def build_target(addlargs, targetmap):
188 if not targetmap.has_key(addlargs["target"]):
189 raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
191 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
198 if __name__ == "__main__":
203 # catalyst cannot be run as a normal user due to chroots, mounts, etc
204 print "!!! catalyst: This script requires root privileges to operate"
207 # we need some options in order to work correctly
208 if len(sys.argv) < 2:
212 # parse out the command line arguments
214 opts,args = getopt.getopt(sys.argv[1:], "apxhvdc:C:f:FVs:", ["purge","help", "version", "debug",\
215 "clear_autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
217 except getopt.GetoptError:
221 # defaults for commandline opts
230 # check preconditions
232 print "!!! catalyst: please specify one of either -f or -C\n"
237 if o in ("-h", "--help"):
241 if o in ("-v", "--version"):
242 print "Catalyst version "+__version__
245 if o in ("-d", "--debug"):
246 if len(sys.argv) < 3:
247 print "!!! catalyst: please specify one of either -f or -C\n"
251 conf_values["DEBUG"]="1"
253 if o in ("-c", "--config"):
254 if len(sys.argv) < 3:
255 print "!!! catalyst: please specify one of either -f or -C\n"
261 if o in ("-C", "--cli"):
263 x=sys.argv.index(o)+1
264 while x < len(sys.argv):
265 mycmdline.append(sys.argv[x])
268 if o in ("-f", "--file"):
272 if o in ("-F", "--fetchonly"):
273 if len(sys.argv) < 3:
274 print "!!! catalyst: please specify one of either -f or -C\n"
278 conf_values["FETCH"]="1"
280 if o in ("-V", "--verbose"):
281 if len(sys.argv) < 3:
282 print "!!! catalyst: please specify one of either -f or -C\n"
286 conf_values["VERBOSE"]="1"
288 if o in ("-s", "--snapshot"):
289 if len(sys.argv) < 3:
290 print "!!! catalyst: missing snapshot identifier\n"
295 mycmdline.append("target=snapshot")
296 mycmdline.append("version_stamp="+a)
298 if o in ("-p", "--purge"):
299 if len(sys.argv) < 3:
300 print "!!! catalyst: please specify one of either -f or -C\n"
304 conf_values["PURGE"]="1"
305 if o in ("-a", "--clear-autoresume"):
306 if len(sys.argv) < 3:
307 print "!!! catalyst: please specify one of either -f or -C\n"
311 conf_values["CLEAR_AUTORESUME"]="1"
313 print "!!! catalyst: please specify one of either -f or -C\n"
317 # import configuration file and import our main module using those settings
318 parse_config(myconfig)
319 sys.path.append(conf_values["sharedir"]+"/modules")
320 from catalyst_support import *
322 # import the rest of the catalyst modules
323 targetmap=import_modules()
328 addlargs.update(do_spec(myspecfile))
331 addlargs.update(do_cli(mycmdline))
333 if not addlargs.has_key("target"):
334 raise CatalystError, "Required value \"target\" not specified."
336 # everything is setup, so the build is a go
338 build_target(addlargs, targetmap)
340 except CatalystError:
342 print "Catalyst aborting...."
344 except KeyboardInterrupt:
345 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
347 print "Catalyst aborting...."
349 print "Catalyst aborting...."
352 print "Catalyst aborting...."
356 #except CatalystError:
358 # print "Catalyst aborting...."
361 # print "\nproblem with command line or spec file ( Key Error )"
362 # print "Key: "+str(sys.exc_value)+" was not found"
363 # print "Catalyst aborting...."
365 #except UnboundLocalError:
367 # print "UnboundLocalError: "+str(sys.exc_value)+" was not found"
370 # print "Catalyst aborting...."