Added a patch from Andrew Gaffney <agaffney@gentoo.org> to fix my completely broken...
[catalyst.git] / catalyst
1 #!/usr/bin/python -OO
2
3 # Maintained in full by:
4 # Eric Edgar <rocket@gentoo.org>
5 # Chris Gianelloni <wolf31o2@gentoo.org>
6
7 import os,sys,imp,string,getopt
8 import pdb
9
10 __maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
11 __version__="2.0.3_pre3"
12
13 conf_values={}
14
15 def usage():
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"
28         print
29         print "Usage examples:"
30         print
31         print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
32         print "catalyst -C target=snapshot version_stamp=my_date"
33         print
34         print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
35         print "catalyst -s 20051208"
36         print
37         print "Using the specfile option (-f, --file) to build a stage target:"
38         print "catalyst -f stage1-specfile.spec"
39
40 def version():
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"
44
45 def parse_config(myconfig):
46         # search a couple of different areas for the main config file
47         myconf={}
48         config_file=""
49
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"}
55                 
56         # first, try the one passed (presumably from the cmdline)
57         if myconfig:
58                 if os.path.exists(myconfig):
59                         print "Using command line specified Catalyst configuration file, "+myconfig
60                         config_file=myconfig
61
62                 else:
63                         print "!!! catalyst: Could not use specified configuration file "+\
64                                 myconfig
65                         sys.exit(1)
66         
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"
71         
72         # can't find a config file (we are screwed), so bail out
73         else:
74                 print "!!! catalyst: Could not find a suitable configuration file"
75                 sys.exit(1)
76
77         # now, try and parse the config file "config_file"
78         try:
79                 execfile(config_file, myconf, myconf)
80         
81         except:
82                 print "!!! catalyst: Unable to parse configuration file, "+myconfig
83                 sys.exit(1)
84         
85         # now, load up the values into conf_values so that we can use them
86         for x in confdefaults.keys():
87                 if myconf.has_key(x):
88                         print "Setting",x,"to config file value \""+myconf[x]+"\""
89                         conf_values[x]=myconf[x]
90                 else:
91                         print "Setting",x,"to default value \""+confdefaults[x]+"\""
92                         conf_values[x]=confdefaults[x]
93
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"
98
99         if "ccache" in string.split(conf_values["options"]):
100                 print "Compiler cache support enabled."
101                 conf_values["CCACHE"]="1"
102
103         if "clear-autoresume" in string.split(conf_values["options"]):
104                 print "Cleaning autoresume flags support enabled."
105                 conf_values["CLEAR_AUTORESUME"]="1"
106
107 #       if "compress" in string.split(conf_values["options"]):
108 #               print "Compression enabled."
109 #               conf_values["COMPRESS"]="1"
110
111         if "distcc" in string.split(conf_values["options"]):
112                 print "Distcc support enabled."
113                 conf_values["DISTCC"]="1"
114
115         if "kerncache" in string.split(conf_values["options"]):
116                 print "Kernel cache support enabled."
117                 conf_values["KERNCACHE"]="1"
118
119         if "pkgcache" in string.split(conf_values["options"]):
120                 print "Package cache support enabled."
121                 conf_values["PKGCACHE"]="1"
122
123         if "purge" in string.split(conf_values["options"]):
124                 print "Purge support enabled."
125                 conf_values["PURGE"]="1"
126
127         if "seedcache" in string.split(conf_values["options"]):
128                 print "Seed cache support enabled."
129                 conf_values["SEEDCACHE"]="1"
130
131         if "snapcache" in string.split(conf_values["options"]):
132                 print "Snapshot cache support enabled."
133                 conf_values["SNAPCACHE"]="1"
134
135 #       if "tarball" in string.split(conf_values["options"]):
136 #               print "Tarball creation enabled."
137 #               conf_values["TARBALL"]="1"
138
139         if myconf.has_key("digests"):
140                 conf_values["digests"]=myconf["digests"]
141
142         if myconf.has_key("envscript"):
143                 print "Envscript support enabled."
144                 conf_values["ENVSCRIPT"]=myconf["envscript"]
145
146 def import_modules():
147         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
148         targetmap={}
149
150         try:
151                 for x in required_build_targets:
152                         try:
153                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
154                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
155                                 fh.close()
156                 
157                         except IOError:
158                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
159                                         conf_values.settings["sharedir"]+"/modules/"
160
161                 for x in valid_build_targets:
162                         try:
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)
166                                 fh.close()
167                 
168                         except IOError:
169                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
170                                         conf_values.settings["sharedir"]+"/modules/"
171
172         except ImportError:
173                 print "!!! catalyst: Python modules not found in "+\
174                         conf_values["sharedir"]+"/modules; exiting."
175                 sys.exit(1)
176
177         return targetmap
178
179 def do_spec(myspecfile):
180         try:
181                 addlargs=read_spec(myspecfile)
182         except:
183                 sys.exit(1)
184                 
185         return addlargs
186
187 def do_cli(cmdline):
188         try:
189                 return arg_parse(cmdline)
190         
191         except CatalystError:
192                 print "!!! catalyst: Could not parse commandline, exiting."
193                 sys.exit(1)
194         
195 def build_target(addlargs, targetmap):
196         try:
197                 if not targetmap.has_key(addlargs["target"]):
198                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
199                 
200                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
201         
202                 mytarget.run()
203
204         except:
205                 raise
206
207 if __name__ == "__main__":
208         targetmap={}
209         
210         version()
211         if os.getuid() != 0:
212                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
213                 print "!!! catalyst: This script requires root privileges to operate"
214                 sys.exit(2)
215
216         # we need some options in order to work correctly
217         if len(sys.argv) < 2:
218                 usage()
219                 sys.exit(2)
220
221         # parse out the command line arguments
222         try:
223                 opts,args = getopt.getopt(sys.argv[1:], "aphvdc:C:f:FVs:", ["purge","help", "version", "debug",\
224                         "clear_autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
225         
226         except getopt.GetoptError:
227                 usage()
228                 sys.exit(2)
229         
230         # defaults for commandline opts
231         debug=False
232         verbose=False
233         fetch=False
234         myconfig=""
235         myspecfile=""
236         mycmdline=[]
237         myopts=[]
238
239         # check preconditions
240         if len(opts) == 0:
241                 print "!!! catalyst: please specify one of either -f or -C\n"
242                 usage()
243                 sys.exit(2)
244         run=0   
245         for o, a in opts:
246                 if o in ("-h", "--help"):
247                         usage()
248                         sys.exit(1)
249                 
250                 if o in ("-V", "--version"):
251                         print "Catalyst version "+__version__
252                         sys.exit(1)
253
254                 if o in ("-d", "--debug"):
255                         if len(sys.argv) < 3:
256                                 print "!!! catalyst: please specify one of either -f or -C\n"
257                                 usage()
258                                 sys.exit(2)
259                         else:
260                                 conf_values["DEBUG"]="1"
261
262                 if o in ("-c", "--config"):
263                         if len(sys.argv) < 3:
264                                 print "!!! catalyst: please specify one of either -f or -C\n"
265                                 usage()
266                                 sys.exit(2)
267                         else:
268                                 myconfig=a
269
270                 if o in ("-C", "--cli"):
271                         run=1   
272                         x=sys.argv.index(o)+1
273                         while x < len(sys.argv):
274                                 mycmdline.append(sys.argv[x])
275                                 x=x+1
276                         
277                 if o in ("-f", "--file"):
278                         run=1   
279                         myspecfile=a
280
281                 if o in ("-F", "--fetchonly"):
282                         if len(sys.argv) < 3:
283                                 print "!!! catalyst: please specify one of either -f or -C\n"
284                                 usage()
285                                 sys.exit(2)
286                         else:
287                                 conf_values["FETCH"]="1"
288                         
289                 if o in ("-v", "--verbose"):
290                         if len(sys.argv) < 3:
291                                 print "!!! catalyst: please specify one of either -f or -C\n"
292                                 usage()
293                                 sys.exit(2)
294                         else:
295                                 conf_values["VERBOSE"]="1"
296
297                 if o in ("-s", "--snapshot"):
298                         if len(sys.argv) < 3:
299                                 print "!!! catalyst: missing snapshot identifier\n"
300                                 usage()
301                                 sys.exit(2)
302                         else:
303                                 run=1
304                                 mycmdline.append("target=snapshot")
305                                 mycmdline.append("version_stamp="+a)
306                 
307                 if o in ("-p", "--purge"):
308                         if len(sys.argv) < 3:
309                                 print "!!! catalyst: please specify one of either -f or -C\n"
310                                 usage()
311                                 sys.exit(2)
312                         else:
313                                 conf_values["PURGE"]="1"
314                 if o in ("-a", "--clear-autoresume"):
315                         if len(sys.argv) < 3:
316                                 print "!!! catalyst: please specify one of either -f or -C\n"
317                                 usage()
318                                 sys.exit(2)
319                         else:
320                                 conf_values["CLEAR_AUTORESUME"]="1"
321         if run != 1:
322                 print "!!! catalyst: please specify one of either -f or -C\n"
323                 usage()
324                 sys.exit(2)
325
326         # import configuration file and import our main module using those settings
327         parse_config(myconfig)
328         sys.path.append(conf_values["sharedir"]+"/modules")
329         from catalyst_support import *
330         
331         # Start checking that digests are valid now that the hash_map was imported from catalyst_support
332         if conf_values.has_key("digests"):
333                 for i in conf_values["digests"].split():
334                         if not hash_map.has_key(i):
335                                 print
336                                 print i+" is not a valid digest entry"
337                                 print "Valid digest entries:"
338                                 print hash_map.keys()
339                                 print
340                                 print "Catalyst aborting...."
341                                 sys.exit(2)
342                         if find_binary(hash_map[i][1]) == None:
343                                 print
344                                 print "digest="+i
345                                 print "\tThe "+hash_map[i][1]+\
346                                         " binary was not found. It needs to be in your system path"
347                                 print
348                                 print "Catalyst aborting...."
349                                 sys.exit(2)
350         if conf_values.has_key("hash_function"):
351                 if not hash_map.has_key(conf_values["hash_function"]):
352                         print
353                         print conf_values["hash_function"]+" is not a valid hash_function entry"
354                         print "Valid hash_function entries:"
355                         print hash_map.keys()
356                         print
357                         print "Catalyst aborting...."
358                         sys.exit(2)
359                 if find_binary(hash_map[conf_values["hash_function"]][1]) == None:
360                         print
361                         print "hash_function="+conf_values["hash_function"]
362                         print "\tThe "+hash_map[conf_values["hash_function"]][1]+\
363                                 " binary was not found. It needs to be in your system path"
364                         print
365                         print "Catalyst aborting...."
366                         sys.exit(2)
367
368         # import the rest of the catalyst modules
369         targetmap=import_modules()
370
371         addlargs={}
372         
373         if myspecfile:
374                 addlargs.update(do_spec(myspecfile))
375         
376         if mycmdline:
377                 addlargs.update(do_cli(mycmdline))
378         
379         if not addlargs.has_key("target"):
380                 raise CatalystError, "Required value \"target\" not specified."
381
382         # everything is setup, so the build is a go
383         try:
384                 build_target(addlargs, targetmap)
385                         
386         except CatalystError:
387                 print
388                 print "Catalyst aborting...."
389                 sys.exit(2)
390         except KeyboardInterrupt:
391                 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
392                 print
393                 print "Catalyst aborting...."
394                 sys.exit(2)
395         except LockInUse:
396                 print "Catalyst aborting...."
397                 sys.exit(2)
398         except:
399                 print "Catalyst aborting...."
400                 raise
401                 sys.exit(2)
402
403         #except CatalystError:
404         #       print
405         #       print "Catalyst aborting...."
406         #       sys.exit(2)
407         #except KeyError:
408         #       print "\nproblem with command line or spec file ( Key Error )"
409         #       print "Key: "+str(sys.exc_value)+" was not found"
410         #       print "Catalyst aborting...."
411         #       sys.exit(2)
412         #except UnboundLocalError:
413         #       print
414         #       print "UnboundLocalError: "+str(sys.exc_value)+" was not found"
415         #       raise
416         #       print
417         #       print "Catalyst aborting...."
418         #       sys.exit(2)