new -s option for creating snapshots
[catalyst.git] / catalyst
1 #!/usr/bin/python
2 # Copyright 1999-2004 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/catalyst/catalyst,v 1.48 2004/10/04 13:10:32 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.0.9"
12
13 conf_values={}
14
15 def usage():
16         print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
17         print " -h --help               print this help message"
18         print " -v --version            display version information"
19         print " -d --debug              enable debugging"
20         print " -c --config             use specified configuration file"
21         print " -C --cli                catalyst commandline (MUST BE LAST OPTION)"
22         print " -f --file               read specfile"
23         print " -V --verbose            verbose output"
24         print " -s --snapshot           generate a Portage snapshot"
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 "distcc" in string.split(conf_values["options"]):
100                 print "Distcc support enabled."
101                 conf_values["DISTCC"]="1"
102
103         if "autoresume" in string.split(conf_values["options"]):
104                 print "Autoresuming support enabled."
105                 conf_values["AUTORESUME"]="1"
106
107         if myconf.has_key("envscript"):
108                 print "Envscript support enabled."
109                 conf_values["ENVSCRIPT"]=myconf["envscript"]
110
111 def import_modules():
112         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
113         targetmap={}
114         
115         try:
116                 for x in required_build_targets:
117                         try:
118                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
119                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
120                                 fh.close()
121                 
122                         except IOError:
123                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
124                                         conf_values.settings["sharedir"]+"/modules/"
125
126                 for x in valid_build_targets:
127                         try:
128                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
129                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
130                                 module.register(targetmap)
131                                 fh.close()
132                 
133                         except IOError:
134                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
135                                         conf_values.settings["sharedir"]+"/modules/"
136
137         except ImportError:
138                 print "!!! catalyst: Python modules not found in "+\
139                         conf_values["sharedir"]+"/modules; exiting."
140                 sys.exit(1)
141
142         return targetmap
143
144 def do_spec(myspecfile):
145         try:
146                 addlargs=read_spec(myspecfile)
147         except:
148                 sys.exit(1)
149                 
150         return addlargs
151
152 def do_cli(cmdline):
153         try:
154                 return arg_parse(cmdline)
155         
156         except CatalystError:
157                 print "!!! catalyst: Could not parse commandline, exiting."
158                 sys.exit(1)
159         
160 def build_target(addlargs, targetmap):
161         try:
162                 if not targetmap.has_key(addlargs["target"]):
163                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
164                 
165                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
166                 mytarget.run()
167
168         except CatalystError:
169                 sys.exit(1)
170         
171 if __name__ == "__main__":
172         targetmap={}
173         
174         version()
175         if os.getuid() != 0:
176                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
177                 print "!!! catalyst: This script requires root privileges to operate"
178                 sys.exit(2)
179
180         # we need some options in order to work correctly
181         if len(sys.argv) < 2:
182                 usage()
183                 sys.exit(2)
184
185         # parse out the command line arguments
186         try:
187                 opts,args = getopt.getopt(sys.argv[1:], "hvdc:C:f:Vs:", ["help", "version", "debug",\
188                         "config=", "cli=", "file=", "verbose","snapshot="])
189         
190         except getopt.GetoptError:
191                 usage()
192                 sys.exit(2)
193         
194         # defaults for commandline opts
195         debug=False
196         verbose=False
197         myconfig=""
198         myspecfile=""
199         mycmdline=[]
200         myopts=[]
201
202         # check preconditions
203         if len(opts) == 0:
204                 print "!!! catalyst: please specify one of either -f or -C\n"
205                 usage()
206                 sys.exit(2)
207         
208         # check to see if -f and -C are used together
209         for i in opts:
210                 myopts.append(i[0])
211                 
212         if ('-f' in myopts or '--file' in myopts) and ('-C' in myopts or '--cli' in myopts):
213                 print "!!! catalyst: please specify one of either -f or -C\n"
214                 usage()
215                 sys.exit(2)
216
217         for o, a in opts:
218                 if o in ("-h", "--help"):
219                         usage()
220                         sys.exit(1)
221                 
222                 if o in ("-v", "--version"):
223                         print "Catalyst version "+__version__
224                         sys.exit(1)
225
226                 if o in ("-d", "--debug"):
227                         if len(sys.argv) < 3:
228                                 print "!!! catalyst: please specify one of either -f or -C\n"
229                                 usage()
230                                 sys.exit(2)
231                         else:
232                                 conf_values["DEBUG"]="1"
233
234                 if o in ("-c", "--config"):
235                         if len(sys.argv) < 3:
236                                 print "!!! catalyst: please specify one of either -f or -C\n"
237                                 usage()
238                                 sys.exit(2)
239                         else:
240                                 myconfig=a
241
242                 if o in ("-C", "--cli"):
243                         x=sys.argv.index(o)+1
244                         while x < len(sys.argv):
245                                 mycmdline.append(sys.argv[x])
246                                 x=x+1
247                         
248                 if o in ("-f", "--file"):
249                         myspecfile=a
250                         
251                 if o in ("-V", "--verbose"):
252                         if len(sys.argv) < 3:
253                                 print "!!! catalyst: please specify one of either -f or -C\n"
254                                 usage()
255                                 sys.exit(2)
256                         else:
257                                 conf_values["VERBOSE"]="1"
258
259                 if o in ("-s", "--snapshot"):
260                         if len(sys.argv) < 3:
261                                 print "!!! catalyst: missing snapshot identifier\n"
262                                 usage()
263                                 sys.exit(2)
264                         else:
265                                 mycmdline.append("target=snapshot")
266                                 mycmdline.append("version_stamp="+a)
267         
268         # import configuration file and import our main module using those settings
269         parse_config(myconfig)
270         sys.path.append(conf_values["sharedir"]+"/modules")
271         from catalyst_support import *
272                 
273         # import the rest of the catalyst modules
274         targetmap=import_modules()
275
276         if myspecfile:
277                 addlargs=do_spec(myspecfile)
278         
279         if mycmdline:
280                 addlargs=do_cli(mycmdline)
281         
282         # everything is setup, so the build is a go
283         try:
284                 build_target(addlargs, targetmap)
285         except:
286                 print "!!! catalyst: could not complete build"
287                 sys.exit(2)