ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab_util.py
Revision: 1.3
Committed: Thu Jun 23 16:53:25 2005 UTC (19 years, 10 months ago) by nsmirnov
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +38 -0 lines
Log Message:
Commands '-list', '-status', '-kill', '-retrieve' implemented

File Contents

# User Rev Content
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     def crabJobStatusToString(crab_status):
104     """
105     Convert one-letter crab job status into more readable string.
106     """
107     if crab_status == 'C': status = 'Created'
108     elif crab_status == 'S': status = 'Submitted'
109     elif crab_status == 'K': status = 'Killed'
110     elif crab_status == 'X': status = 'None'
111     elif crab_status == 'Y': status = 'Output retrieved'
112     else: status = '???'
113     return status
114    
115     ###########################################################################
116 nsmirnov 1.1 def findLastWorkDir(dir_prefix, where = None):
117    
118     if not where: where = os.getcwd() + '/'
119     # dir_prefix usually has the form 'crab_0_'
120     pattern = re.compile(dir_prefix)
121    
122     file_list = []
123     for fl in os.listdir(where):
124     if pattern.match(fl):
125     file_list.append(fl)
126     pass
127     pass
128    
129     if len(file_list) == 0: return None
130    
131     file_list.sort()
132    
133     wdir = where + file_list[len(file_list)-1]
134     return wdir
135    
136     ###########################################################################
137     def importName(module_name, name):
138     """
139     Import a named object from a Python module,
140     i.e., it is an equivalent of 'from module_name import name'.
141     """
142     module = __import__(module_name, globals(), locals(), [name])
143     return vars(module)[name]
144    
145     ###########################################################################
146 nsmirnov 1.2 def runCommand(cmd):
147 nsmirnov 1.1 """
148     Run command 'cmd'.
149     Returns command stdoutput+stderror string on success,
150     or None if an error occurred.
151     """
152 nsmirnov 1.2 common.logger.debug(2, cmd)
153 nsmirnov 1.1 #child = os.popen(cmd)
154     #(child,stdin) = popen2.popen4(cmd) # requires python2
155    
156     child = popen2.Popen3(cmd,1)
157     err = child.wait()
158     cmd_out = child.fromchild.read()
159     cmd_err = child.childerr.read()
160     if err:
161     msg = ('`'+cmd+'`\n failed with exit code '
162     +`err`+'='+`(err&0xff)`+'(signal)+'
163     +`(err>>8)`+'(status)'+'\n')
164     msg += cmd_out
165     msg += cmd_err
166 nsmirnov 1.2 common.logger.message(msg)
167 nsmirnov 1.1 return None
168    
169     cmd_out = cmd_out + cmd_err
170 nsmirnov 1.2 common.logger.debug(2, cmd_out)
171 nsmirnov 1.1 #err = child_stdout.close()
172     #if err:
173     # common.log.message('OUT`'+cmd+'`\n failed with exit code '
174     # +`err`+'='+`(err&0xff)`+'(signal)+'
175     # +`(err>>8)`+'(status)')
176     # return None
177     if cmd_out == '' : cmd_out = ' '
178     return cmd_out