large amount of changes - see ChangeLog
[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.28 2004/02/11 03:31:55 zhen Exp $
5
6 import os,sys,imp,string
7
8 def usage():
9         print "Usage: catalyst [-f file] [variable=value ...]"
10         print " -h --help               print this usage and exit"
11         print " -v --version            display version information"
12         print " -f --file               read specified file for build instructions"
13         print " variable=value          specify a variable/value pair"
14
15 def vers():
16         print "Gentoo catalyst, version "+version
17         print "Copyright 2003-2004 Gentoo Technologies, Inc."
18         print "Distributed under the GNU General Public License version 2"
19
20 version="1.0"
21
22 if len(sys.argv)==1 or sys.argv[1] in ["-h","--help"]:
23         usage()
24         sys.exit(1)
25 elif sys.argv[1] in ["-v","--version"]:
26         vers()
27         sys.exit(1)
28 if os.getuid()!=0:
29         #non-root callers can't go any further than here. 
30         print "catalyst: This script requires root privileges to operate."
31         sys.exit(1)
32
33 myspec={}
34 myconf={}
35 if os.path.exists("/etc/catalyst.conf"):
36         try:
37                 execfile("/etc/catalyst.conf",myconf,myconf)
38         except:
39                 print "catalyst: Unable to parse /etc/catalyst.conf config file (syntax error)"
40                 sys.exit(1)
41
42 confdefaults={ "storedir":"/var/tmp/catalyst","sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",
43 "portdir":"/usr/portage","options":"ccache"}
44
45 for x in confdefaults.keys():
46         if myconf.has_key(x):
47                 print "Setting",x,"to config file value \""+myconf[x]+"\""
48                 myspec[x]=myconf[x]
49         else:
50                 print "Setting",x,"to default value \""+confdefaults[x]+"\""
51                 myspec[x]=confdefaults[x]
52
53 #This allows plugins (and this code) to import modules in the /modules dir
54 sys.path.append(myspec["sharedir"]+"/modules")
55 try:
56         from catalyst_support import *
57         import targets
58 except ImportError:
59         print "catalyst: python modules not found in "+myspec["sharedir"]+"/modules; exiting."
60         sys.exit(1)
61
62 targetmap={}
63 targets.register(targetmap)
64
65 myspec["CCACHE"]="1"
66 print "Compiler cache support enabled."
67
68 if "pkgcache" in string.split(myspec["options"]):
69         print "Package cache support enabled."
70         myspec["PKGCACHE"]="1"
71
72 if "distcc" in string.split(myspec["options"]):
73         print "Distcc support enabled."
74         myspec["DISTCC"]="1"
75
76 if myconf.has_key("envscript"):
77         print "Envscript support enabled."
78         myspec["ENVSCRIPT"]=myconf["envscript"]
79         
80 if sys.argv[1] in ["-f", "--file" ]:
81         try:
82                 addlargs=read_spec(sys.argv[2])
83         except:
84                 raise CatalystError,"Unable to read spec file: "+sys.argv[2]
85         unparsedargs=sys.argv[3:]
86 else:
87         addlargs={}
88         unparsedargs=sys.argv[1:]
89 try:
90         arg_parse(myspec,addlargs,unparsedargs)
91         if not targetmap.has_key(myspec["target"]):
92                 raise CatalystError,"Target \""+myspec["target"]+"\" not available."
93         mytarget=targetmap[myspec["target"]](myspec,addlargs)
94         
95         mytarget.run()
96 except CatalystError:
97         sys.exit(1)