public
Description:
Homepage: http://www.funtoo.org
Clone URL: git://github.com/funtoo/metro.git
funtoo (author)
Tue Oct 13 08:31:32 -0700 2009
commit  66530b82aefb61b2c36f660081dee1b1b39a0344
tree    de26ecff82f50a5450a342d7cf3d99b8cbb84d80
parent  2353f2170c764983c5c3903690526afdcf808888
metro / metro
100755 219 lines (167 sloc) 6.119 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python -OO
 
import os,sys
 
__app__=os.path.basename(sys.argv[0])
 
import string,getopt,types
 
# we need this hard-coded path because we need a module here before we can parse our config file
__maintainer__="Daniel Robbins <drobbins@funtoo.org>"
 
__license__=""" Distributed under the terms of the GNU General Public License version 2
Metro comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to
redistribute it under certain conditions. See /usr/lib/metro/LICENSE for details.
"""
__status__="Release"
__version__="1.4.2"
 
def usage():
version()
print """ metro [OPTION]... [FILE]...
 
-h, --help Print this message
-V, --version Display version information
 
-d, --debug Enable debug mode
-v, --verbose Verbose mode
-l [dir], --libdir [dir] Use alternate library dir (default /usr/lib/metro)
 
-N, --notrigger Disable build triggers
 
-k [key], --key [key] Print value of [key], ie. "distfiles"
 
[FILE] File(s) to parse and evaluate
"""
 
def version():
print " "+__app__,"version",__version__
print
print " Copyright 2008-2009 Funtoo Technologies, LLC; Portions copyright 2003-2007 Gentoo Foundation"
print " Maintainer:",__maintainer__
print
print " Web: http://www.funtoo.org"
print " Project: http://github.com/funtoo/metro/wikis"
print
print __license__
 
def find_target(settings):
"""
 
Use the "metro/class" setting in our metadata to initialize the proper class defined in the modules/targets.py module.
 
The targets.__dict__ dictionary contains all the objects in the targets module. We look inside it to see if the class
defined in "metro/class" exists in there and is a class. If not, we raise an exception.
 
"""
if not targets.__dict__.has_key(settings["metro/class"]):
raise MetroError, "Metro class "+settings["metro/class"]+" not defined in modules/targets.py."
if type(targets.__dict__[settings["metro/class"]]) != types.ClassType:
raise MetroError, "Metro class "+settings["metro/class"]+" does not appear to be a class."
return targets.__dict__[settings["metro/class"]](settings)
 
debug=False
 
def initSettings(configfile,args,extraargs={}):
settings=flexdata.collection()
 
if os.path.exists(configfile):
settings.collect(configfile,None)
else:
print "Error: config file %s not found." % configfile
sys.exit(1)
 
# parse command-line supplied configuration files and spec files in the order they were specified
 
apos = 0
 
while apos < len(args):
if args[apos][-1] == ":":
# args such as: target/version: 2008.10.12 - parse both of these args
if apos+1 >= len(args):
raise MetroError, "Missing value argument for %s" % args[apos]
settings[args[apos][:-1]] = args[apos+1]
apos += 2
else:
raise MetroError, "cmdline argument '%s' invalid - does not end in a colon" % args[apos]
for arg in extraargs.keys():
settings[arg]=extraargs[arg]
settings.runCollector()
return settings
 
 
def main():
if os.getuid() != 0:
print __app__ + ": This script requires root privileges to operate."
sys.exit(2)
 
# we need some options in order to work correctly
if len(sys.argv) < 2:
usage()
sys.exit(2)
 
# parse out the command line arguments
try:
opts,args = getopt.getopt(sys.argv[1:], "dhvxVk:l:", ["debug","help", "verbose", "version","key=","libdir="])
except getopt.GetoptError:
usage()
sys.exit(2)
 
# defaults for commandline opts
verbose=False
myopts=[]
strict=True
 
# check preconditions
 
valueme=None
optdict={}
for opt,optarg in opts:
optdict[opt]=optarg
 
# Step 1: parse optional arguments that affect behavior of the program
 
if optdict.has_key("-d") or optdict.has_key("--debug"):
debug=True
if optdict.has_key("-v") or optdict.has_key("--verbose"):
verbose=True
 
binpath=os.path.abspath(sys.argv[0])
if os.path.islink(binpath):
binpath=os.readlink(binpath)
libdir=os.path.dirname(binpath)
if optdict.has_key("-l") or optdict.has_key("--libdir"):
if optdict.has_key("-l"):
libdir = optdict["-l"]
else:
libdir = optdict["--libdir"]
sys.stderr.write("Metro: Using library directory of %s.\n" % libdir)
sys.path.append(libdir+"/modules")
configfile = libdir+"/etc/metro.conf"
sys.stderr.write("Metro: Using main configuration file %s.\n" % configfile)
 
global flexdata
global targets
global MetroError
 
import flexdata
import targets
# for MetroError:
from catalyst_support import MetroError
 
# Step 2: check for "special" help/version options, handle them and exit:
 
if optdict.has_key("-h") or optdict.has_key("--help"):
usage()
sys.exit(1)
elif optdict.has_key("-V") or optdict.has_key("--version"):
version()
sys.exit(1)
 
 
# Step 3: check for duplicate main options, which is an error
 
mainargs=0
for x in [ "-k", "--key" ]:
if optdict.has_key(x):
mainargs += 1
if mainargs > 1:
print "Please specify only one -k or --key option at a time."
sys.exit(1)
 
# Step 4: parse main options
 
if optdict.has_key("-k") or optdict.has_key("--key"):
if optdict.has_key("-k"):
valueme=optdict["-k"]
else:
valueme=optdict["--key"]
 
# Step 5: Initialize Metro data:
 
settings = initSettings(configfile,args)
 
# Step 6: Create list of targets to run, checking whether "multi" mode is enabled
 
if valueme:
print settings[valueme]
sys.exit(1)
if settings.has_key("multi") and settings["multi"]=="yes":
targetlist=string.split(settings["multi/targets"])
# build automation mode
elif settings.has_key("target"):
targetlist=[settings["target"]]
else:
raise MetroError("Please specify a target or enable multi mode.")
for targetname in targetlist:
# Step 7a,b: Reinitialize settings, initialize target element to proper value:
 
print "Multi-mode: running target %s" % targetname
settings = initSettings(configfile,args,{"target":targetname})
 
# Step 7c: find and run target
 
target = find_target(settings)
print __app__+": running target \""+settings["target"]+"\"."
target.run()
print __app__+": complete."
 
try:
main()
except KeyboardInterrupt:
print "\n" + __app__ +" build aborted due to user interrupt (ctrl-C)"
print
print __app__,"aborting..."
raise
sys.exit(2)