fixed usage() to reflect that -C must be the last option passed)
[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.41 2004/07/11 21:24:15 zhen Exp $
5
6 # Maintained in full by John Davis <zhen@gentoo.org>
7
8 import os,sys,imp,string,getopt,types
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...]"
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
25 def version():
26         print "Gentoo Catalyst, version "+__version__
27         print "Copyright 2003-2004 The Gentoo Foundation"
28         print "Distributed under the GNU General Public License version 2\n"
29         
30
31 def parse_config(myconfig):
32         # search a couple of different areas for the main config file
33         myconf={}
34         config_file=""
35
36         confdefaults={ "storedir":"/var/tmp/catalyst",\
37                 "sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",\
38                 "portdir":"/usr/portage","options":""}
39                 
40         # first, try the one passed (presumably from the cmdline)
41         if myconfig:
42                 if os.path.exists(myconfig):
43                         print "Using command line specified Catalyst configuration file, "+myconfig
44                         config_file=myconfig
45
46                 else:
47                         print "!!! catalyst: Could not use specified configuration file "+\
48                                 myconfig
49                         sys.exit(1)
50         
51         # next, try the default location
52         elif os.path.exists("/etc/catalyst/catalyst.conf"):
53                 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
54                 config_file="/etc/catalyst/catalyst.conf"
55         
56         # can't find a config file (we are screwed), so bail out
57         else:
58                 print "!!! catalyst: Could not find a suitable configuration file"
59                 sys.exit(1)
60
61         # now, try and parse the config file "config_file"
62         try:
63                 execfile(config_file, myconf, myconf)
64         
65         except:
66                 print "!!! catalyst: Unable to parse configuration file, "+myconfig
67                 sys.exit(1)
68         
69         # now, load up the values into conf_values so that we can use them
70         for x in confdefaults.keys():
71                 if myconf.has_key(x):
72                         print "Setting",x,"to config file value \""+myconf[x]+"\""
73                         conf_values[x]=myconf[x]
74                 else:
75                         print "Setting",x,"to default value \""+confdefaults[x]+"\""
76                         conf_values[x]=confdefaults[x]
77
78         # parse out the rest of the options from the config file
79         if "ccache" in string.split(conf_values["options"]):
80                 print "Compiler cache support enabled."
81                 conf_values["CCACHE"]="1"
82
83         if "pkgcache" in string.split(conf_values["options"]):
84                 print "Package cache support enabled."
85                 conf_values["PKGCACHE"]="1"
86         
87         if "distcc" in string.split(conf_values["options"]):
88                 print "Distcc support enabled."
89                 conf_values["DISTCC"]="1"
90
91         if "autoresume" in string.split(conf_values["options"]):
92                 print "Autoresuming support enabled."
93                 conf_values["AUTORESUME"]="1"
94
95         if myconf.has_key("envscript"):
96                 print "Envscript support enabled."
97                 conf_values["ENVSCRIPT"]=myconf["envscript"]
98
99 def import_modules():
100         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
101         targetmap={}
102         
103         try:
104                 for x in required_build_targets:
105                         try:
106                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
107                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
108                                 fh.close()
109                 
110                         except IOError:
111                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
112                                         conf_values.settings["sharedir"]+"/modules/"
113
114                 for x in valid_build_targets:
115                         try:
116                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
117                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
118                                 module.register(targetmap)
119                                 fh.close()
120                 
121                         except IOError:
122                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
123                                         conf_values.settings["sharedir"]+"/modules/"
124
125         except ImportError:
126                 print "!!! catalyst: Python modules not found in "+\
127                         conf_values["sharedir"]+"/modules; exiting."
128                 sys.exit(1)
129
130         return targetmap
131
132 def do_spec(myspecfile):
133         try:
134                 addlargs=read_spec(myspecfile)
135         except:
136                 sys.exit(1)
137                 
138         return addlargs
139
140 def do_cli(cmdline):
141         try:
142                 return arg_parse(cmdline)
143         
144         except CatalystError:
145                 print "!!! catalyst: Could not parse commandline, exiting."
146                 sys.exit(1)
147         
148 def build_target(addlargs, targetmap):
149         try:
150                 if not targetmap.has_key(addlargs["target"]):
151                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
152                 
153                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
154                 mytarget.run()
155
156         except CatalystError:
157                 sys.exit(1)
158         
159 if __name__ == "__main__":
160         targetmap={}
161         
162         version()
163         if os.getuid() != 0:
164                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
165                 print "!!! catalyst: This script requires root privileges to operate"
166                 sys.exit(2)
167
168         # we need some options in order to work correctly
169         if len(sys.argv) < 2:
170                 usage()
171                 sys.exit(2)
172
173         # parse out the command line arguments
174         try:
175                 opts,args = getopt.getopt(sys.argv[1:], "hvdc:C:f:V", ["help", "version", "debug",\
176                         "config=", "cli=", "file=", "verbose"])
177         
178         except getopt.GetoptError:
179                 usage()
180                 sys.exit(2)
181         
182         # defaults for commandline opts
183         debug=False
184         verbose=False
185         myconfig=""
186         myspecfile=""
187         mycmdline=[]
188
189         for o, a in opts:
190                 if o in ("-h", "--help"):
191                         usage()
192                         sys.exit(2)
193                 
194                 if o in ("-v", "--version"):
195                         print "Catalyst version "+__version__
196                         sys.exit(2)
197
198                 if o in ("-d", "--debug"):
199                         debug=True
200
201                 if o in ("-c", "--config"):
202                         myconfig=a
203
204                 if o in ("-C", "--cli"):
205                         x=sys.argv.index(o)+1
206                         while x < len(sys.argv):
207                                 mycmdline.append(sys.argv[x])
208                                 x=x+1
209                         
210                 if o in ("-f", "--file"):
211                         myspecfile=a
212                         
213                 if o in ("-V", "--verbose"):
214                         verbose=True
215         
216         # import configuration file and import our main module using those settings
217         parse_config(myconfig)
218         sys.path.append(conf_values["sharedir"]+"/modules")
219         from catalyst_support import *
220                 
221         # import the rest of the catalyst modules
222         targetmap=import_modules()
223
224         if myspecfile:
225                 addlargs=do_spec(myspecfile)
226         
227         if mycmdline:
228                 addlargs=do_cli(mycmdline)
229
230         # everything is setup, so the build is a go
231         try:
232                 build_target(addlargs, targetmap)
233         except:
234                 print "!!! catalyst: could not complete build"