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