Remove the die on LIBDIR check, since it won't be set on non-multilib profiles. ...
[catalyst.git] / catalyst
1 #!/usr/bin/python -OO
2
3 # Maintained in full by:
4 # Andrew Gaffney <agaffney@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.6_pre17"
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 release 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 release snapshot:"
35         print "catalyst -s 20071121"
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 "Catalyst, version "+__version__
42         print "Copyright 2003-2008 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 "icecream" in string.split(conf_values["options"]):
116                 print "Icecream compiler cluster support enabled."
117                 conf_values["ICECREAM"]="1"
118
119         if "kerncache" in string.split(conf_values["options"]):
120                 print "Kernel cache support enabled."
121                 conf_values["KERNCACHE"]="1"
122
123         if "pkgcache" in string.split(conf_values["options"]):
124                 print "Package cache support enabled."
125                 conf_values["PKGCACHE"]="1"
126
127         if "purge" in string.split(conf_values["options"]):
128                 print "Purge support enabled."
129                 conf_values["PURGE"]="1"
130
131         if "seedcache" in string.split(conf_values["options"]):
132                 print "Seed cache support enabled."
133                 conf_values["SEEDCACHE"]="1"
134
135         if "snapcache" in string.split(conf_values["options"]):
136                 print "Snapshot cache support enabled."
137                 conf_values["SNAPCACHE"]="1"
138
139         if "metadata_overlay" in conf_values["options"].split():
140                 print "Use of metadata_overlay module for portage enabled."
141                 conf_values["METADATA_OVERLAY"]="1"
142
143 #       if "tarball" in string.split(conf_values["options"]):
144 #               print "Tarball creation enabled."
145 #               conf_values["TARBALL"]="1"
146
147         if myconf.has_key("digests"):
148                 conf_values["digests"]=myconf["digests"]
149         if myconf.has_key("contents"):
150                 conf_values["contents"]=myconf["contents"]
151
152         if myconf.has_key("envscript"):
153                 print "Envscript support enabled."
154                 conf_values["ENVSCRIPT"]=myconf["envscript"]
155
156 def import_modules():
157         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
158         targetmap={}
159
160         try:
161                 for x in required_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                                 fh.close()
166                 
167                         except IOError:
168                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
169                                         conf_values.settings["sharedir"]+"/modules/"
170
171                 for x in valid_build_targets:
172                         try:
173                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
174                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
175                                 module.register(targetmap)
176                                 fh.close()
177                 
178                         except IOError:
179                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
180                                         conf_values.settings["sharedir"]+"/modules/"
181
182         except ImportError:
183                 print "!!! catalyst: Python modules not found in "+\
184                         conf_values["sharedir"]+"/modules; exiting."
185                 sys.exit(1)
186
187         return targetmap
188
189 def do_spec(myspecfile):
190         try:
191                 addlargs=read_spec(myspecfile)
192         except:
193                 sys.exit(1)
194                 
195         return addlargs
196
197 def do_cli(cmdline):
198         try:
199                 return arg_parse(cmdline)
200         
201         except CatalystError:
202                 print "!!! catalyst: Could not parse commandline, exiting."
203                 sys.exit(1)
204         
205 def build_target(addlargs, targetmap):
206         try:
207                 if not targetmap.has_key(addlargs["target"]):
208                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
209                 
210                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
211         
212                 mytarget.run()
213
214         except:
215                 # TODO: Capture traceback, so we can display this error after printing of the traceback
216                 print "!!! catalyst: Error encountered during run of target " + addlargs["target"]
217                 raise
218
219 if __name__ == "__main__":
220         targetmap={}
221         
222         version()
223         if os.getuid() != 0:
224                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
225                 print "!!! catalyst: This script requires root privileges to operate"
226                 sys.exit(2)
227
228         # we need some options in order to work correctly
229         if len(sys.argv) < 2:
230                 usage()
231                 sys.exit(2)
232
233         # parse out the command line arguments
234         try:
235                 opts,args = getopt.getopt(sys.argv[1:], "aphvdc:C:f:FVs:", ["purge","help", "version", "debug",\
236                         "clear-autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
237         
238         except getopt.GetoptError:
239                 usage()
240                 sys.exit(2)
241         
242         # defaults for commandline opts
243         debug=False
244         verbose=False
245         fetch=False
246         myconfig=""
247         myspecfile=""
248         mycmdline=[]
249         myopts=[]
250
251         # check preconditions
252         if len(opts) == 0:
253                 print "!!! catalyst: please specify one of either -f or -C\n"
254                 usage()
255                 sys.exit(2)
256         run=0   
257         for o, a in opts:
258                 if o in ("-h", "--help"):
259                         usage()
260                         sys.exit(1)
261                 
262                 if o in ("-V", "--version"):
263                         print "Catalyst version "+__version__
264                         sys.exit(1)
265
266                 if o in ("-d", "--debug"):
267                         if len(sys.argv) < 3:
268                                 print "!!! catalyst: please specify one of either -f or -C\n"
269                                 usage()
270                                 sys.exit(2)
271                         else:
272                                 conf_values["DEBUG"]="1"
273                                 conf_values["VERBOSE"]="1"
274
275                 if o in ("-c", "--config"):
276                         if len(sys.argv) < 3:
277                                 print "!!! catalyst: please specify one of either -f or -C\n"
278                                 usage()
279                                 sys.exit(2)
280                         else:
281                                 myconfig=a
282
283                 if o in ("-C", "--cli"):
284                         run=1   
285                         x=sys.argv.index(o)+1
286                         while x < len(sys.argv):
287                                 mycmdline.append(sys.argv[x])
288                                 x=x+1
289                         
290                 if o in ("-f", "--file"):
291                         run=1   
292                         myspecfile=a
293
294                 if o in ("-F", "--fetchonly"):
295                         if len(sys.argv) < 3:
296                                 print "!!! catalyst: please specify one of either -f or -C\n"
297                                 usage()
298                                 sys.exit(2)
299                         else:
300                                 conf_values["FETCH"]="1"
301                         
302                 if o in ("-v", "--verbose"):
303                         if len(sys.argv) < 3:
304                                 print "!!! catalyst: please specify one of either -f or -C\n"
305                                 usage()
306                                 sys.exit(2)
307                         else:
308                                 conf_values["VERBOSE"]="1"
309
310                 if o in ("-s", "--snapshot"):
311                         if len(sys.argv) < 3:
312                                 print "!!! catalyst: missing snapshot identifier\n"
313                                 usage()
314                                 sys.exit(2)
315                         else:
316                                 run=1
317                                 mycmdline.append("target=snapshot")
318                                 mycmdline.append("version_stamp="+a)
319                 
320                 if o in ("-p", "--purge"):
321                         if len(sys.argv) < 3:
322                                 print "!!! catalyst: please specify one of either -f or -C\n"
323                                 usage()
324                                 sys.exit(2)
325                         else:
326                                 conf_values["PURGE"]="1"
327                 if o in ("-a", "--clear-autoresume"):
328                         if len(sys.argv) < 3:
329                                 print "!!! catalyst: please specify one of either -f or -C\n"
330                                 usage()
331                                 sys.exit(2)
332                         else:
333                                 conf_values["CLEAR_AUTORESUME"]="1"
334         if run != 1:
335                 print "!!! catalyst: please specify one of either -f or -C\n"
336                 usage()
337                 sys.exit(2)
338
339         # import configuration file and import our main module using those settings
340         parse_config(myconfig)
341         sys.path.append(conf_values["sharedir"]+"/modules")
342         from catalyst_support import *
343         
344         # Start checking that digests are valid now that the hash_map was imported from catalyst_support
345         if conf_values.has_key("digests"):
346                 for i in conf_values["digests"].split():
347                         if not hash_map.has_key(i):
348                                 print
349                                 print i+" is not a valid digest entry"
350                                 print "Valid digest entries:"
351                                 print hash_map.keys()
352                                 print
353                                 print "Catalyst aborting...."
354                                 sys.exit(2)
355                         if find_binary(hash_map[i][1]) == None:
356                                 print
357                                 print "digest="+i
358                                 print "\tThe "+hash_map[i][1]+\
359                                         " binary was not found. It needs to be in your system path"
360                                 print
361                                 print "Catalyst aborting...."
362                                 sys.exit(2)
363         if conf_values.has_key("hash_function"):
364                 if not hash_map.has_key(conf_values["hash_function"]):
365                         print
366                         print conf_values["hash_function"]+" is not a valid hash_function entry"
367                         print "Valid hash_function entries:"
368                         print hash_map.keys()
369                         print
370                         print "Catalyst aborting...."
371                         sys.exit(2)
372                 if find_binary(hash_map[conf_values["hash_function"]][1]) == None:
373                         print
374                         print "hash_function="+conf_values["hash_function"]
375                         print "\tThe "+hash_map[conf_values["hash_function"]][1]+\
376                                 " binary was not found. It needs to be in your system path"
377                         print
378                         print "Catalyst aborting...."
379                         sys.exit(2)
380
381         # import the rest of the catalyst modules
382         targetmap=import_modules()
383
384         addlargs={}
385         
386         if myspecfile:
387                 addlargs.update(do_spec(myspecfile))
388         
389         if mycmdline:
390                 addlargs.update(do_cli(mycmdline))
391         
392         if not addlargs.has_key("target"):
393                 raise CatalystError, "Required value \"target\" not specified."
394
395         # everything is setup, so the build is a go
396         try:
397                 build_target(addlargs, targetmap)
398                         
399         except CatalystError:
400                 print
401                 print "Catalyst aborting...."
402                 sys.exit(2)
403         except KeyboardInterrupt:
404                 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
405                 print
406                 print "Catalyst aborting...."
407                 sys.exit(2)
408         except LockInUse:
409                 print "Catalyst aborting...."
410                 sys.exit(2)
411         except:
412                 print "Catalyst aborting...."
413                 raise
414                 sys.exit(2)
415
416         #except CatalystError:
417         #       print
418         #       print "Catalyst aborting...."
419         #       sys.exit(2)
420         #except KeyError:
421         #       print "\nproblem with command line or spec file ( Key Error )"
422         #       print "Key: "+str(sys.exc_value)+" was not found"
423         #       print "Catalyst aborting...."
424         #       sys.exit(2)
425         #except UnboundLocalError:
426         #       print
427         #       print "UnboundLocalError: "+str(sys.exc_value)+" was not found"
428         #       raise
429         #       print
430         #       print "Catalyst aborting...."
431         #       sys.exit(2)