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 |
|
|
def findLastWorkDir(dir_prefix, where = None):
|
79 |
|
|
|
80 |
|
|
if not where: where = os.getcwd() + '/'
|
81 |
|
|
# dir_prefix usually has the form 'crab_0_'
|
82 |
|
|
pattern = re.compile(dir_prefix)
|
83 |
|
|
|
84 |
|
|
file_list = []
|
85 |
|
|
for fl in os.listdir(where):
|
86 |
|
|
if pattern.match(fl):
|
87 |
|
|
file_list.append(fl)
|
88 |
|
|
pass
|
89 |
|
|
pass
|
90 |
|
|
|
91 |
|
|
if len(file_list) == 0: return None
|
92 |
|
|
|
93 |
|
|
file_list.sort()
|
94 |
|
|
|
95 |
|
|
wdir = where + file_list[len(file_list)-1]
|
96 |
|
|
return wdir
|
97 |
|
|
|
98 |
|
|
###########################################################################
|
99 |
|
|
def importName(module_name, name):
|
100 |
|
|
"""
|
101 |
|
|
Import a named object from a Python module,
|
102 |
|
|
i.e., it is an equivalent of 'from module_name import name'.
|
103 |
|
|
"""
|
104 |
|
|
module = __import__(module_name, globals(), locals(), [name])
|
105 |
|
|
return vars(module)[name]
|
106 |
|
|
|
107 |
|
|
###########################################################################
|
108 |
nsmirnov |
1.2 |
def runCommand(cmd):
|
109 |
nsmirnov |
1.1 |
"""
|
110 |
|
|
Run command 'cmd'.
|
111 |
|
|
Returns command stdoutput+stderror string on success,
|
112 |
|
|
or None if an error occurred.
|
113 |
|
|
"""
|
114 |
nsmirnov |
1.2 |
common.logger.debug(2, cmd)
|
115 |
nsmirnov |
1.1 |
#child = os.popen(cmd)
|
116 |
|
|
#(child,stdin) = popen2.popen4(cmd) # requires python2
|
117 |
|
|
|
118 |
|
|
child = popen2.Popen3(cmd,1)
|
119 |
|
|
err = child.wait()
|
120 |
|
|
cmd_out = child.fromchild.read()
|
121 |
|
|
cmd_err = child.childerr.read()
|
122 |
|
|
if err:
|
123 |
|
|
msg = ('`'+cmd+'`\n failed with exit code '
|
124 |
|
|
+`err`+'='+`(err&0xff)`+'(signal)+'
|
125 |
|
|
+`(err>>8)`+'(status)'+'\n')
|
126 |
|
|
msg += cmd_out
|
127 |
|
|
msg += cmd_err
|
128 |
nsmirnov |
1.2 |
common.logger.message(msg)
|
129 |
nsmirnov |
1.1 |
return None
|
130 |
|
|
|
131 |
|
|
cmd_out = cmd_out + cmd_err
|
132 |
nsmirnov |
1.2 |
common.logger.debug(2, cmd_out)
|
133 |
nsmirnov |
1.1 |
#err = child_stdout.close()
|
134 |
|
|
#if err:
|
135 |
|
|
# common.log.message('OUT`'+cmd+'`\n failed with exit code '
|
136 |
|
|
# +`err`+'='+`(err&0xff)`+'(signal)+'
|
137 |
|
|
# +`(err>>8)`+'(status)')
|
138 |
|
|
# return None
|
139 |
|
|
if cmd_out == '' : cmd_out = ' '
|
140 |
|
|
return cmd_out
|