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