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