2 # Copyright 1999-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/catalyst/catalyst,v 1.56 2005/01/02 04:15:51 zhen Exp $
6 # Maintained in full by John Davis <zhen@gentoo.org>
8 import os,sys,imp,string,getopt
10 __maintainer__="John Davis <zhen@gentoo.org>"
16 print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
17 print " -c --config use specified configuration file"
18 print " -C --cli catalyst commandline (MUST BE LAST OPTION)"
19 print " -d --debug enable debugging"
20 print " -f --file read specfile"
21 print " -h --help print this help message"
22 print " -s --snapshot generate a Portage snapshot"
23 print " -v --version display version information"
24 print " -V --verbose verbose output"
26 print "Usage examples:"
28 print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
29 print "catalyst -C target=snapshot version_stamp=my_date"
31 print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
32 print "catalyst -s 2004.3"
34 print "Using the specfile option (-f, --file) to build a stage target:"
35 print "catalyst -f stage1-specfile.spec"
38 print "Gentoo Catalyst, version "+__version__
39 print "Copyright 2003-2004 The Gentoo Foundation"
40 print "Distributed under the GNU General Public License version 2\n"
43 def parse_config(myconfig):
44 # search a couple of different areas for the main config file
48 confdefaults={ "storedir":"/var/tmp/catalyst",\
49 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
50 "portdir":"/usr/portage","options":""}
52 # first, try the one passed (presumably from the cmdline)
54 if os.path.exists(myconfig):
55 print "Using command line specified Catalyst configuration file, "+myconfig
59 print "!!! catalyst: Could not use specified configuration file "+\
63 # next, try the default location
64 elif os.path.exists("/etc/catalyst/catalyst.conf"):
65 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
66 config_file="/etc/catalyst/catalyst.conf"
68 # can't find a config file (we are screwed), so bail out
70 print "!!! catalyst: Could not find a suitable configuration file"
73 # now, try and parse the config file "config_file"
75 execfile(config_file, myconf, myconf)
78 print "!!! catalyst: Unable to parse configuration file, "+myconfig
81 # now, load up the values into conf_values so that we can use them
82 for x in confdefaults.keys():
84 print "Setting",x,"to config file value \""+myconf[x]+"\""
85 conf_values[x]=myconf[x]
87 print "Setting",x,"to default value \""+confdefaults[x]+"\""
88 conf_values[x]=confdefaults[x]
90 # parse out the rest of the options from the config file
91 if "ccache" in string.split(conf_values["options"]):
92 print "Compiler cache support enabled."
93 conf_values["CCACHE"]="1"
95 if "pkgcache" in string.split(conf_values["options"]):
96 print "Package cache support enabled."
97 conf_values["PKGCACHE"]="1"
99 if "kerncache" in string.split(conf_values["options"]):
100 print "Kernel cache support enabled."
101 conf_values["KERNCACHE"]="1"
103 if "distcc" in string.split(conf_values["options"]):
104 print "Distcc support enabled."
105 conf_values["DISTCC"]="1"
107 if "autoresume" in string.split(conf_values["options"]):
108 print "Autoresuming support enabled."
109 conf_values["AUTORESUME"]="1"
111 if myconf.has_key("envscript"):
112 print "Envscript support enabled."
113 conf_values["ENVSCRIPT"]=myconf["envscript"]
115 def import_modules():
116 # import catalyst's own modules (i.e. catalyst_support and the arch modules)
120 for x in required_build_targets:
122 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
123 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
127 raise CatalystError,"Can't find "+x+".py plugin in "+\
128 conf_values.settings["sharedir"]+"/modules/"
130 for x in valid_build_targets:
132 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
133 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
134 module.register(targetmap)
138 raise CatalystError,"Can't find "+x+".py plugin in "+\
139 conf_values.settings["sharedir"]+"/modules/"
142 print "!!! catalyst: Python modules not found in "+\
143 conf_values["sharedir"]+"/modules; exiting."
148 def do_spec(myspecfile):
150 addlargs=read_spec(myspecfile)
158 return arg_parse(cmdline)
160 except CatalystError:
161 print "!!! catalyst: Could not parse commandline, exiting."
164 def build_target(addlargs, targetmap):
166 if not targetmap.has_key(addlargs["target"]):
167 raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
169 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
172 except CatalystError:
175 if __name__ == "__main__":
180 # catalyst cannot be run as a normal user due to chroots, mounts, etc
181 print "!!! catalyst: This script requires root privileges to operate"
184 # we need some options in order to work correctly
185 if len(sys.argv) < 2:
189 # parse out the command line arguments
191 opts,args = getopt.getopt(sys.argv[1:], "hvdc:C:f:Vs:", ["help", "version", "debug",\
192 "config=", "cli=", "file=", "verbose","snapshot="])
194 except getopt.GetoptError:
198 # defaults for commandline opts
206 # check preconditions
208 print "!!! catalyst: please specify one of either -f or -C\n"
212 # check to see if -f and -C are used together
216 if ('-f' in myopts or '--file' in myopts) and ('-C' in myopts or '--cli' in myopts):
217 print "!!! catalyst: please specify one of either -f or -C\n"
222 if o in ("-h", "--help"):
226 if o in ("-v", "--version"):
227 print "Catalyst version "+__version__
230 if o in ("-d", "--debug"):
231 if len(sys.argv) < 3:
232 print "!!! catalyst: please specify one of either -f or -C\n"
236 conf_values["DEBUG"]="1"
238 if o in ("-c", "--config"):
239 if len(sys.argv) < 3:
240 print "!!! catalyst: please specify one of either -f or -C\n"
246 if o in ("-C", "--cli"):
247 x=sys.argv.index(o)+1
248 while x < len(sys.argv):
249 mycmdline.append(sys.argv[x])
252 if o in ("-f", "--file"):
255 if o in ("-V", "--verbose"):
256 if len(sys.argv) < 3:
257 print "!!! catalyst: please specify one of either -f or -C\n"
261 conf_values["VERBOSE"]="1"
263 if o in ("-s", "--snapshot"):
264 if len(sys.argv) < 3:
265 print "!!! catalyst: missing snapshot identifier\n"
269 mycmdline.append("target=snapshot")
270 mycmdline.append("version_stamp="+a)
272 # import configuration file and import our main module using those settings
273 parse_config(myconfig)
274 sys.path.append(conf_values["sharedir"]+"/modules")
275 from catalyst_support import *
277 # import the rest of the catalyst modules
278 targetmap=import_modules()
281 addlargs=do_spec(myspecfile)
284 addlargs=do_cli(mycmdline)
286 # everything is setup, so the build is a go
288 build_target(addlargs, targetmap)
290 except CatalystError:
291 raise CatalystError,"Could not complete build"