tweaks to error handling in the main catalyst script, and added portage_confdir to...
[catalyst.git] / catalyst
1 #!/usr/bin/python
2 # Copyright 1999-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/catalyst/catalyst,v 1.56 2005/01/02 04:15:51 zhen Exp $
5
6 # Maintained in full by John Davis <zhen@gentoo.org>
7
8 import os,sys,imp,string,getopt
9
10 __maintainer__="John Davis <zhen@gentoo.org>"
11 __version__="1.1.2"
12
13 conf_values={}
14
15 def usage():
16         print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
17         print " -c --config             use specified configuration file"
18         print " -C --cli                catalyst commandline (MUST BE LAST OPTION)"
19         print " -d --debug              enable debugging"
20         print " -f --file               read specfile"
21         print " -h --help               print this help message"
22         print " -s --snapshot           generate a Portage snapshot"
23         print " -v --version            display version information"
24         print " -V --verbose            verbose output"
25         print
26         print "Usage examples:"
27         print
28         print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
29         print "catalyst -C target=snapshot version_stamp=my_date"
30         print
31         print "Using the snapshot option (-s, --snapshot) to build a Portage snapshot:"
32         print "catalyst -s 2004.3"
33         print
34         print "Using the specfile option (-f, --file) to build a stage target:"
35         print "catalyst -f stage1-specfile.spec"
36
37 def version():
38         print "Gentoo Catalyst, version "+__version__
39         print "Copyright 2003-2004 The Gentoo Foundation"
40         print "Distributed under the GNU General Public License version 2\n"
41         
42
43 def parse_config(myconfig):
44         # search a couple of different areas for the main config file
45         myconf={}
46         config_file=""
47
48         confdefaults={ "storedir":"/var/tmp/catalyst",\
49                 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
50                 "portdir":"/usr/portage","options":""}
51                 
52         # first, try the one passed (presumably from the cmdline)
53         if myconfig:
54                 if os.path.exists(myconfig):
55                         print "Using command line specified Catalyst configuration file, "+myconfig
56                         config_file=myconfig
57
58                 else:
59                         print "!!! catalyst: Could not use specified configuration file "+\
60                                 myconfig
61                         sys.exit(1)
62         
63         # next, try the default location
64         elif os.path.exists("/etc/catalyst/catalyst.conf"):
65                 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
66                 config_file="/etc/catalyst/catalyst.conf"
67         
68         # can't find a config file (we are screwed), so bail out
69         else:
70                 print "!!! catalyst: Could not find a suitable configuration file"
71                 sys.exit(1)
72
73         # now, try and parse the config file "config_file"
74         try:
75                 execfile(config_file, myconf, myconf)
76         
77         except:
78                 print "!!! catalyst: Unable to parse configuration file, "+myconfig
79                 sys.exit(1)
80         
81         # now, load up the values into conf_values so that we can use them
82         for x in confdefaults.keys():
83                 if myconf.has_key(x):
84                         print "Setting",x,"to config file value \""+myconf[x]+"\""
85                         conf_values[x]=myconf[x]
86                 else:
87                         print "Setting",x,"to default value \""+confdefaults[x]+"\""
88                         conf_values[x]=confdefaults[x]
89
90         # parse out the rest of the options from the config file
91         if "ccache" in string.split(conf_values["options"]):
92                 print "Compiler cache support enabled."
93                 conf_values["CCACHE"]="1"
94
95         if "pkgcache" in string.split(conf_values["options"]):
96                 print "Package cache support enabled."
97                 conf_values["PKGCACHE"]="1"
98
99         if "kerncache" in string.split(conf_values["options"]):
100                 print "Kernel cache support enabled."
101                 conf_values["KERNCACHE"]="1"
102         
103         if "distcc" in string.split(conf_values["options"]):
104                 print "Distcc support enabled."
105                 conf_values["DISTCC"]="1"
106
107         if "autoresume" in string.split(conf_values["options"]):
108                 print "Autoresuming support enabled."
109                 conf_values["AUTORESUME"]="1"
110
111         if myconf.has_key("envscript"):
112                 print "Envscript support enabled."
113                 conf_values["ENVSCRIPT"]=myconf["envscript"]
114
115 def import_modules():
116         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
117         targetmap={}
118         
119         try:
120                 for x in required_build_targets:
121                         try:
122                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
123                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
124                                 fh.close()
125                 
126                         except IOError:
127                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
128                                         conf_values.settings["sharedir"]+"/modules/"
129
130                 for x in valid_build_targets:
131                         try:
132                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
133                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
134                                 module.register(targetmap)
135                                 fh.close()
136                 
137                         except IOError:
138                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
139                                         conf_values.settings["sharedir"]+"/modules/"
140
141         except ImportError:
142                 print "!!! catalyst: Python modules not found in "+\
143                         conf_values["sharedir"]+"/modules; exiting."
144                 sys.exit(1)
145
146         return targetmap
147
148 def do_spec(myspecfile):
149         try:
150                 addlargs=read_spec(myspecfile)
151         except:
152                 sys.exit(1)
153                 
154         return addlargs
155
156 def do_cli(cmdline):
157         try:
158                 return arg_parse(cmdline)
159         
160         except CatalystError:
161                 print "!!! catalyst: Could not parse commandline, exiting."
162                 sys.exit(1)
163         
164 def build_target(addlargs, targetmap):
165         try:
166                 if not targetmap.has_key(addlargs["target"]):
167                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
168                 
169                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
170                 mytarget.run()
171
172         except CatalystError:
173                 sys.exit(1)
174         
175 if __name__ == "__main__":
176         targetmap={}
177         
178         version()
179         if os.getuid() != 0:
180                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
181                 print "!!! catalyst: This script requires root privileges to operate"
182                 sys.exit(2)
183
184         # we need some options in order to work correctly
185         if len(sys.argv) < 2:
186                 usage()
187                 sys.exit(2)
188
189         # parse out the command line arguments
190         try:
191                 opts,args = getopt.getopt(sys.argv[1:], "hvdc:C:f:Vs:", ["help", "version", "debug",\
192                         "config=", "cli=", "file=", "verbose","snapshot="])
193         
194         except getopt.GetoptError:
195                 usage()
196                 sys.exit(2)
197         
198         # defaults for commandline opts
199         debug=False
200         verbose=False
201         myconfig=""
202         myspecfile=""
203         mycmdline=[]
204         myopts=[]
205
206         # check preconditions
207         if len(opts) == 0:
208                 print "!!! catalyst: please specify one of either -f or -C\n"
209                 usage()
210                 sys.exit(2)
211         
212         # check to see if -f and -C are used together
213         for i in opts:
214                 myopts.append(i[0])
215                 
216         if ('-f' in myopts or '--file' in myopts) and ('-C' in myopts or '--cli' in myopts):
217                 print "!!! catalyst: please specify one of either -f or -C\n"
218                 usage()
219                 sys.exit(2)
220
221         for o, a in opts:
222                 if o in ("-h", "--help"):
223                         usage()
224                         sys.exit(1)
225                 
226                 if o in ("-v", "--version"):
227                         print "Catalyst version "+__version__
228                         sys.exit(1)
229
230                 if o in ("-d", "--debug"):
231                         if len(sys.argv) < 3:
232                                 print "!!! catalyst: please specify one of either -f or -C\n"
233                                 usage()
234                                 sys.exit(2)
235                         else:
236                                 conf_values["DEBUG"]="1"
237
238                 if o in ("-c", "--config"):
239                         if len(sys.argv) < 3:
240                                 print "!!! catalyst: please specify one of either -f or -C\n"
241                                 usage()
242                                 sys.exit(2)
243                         else:
244                                 myconfig=a
245
246                 if o in ("-C", "--cli"):
247                         x=sys.argv.index(o)+1
248                         while x < len(sys.argv):
249                                 mycmdline.append(sys.argv[x])
250                                 x=x+1
251                         
252                 if o in ("-f", "--file"):
253                         myspecfile=a
254                         
255                 if o in ("-V", "--verbose"):
256                         if len(sys.argv) < 3:
257                                 print "!!! catalyst: please specify one of either -f or -C\n"
258                                 usage()
259                                 sys.exit(2)
260                         else:
261                                 conf_values["VERBOSE"]="1"
262
263                 if o in ("-s", "--snapshot"):
264                         if len(sys.argv) < 3:
265                                 print "!!! catalyst: missing snapshot identifier\n"
266                                 usage()
267                                 sys.exit(2)
268                         else:
269                                 mycmdline.append("target=snapshot")
270                                 mycmdline.append("version_stamp="+a)
271         
272         # import configuration file and import our main module using those settings
273         parse_config(myconfig)
274         sys.path.append(conf_values["sharedir"]+"/modules")
275         from catalyst_support import *
276                 
277         # import the rest of the catalyst modules
278         targetmap=import_modules()
279
280         if myspecfile:
281                 addlargs=do_spec(myspecfile)
282         
283         if mycmdline:
284                 addlargs=do_cli(mycmdline)
285         
286         # everything is setup, so the build is a go
287         try:
288                 build_target(addlargs, targetmap)
289         
290         except CatalystError:
291                 raise CatalystError,"Could not complete build"
292                 sys.exit(2)