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