missing "/" and new clst_conf support
[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.29 2004/02/12 19:51:37 drobbins 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 myconffile="/etc/catalyst.conf"
36 if os.environ.has_key("clst_conf"):
37         if not os.path.exists(os.environ["clst_conf"]):
38                 print "catalyst: Unable to find config file specified in env var 'clst_conf'"
39                 sys.exit(1)
40         myconffile=os.environ["clst_conf"]
41         
42 if os.path.exists(myconffile):
43         try:
44                 execfile(myconffile,myconf,myconf)
45         except:
46                 print "catalyst: Unable to parse "+myconffile+" config file (syntax error)"
47                 sys.exit(1)
48
49 confdefaults={ "storedir":"/var/tmp/catalyst","sharedir":"/usr/share/catalyst","distdir":"/usr/portage/distfiles",
50 "portdir":"/usr/portage","options":"ccache"}
51
52 for x in confdefaults.keys():
53         if myconf.has_key(x):
54                 print "Setting",x,"to config file value \""+myconf[x]+"\""
55                 myspec[x]=myconf[x]
56         else:
57                 print "Setting",x,"to default value \""+confdefaults[x]+"\""
58                 myspec[x]=confdefaults[x]
59
60 #This allows plugins (and this code) to import modules in the /modules dir
61 sys.path.append(myspec["sharedir"]+"/modules")
62 try:
63         from catalyst_support import *
64         import targets
65 except ImportError:
66         print "catalyst: python modules not found in "+myspec["sharedir"]+"/modules; exiting."
67         sys.exit(1)
68
69 targetmap={}
70 targets.register(targetmap)
71
72 myspec["CCACHE"]="1"
73 print "Compiler cache support enabled."
74
75 if "pkgcache" in string.split(myspec["options"]):
76         print "Package cache support enabled."
77         myspec["PKGCACHE"]="1"
78
79 if "distcc" in string.split(myspec["options"]):
80         print "Distcc support enabled."
81         myspec["DISTCC"]="1"
82
83 if myconf.has_key("envscript"):
84         print "Envscript support enabled."
85         myspec["ENVSCRIPT"]=myconf["envscript"]
86         
87 if sys.argv[1] in ["-f", "--file" ]:
88         try:
89                 addlargs=read_spec(sys.argv[2])
90         except:
91                 raise CatalystError,"Unable to read spec file: "+sys.argv[2]
92         unparsedargs=sys.argv[3:]
93 else:
94         addlargs={}
95         unparsedargs=sys.argv[1:]
96 try:
97         arg_parse(myspec,addlargs,unparsedargs)
98         if not targetmap.has_key(myspec["target"]):
99                 raise CatalystError,"Target \""+myspec["target"]+"\" not available."
100         mytarget=targetmap[myspec["target"]](myspec,addlargs)
101         
102         mytarget.run()
103 except CatalystError:
104         sys.exit(1)