1 |
nsmirnov |
1.1 |
###########################################################################
|
2 |
|
|
#
|
3 |
|
|
# C O N V E N I E N C E F U N C T I O N S
|
4 |
|
|
#
|
5 |
|
|
###########################################################################
|
6 |
|
|
|
7 |
|
|
import string, sys, os
|
8 |
|
|
import ConfigParser, re, popen2
|
9 |
|
|
|
10 |
nsmirnov |
1.2 |
import common
|
11 |
nsmirnov |
1.1 |
|
12 |
|
|
###########################################################################
|
13 |
|
|
def parseOptions(argv):
|
14 |
|
|
"""
|
15 |
|
|
Parses command-line options.
|
16 |
|
|
Returns a dictionary with specified options as keys:
|
17 |
|
|
-opt1 --> 'opt1' : None
|
18 |
|
|
-opt2 val --> 'opt2' : 'val'
|
19 |
|
|
-opt3=val --> 'opt3' : 'val'
|
20 |
|
|
Usually called as
|
21 |
|
|
options = parseOptions(sys.argv[1:])
|
22 |
|
|
"""
|
23 |
|
|
options = {}
|
24 |
|
|
argc = len(argv)
|
25 |
|
|
i = 0
|
26 |
|
|
while ( i < argc ):
|
27 |
|
|
if argv[i][0] != '-':
|
28 |
|
|
i = i + 1
|
29 |
|
|
continue
|
30 |
|
|
eq = string.find(argv[i], '=')
|
31 |
|
|
if eq > 0 :
|
32 |
|
|
opt = argv[i][:eq]
|
33 |
|
|
val = argv[i][eq+1:]
|
34 |
|
|
pass
|
35 |
|
|
else:
|
36 |
|
|
opt = argv[i]
|
37 |
|
|
val = None
|
38 |
|
|
if ( i+1 < argc and argv[i+1][0] != '-' ):
|
39 |
|
|
i = i + 1
|
40 |
|
|
val = argv[i]
|
41 |
|
|
pass
|
42 |
|
|
pass
|
43 |
|
|
options[opt] = val
|
44 |
|
|
i = i + 1
|
45 |
|
|
pass
|
46 |
|
|
return options
|
47 |
|
|
|
48 |
|
|
###########################################################################
|
49 |
|
|
def loadConfig(file):
|
50 |
|
|
"""
|
51 |
|
|
returns a dictionary with keys of the form
|
52 |
|
|
<section>.<option> and the corresponding values
|
53 |
|
|
"""
|
54 |
|
|
config={}
|
55 |
|
|
cp = ConfigParser.ConfigParser()
|
56 |
|
|
cp.read(file)
|
57 |
|
|
for sec in cp.sections():
|
58 |
|
|
# print 'Section',sec
|
59 |
|
|
for opt in cp.options(sec):
|
60 |
|
|
#print 'config['+sec+'.'+opt+'] = '+string.strip(cp.get(sec,opt))
|
61 |
|
|
config[sec+'.'+opt] = string.strip(cp.get(sec,opt))
|
62 |
|
|
return config
|
63 |
|
|
|
64 |
|
|
###########################################################################
|
65 |
|
|
def isInt(str):
|
66 |
|
|
""" Is the given string an integer ?"""
|
67 |
|
|
try: int(str)
|
68 |
|
|
except ValueError: return 0
|
69 |
|
|
return 1
|
70 |
|
|
|
71 |
|
|
###########################################################################
|
72 |
|
|
def isBool(str):
|
73 |
|
|
""" Is the given string 0 or 1 ?"""
|
74 |
|
|
if (str in ('0','1')): return 1
|
75 |
|
|
return 0
|
76 |
|
|
|
77 |
|
|
###########################################################################
|
78 |
nsmirnov |
1.3 |
def parseRange(range):
|
79 |
|
|
"""
|
80 |
|
|
Takes as the input a string with two integers separated by
|
81 |
|
|
the minus sign and returns the tuple with these numbers:
|
82 |
|
|
'n1-n2' -> (n1, n2)
|
83 |
|
|
'n1' -> (n1, n1)
|
84 |
|
|
"""
|
85 |
|
|
start = None
|
86 |
|
|
end = None
|
87 |
|
|
minus = string.find(range, '-')
|
88 |
|
|
if ( minus < 0 ):
|
89 |
|
|
if isInt(range):
|
90 |
|
|
start = int(range)
|
91 |
|
|
end = start
|
92 |
|
|
pass
|
93 |
|
|
pass
|
94 |
|
|
else:
|
95 |
|
|
if isInt(range[:minus]) and isInt(range[minus+1:]):
|
96 |
|
|
start = int(range[:minus])
|
97 |
|
|
end = int(range[minus+1:])
|
98 |
|
|
pass
|
99 |
|
|
pass
|
100 |
|
|
return (start, end)
|
101 |
|
|
|
102 |
|
|
###########################################################################
|
103 |
nsmirnov |
1.4 |
def parseRange2(range):
|
104 |
|
|
"""
|
105 |
|
|
Takes as the input a string in the form of a comma-separated
|
106 |
|
|
numbers and ranges
|
107 |
|
|
and returns a list with all specified numbers:
|
108 |
|
|
'n1' -> [n1]
|
109 |
|
|
'n1-n2' -> [n1, n1+1, ..., n2]
|
110 |
|
|
'n1,n2-n3,n4' -> [n1, n2, n2+1, ..., n3, n4]
|
111 |
|
|
"""
|
112 |
|
|
list = []
|
113 |
|
|
if not range: return list
|
114 |
|
|
|
115 |
|
|
comma = string.find(range, ',')
|
116 |
|
|
if comma == -1: left = range
|
117 |
|
|
else: left = range[:comma]
|
118 |
|
|
|
119 |
|
|
(n1, n2) = parseRange(left)
|
120 |
|
|
while ( n1 <= n2 ):
|
121 |
|
|
list.append(n1)
|
122 |
|
|
n1 += 1
|
123 |
|
|
pass
|
124 |
|
|
|
125 |
|
|
if comma != -1:
|
126 |
|
|
list.extend(parseRange2(range[comma+1:]))
|
127 |
|
|
pass
|
128 |
|
|
|
129 |
|
|
return list
|
130 |
|
|
|
131 |
|
|
###########################################################################
|
132 |
nsmirnov |
1.3 |
def crabJobStatusToString(crab_status):
|
133 |
|
|
"""
|
134 |
|
|
Convert one-letter crab job status into more readable string.
|
135 |
|
|
"""
|
136 |
|
|
if crab_status == 'C': status = 'Created'
|
137 |
slacapra |
1.6 |
elif crab_status == 'D': status = 'Done'
|
138 |
nsmirnov |
1.3 |
elif crab_status == 'S': status = 'Submitted'
|
139 |
|
|
elif crab_status == 'K': status = 'Killed'
|
140 |
|
|
elif crab_status == 'X': status = 'None'
|
141 |
|
|
elif crab_status == 'Y': status = 'Output retrieved'
|
142 |
slacapra |
1.5 |
elif crab_status == 'A': status = 'Aborted'
|
143 |
spiga |
1.7 |
elif crab_status == 'RC': status = 'ReCreated'
|
144 |
nsmirnov |
1.3 |
else: status = '???'
|
145 |
|
|
return status
|
146 |
|
|
|
147 |
|
|
###########################################################################
|
148 |
nsmirnov |
1.1 |
def findLastWorkDir(dir_prefix, where = None):
|
149 |
|
|
|
150 |
|
|
if not where: where = os.getcwd() + '/'
|
151 |
|
|
# dir_prefix usually has the form 'crab_0_'
|
152 |
|
|
pattern = re.compile(dir_prefix)
|
153 |
|
|
|
154 |
|
|
file_list = []
|
155 |
|
|
for fl in os.listdir(where):
|
156 |
|
|
if pattern.match(fl):
|
157 |
|
|
file_list.append(fl)
|
158 |
|
|
pass
|
159 |
|
|
pass
|
160 |
|
|
|
161 |
|
|
if len(file_list) == 0: return None
|
162 |
|
|
|
163 |
|
|
file_list.sort()
|
164 |
|
|
|
165 |
|
|
wdir = where + file_list[len(file_list)-1]
|
166 |
|
|
return wdir
|
167 |
|
|
|
168 |
|
|
###########################################################################
|
169 |
|
|
def importName(module_name, name):
|
170 |
|
|
"""
|
171 |
|
|
Import a named object from a Python module,
|
172 |
|
|
i.e., it is an equivalent of 'from module_name import name'.
|
173 |
|
|
"""
|
174 |
|
|
module = __import__(module_name, globals(), locals(), [name])
|
175 |
|
|
return vars(module)[name]
|
176 |
|
|
|
177 |
|
|
###########################################################################
|
178 |
nsmirnov |
1.2 |
def runCommand(cmd):
|
179 |
nsmirnov |
1.1 |
"""
|
180 |
|
|
Run command 'cmd'.
|
181 |
|
|
Returns command stdoutput+stderror string on success,
|
182 |
|
|
or None if an error occurred.
|
183 |
|
|
"""
|
184 |
nsmirnov |
1.2 |
common.logger.debug(2, cmd)
|
185 |
nsmirnov |
1.1 |
child = popen2.Popen3(cmd,1)
|
186 |
|
|
err = child.wait()
|
187 |
|
|
cmd_out = child.fromchild.read()
|
188 |
|
|
cmd_err = child.childerr.read()
|
189 |
fanzago |
1.8 |
if err :
|
190 |
nsmirnov |
1.1 |
msg = ('`'+cmd+'`\n failed with exit code '
|
191 |
|
|
+`err`+'='+`(err&0xff)`+'(signal)+'
|
192 |
|
|
+`(err>>8)`+'(status)'+'\n')
|
193 |
fanzago |
1.8 |
msg += 'cmd_out = ' + cmd_out
|
194 |
|
|
msg += 'cmd_err = ' + cmd_err
|
195 |
nsmirnov |
1.2 |
common.logger.message(msg)
|
196 |
nsmirnov |
1.1 |
return None
|
197 |
|
|
|
198 |
|
|
cmd_out = cmd_out + cmd_err
|
199 |
nsmirnov |
1.2 |
common.logger.debug(2, cmd_out)
|
200 |
fanzago |
1.8 |
|
201 |
nsmirnov |
1.1 |
if cmd_out == '' : cmd_out = ' '
|
202 |
|
|
return cmd_out
|
203 |
nsmirnov |
1.4 |
|
204 |
|
|
if __name__ == '__main__':
|
205 |
|
|
import sys
|
206 |
|
|
print 'sys.argv[1] =',sys.argv[1]
|
207 |
|
|
list = parseRange2(sys.argv[1])
|
208 |
|
|
print list
|
209 |
|
|
|