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