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