2 # Copyright 1999-2004 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/catalyst/catalyst,v 1.48 2004/10/04 13:10:32 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 " -h --help print this help message"
18 print " -v --version display version information"
19 print " -d --debug enable debugging"
20 print " -c --config use specified configuration file"
21 print " -C --cli catalyst commandline (MUST BE LAST OPTION)"
22 print " -f --file read specfile"
23 print " -V --verbose verbose output"
24 print " -s --snapshot generate a Portage snapshot"
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 "distcc" in string.split(conf_values["options"]):
100 print "Distcc support enabled."
101 conf_values["DISTCC"]="1"
103 if "autoresume" in string.split(conf_values["options"]):
104 print "Autoresuming support enabled."
105 conf_values["AUTORESUME"]="1"
107 if myconf.has_key("envscript"):
108 print "Envscript support enabled."
109 conf_values["ENVSCRIPT"]=myconf["envscript"]
111 def import_modules():
112 # import catalyst's own modules (i.e. catalyst_support and the arch modules)
116 for x in required_build_targets:
118 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
119 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
123 raise CatalystError,"Can't find "+x+".py plugin in "+\
124 conf_values.settings["sharedir"]+"/modules/"
126 for x in valid_build_targets:
128 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
129 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
130 module.register(targetmap)
134 raise CatalystError,"Can't find "+x+".py plugin in "+\
135 conf_values.settings["sharedir"]+"/modules/"
138 print "!!! catalyst: Python modules not found in "+\
139 conf_values["sharedir"]+"/modules; exiting."
144 def do_spec(myspecfile):
146 addlargs=read_spec(myspecfile)
154 return arg_parse(cmdline)
156 except CatalystError:
157 print "!!! catalyst: Could not parse commandline, exiting."
160 def build_target(addlargs, targetmap):
162 if not targetmap.has_key(addlargs["target"]):
163 raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
165 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
168 except CatalystError:
171 if __name__ == "__main__":
176 # catalyst cannot be run as a normal user due to chroots, mounts, etc
177 print "!!! catalyst: This script requires root privileges to operate"
180 # we need some options in order to work correctly
181 if len(sys.argv) < 2:
185 # parse out the command line arguments
187 opts,args = getopt.getopt(sys.argv[1:], "hvdc:C:f:Vs:", ["help", "version", "debug",\
188 "config=", "cli=", "file=", "verbose","snapshot="])
190 except getopt.GetoptError:
194 # defaults for commandline opts
202 # check preconditions
204 print "!!! catalyst: please specify one of either -f or -C\n"
208 # check to see if -f and -C are used together
212 if ('-f' in myopts or '--file' in myopts) and ('-C' in myopts or '--cli' in myopts):
213 print "!!! catalyst: please specify one of either -f or -C\n"
218 if o in ("-h", "--help"):
222 if o in ("-v", "--version"):
223 print "Catalyst version "+__version__
226 if o in ("-d", "--debug"):
227 if len(sys.argv) < 3:
228 print "!!! catalyst: please specify one of either -f or -C\n"
232 conf_values["DEBUG"]="1"
234 if o in ("-c", "--config"):
235 if len(sys.argv) < 3:
236 print "!!! catalyst: please specify one of either -f or -C\n"
242 if o in ("-C", "--cli"):
243 x=sys.argv.index(o)+1
244 while x < len(sys.argv):
245 mycmdline.append(sys.argv[x])
248 if o in ("-f", "--file"):
251 if o in ("-V", "--verbose"):
252 if len(sys.argv) < 3:
253 print "!!! catalyst: please specify one of either -f or -C\n"
257 conf_values["VERBOSE"]="1"
259 if o in ("-s", "--snapshot"):
260 if len(sys.argv) < 3:
261 print "!!! catalyst: missing snapshot identifier\n"
265 mycmdline.append("target=snapshot")
266 mycmdline.append("version_stamp="+a)
268 # import configuration file and import our main module using those settings
269 parse_config(myconfig)
270 sys.path.append(conf_values["sharedir"]+"/modules")
271 from catalyst_support import *
273 # import the rest of the catalyst modules
274 targetmap=import_modules()
277 addlargs=do_spec(myspecfile)
280 addlargs=do_cli(mycmdline)
282 # everything is setup, so the build is a go
284 build_target(addlargs, targetmap)
286 print "!!! catalyst: could not complete build"