1 |
slacapra |
1.9 |
#!/usr/bin/env python2.2
|
2 |
nsmirnov |
1.1 |
from crab_help import *
|
3 |
|
|
from crab_util import *
|
4 |
|
|
from crab_exceptions import *
|
5 |
|
|
from crab_logger import Logger
|
6 |
|
|
from WorkSpace import WorkSpace
|
7 |
|
|
from JobDB import JobDB
|
8 |
nsmirnov |
1.3 |
from JobList import JobList
|
9 |
nsmirnov |
1.1 |
from Creator import Creator
|
10 |
|
|
from Submitter import Submitter
|
11 |
slacapra |
1.8 |
from Checker import Checker
|
12 |
slacapra |
1.9 |
from PostMortem import PostMortem
|
13 |
|
|
from Status import Status
|
14 |
nsmirnov |
1.1 |
import common
|
15 |
|
|
|
16 |
|
|
import sys, os, time, string
|
17 |
|
|
|
18 |
|
|
###########################################################################
|
19 |
|
|
class Crab:
|
20 |
nsmirnov |
1.3 |
def __init__(self, opts):
|
21 |
nsmirnov |
1.1 |
|
22 |
|
|
# The order of main_actions is important !
|
23 |
slacapra |
1.9 |
self.main_actions = [ '-create', '-submit', '-monitor' ]
|
24 |
slacapra |
1.8 |
self.aux_actions = [ '-list', '-kill', '-status', '-getoutput',
|
25 |
slacapra |
1.9 |
'-resubmit' , '-cancelAndResubmit', '-check', '-postMortem', '-clean']
|
26 |
nsmirnov |
1.1 |
|
27 |
|
|
# Dictionary of actions, e.g. '-create' -> object of class Creator
|
28 |
|
|
self.actions = {}
|
29 |
|
|
|
30 |
|
|
# Configuration file
|
31 |
|
|
self.cfg_fname = None
|
32 |
|
|
# Dictionary with configuration parameters
|
33 |
|
|
self.cfg_params = {}
|
34 |
|
|
|
35 |
|
|
# Current working directory
|
36 |
|
|
self.cwd = os.getcwd()+'/'
|
37 |
|
|
# Current time in format 'yymmdd_hhmmss'
|
38 |
|
|
self.current_time = time.strftime('%y%m%d_%H%M%S',
|
39 |
|
|
time.localtime(time.time()))
|
40 |
|
|
|
41 |
nsmirnov |
1.3 |
# Session name (?) Do we need this ?
|
42 |
nsmirnov |
1.1 |
self.name = '0'
|
43 |
|
|
|
44 |
|
|
# Job type
|
45 |
|
|
self.job_type_name = None
|
46 |
|
|
|
47 |
|
|
# Continuation flag
|
48 |
|
|
self.flag_continue = 0
|
49 |
|
|
|
50 |
|
|
# quiet mode, i.e. no output on screen
|
51 |
|
|
self.flag_quiet = 0
|
52 |
|
|
# produce more output
|
53 |
|
|
self.debug_level = 0
|
54 |
|
|
|
55 |
nsmirnov |
1.3 |
# Scheduler name, e.g. 'edg', 'lsf'
|
56 |
nsmirnov |
1.1 |
self.scheduler_name = 'edg'
|
57 |
|
|
|
58 |
nsmirnov |
1.3 |
self.initialize_(opts)
|
59 |
nsmirnov |
1.1 |
|
60 |
|
|
return
|
61 |
|
|
|
62 |
nsmirnov |
1.7 |
def version():
|
63 |
nsmirnov |
1.1 |
return common.prog_version_str
|
64 |
|
|
|
65 |
nsmirnov |
1.7 |
version = staticmethod(version)
|
66 |
|
|
|
67 |
nsmirnov |
1.3 |
def initialize_(self, opts):
|
68 |
nsmirnov |
1.1 |
|
69 |
|
|
# Process the '-continue' option first because
|
70 |
|
|
# in the case of continuation the CRAB configuration
|
71 |
|
|
# parameters are loaded from already existing Working Space.
|
72 |
nsmirnov |
1.3 |
self.processContinueOption_(opts)
|
73 |
nsmirnov |
1.1 |
|
74 |
|
|
# Process ini-file first, then command line options
|
75 |
|
|
# because they override ini-file settings.
|
76 |
|
|
|
77 |
nsmirnov |
1.3 |
self.processIniFile_(opts)
|
78 |
nsmirnov |
1.1 |
|
79 |
nsmirnov |
1.3 |
if self.flag_continue: opts = self.loadConfiguration_(opts)
|
80 |
nsmirnov |
1.1 |
|
81 |
nsmirnov |
1.3 |
self.processOptions_(opts)
|
82 |
nsmirnov |
1.1 |
|
83 |
|
|
if not self.flag_continue:
|
84 |
nsmirnov |
1.3 |
self.createWorkingSpace_()
|
85 |
slacapra |
1.9 |
optsToBeSaved={}
|
86 |
|
|
for it in opts.keys():
|
87 |
|
|
if (it in self.main_actions) or (it in self.aux_actions) or (it == '-debug'):
|
88 |
|
|
pass
|
89 |
|
|
else:
|
90 |
|
|
optsToBeSaved[it]=opts[it]
|
91 |
|
|
common.work_space.saveConfiguration(optsToBeSaved, self.cfg_fname)
|
92 |
nsmirnov |
1.1 |
pass
|
93 |
|
|
|
94 |
|
|
# At this point all configuration options have been read.
|
95 |
|
|
|
96 |
|
|
args = string.join(sys.argv,' ')
|
97 |
nsmirnov |
1.3 |
self.updateHistory_(args)
|
98 |
|
|
self.createLogger_(args)
|
99 |
nsmirnov |
1.1 |
common.jobDB = JobDB()
|
100 |
nsmirnov |
1.3 |
if self.flag_continue:
|
101 |
|
|
common.jobDB.load()
|
102 |
|
|
common.logger.debug(6, str(common.jobDB))
|
103 |
|
|
pass
|
104 |
|
|
self.createScheduler_()
|
105 |
|
|
if common.logger.debugLevel() >= 6:
|
106 |
|
|
common.logger.debug(6, 'Used properties:')
|
107 |
|
|
keys = self.cfg_params.keys()
|
108 |
|
|
keys.sort()
|
109 |
|
|
for k in keys:
|
110 |
|
|
if self.cfg_params[k]:
|
111 |
|
|
common.logger.debug(6, ' '+k+' : '+self.cfg_params[k])
|
112 |
|
|
pass
|
113 |
|
|
else:
|
114 |
|
|
common.logger.debug(6, ' '+k+' : ')
|
115 |
|
|
pass
|
116 |
|
|
pass
|
117 |
|
|
common.logger.debug(6, 'End of used properties.\n')
|
118 |
|
|
pass
|
119 |
|
|
self.initializeActions_(opts)
|
120 |
nsmirnov |
1.1 |
return
|
121 |
|
|
|
122 |
nsmirnov |
1.3 |
def processContinueOption_(self,opts):
|
123 |
nsmirnov |
1.1 |
|
124 |
|
|
continue_dir = None
|
125 |
nsmirnov |
1.4 |
|
126 |
|
|
# Look for the '-continue' option.
|
127 |
|
|
|
128 |
nsmirnov |
1.1 |
for opt in opts.keys():
|
129 |
|
|
if ( opt in ('-continue','-c') ):
|
130 |
|
|
self.flag_continue = 1
|
131 |
|
|
val = opts[opt]
|
132 |
|
|
if val:
|
133 |
|
|
if val[0] == '/': continue_dir = val # abs path
|
134 |
|
|
else: continue_dir = self.cwd + val # rel path
|
135 |
|
|
pass
|
136 |
nsmirnov |
1.4 |
break
|
137 |
|
|
pass
|
138 |
|
|
|
139 |
|
|
# Look for actions which has sense only with '-continue'
|
140 |
|
|
|
141 |
|
|
if not self.flag_continue:
|
142 |
|
|
for opt in opts.keys():
|
143 |
slacapra |
1.6 |
if ( opt in (self.aux_actions) ):
|
144 |
nsmirnov |
1.4 |
self.flag_continue = 1
|
145 |
|
|
break
|
146 |
nsmirnov |
1.1 |
pass
|
147 |
|
|
pass
|
148 |
slacapra |
1.6 |
submit_flag=0
|
149 |
|
|
create_flag=0
|
150 |
|
|
for opt in opts.keys():
|
151 |
|
|
if opt == "-submit": submit_flag=1
|
152 |
|
|
if opt == "-create": create_flag=1
|
153 |
|
|
pass
|
154 |
|
|
if (submit_flag and not create_flag):
|
155 |
nsmirnov |
1.7 |
msg = "'-submit' must be used with either '-create' or '-continue'."
|
156 |
|
|
raise CrabException(msg)
|
157 |
slacapra |
1.6 |
pass
|
158 |
nsmirnov |
1.1 |
|
159 |
|
|
if not self.flag_continue: return
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
if not continue_dir:
|
163 |
|
|
prefix = common.prog_name + '_' + self.name + '_'
|
164 |
|
|
continue_dir = findLastWorkDir(prefix)
|
165 |
|
|
pass
|
166 |
|
|
|
167 |
|
|
if not continue_dir:
|
168 |
|
|
raise CrabException('Cannot find last working directory.')
|
169 |
|
|
|
170 |
|
|
if not os.path.exists(continue_dir):
|
171 |
|
|
msg = 'Cannot continue because the working directory <'
|
172 |
|
|
msg += continue_dir
|
173 |
|
|
msg += '> does not exist.'
|
174 |
|
|
raise CrabException(msg)
|
175 |
|
|
|
176 |
|
|
# Instantiate WorkSpace
|
177 |
|
|
common.work_space = WorkSpace(continue_dir)
|
178 |
|
|
|
179 |
|
|
return
|
180 |
|
|
|
181 |
nsmirnov |
1.3 |
def processIniFile_(self, opts):
|
182 |
nsmirnov |
1.1 |
"""
|
183 |
|
|
Processes a configuration INI-file.
|
184 |
|
|
"""
|
185 |
|
|
|
186 |
|
|
# Extract cfg-file name from the cmd-line options.
|
187 |
|
|
|
188 |
|
|
for opt in opts.keys():
|
189 |
|
|
if ( opt == '-cfg' ):
|
190 |
|
|
if self.flag_continue:
|
191 |
|
|
raise CrabException('-continue and -cfg cannot coexist.')
|
192 |
|
|
if opts[opt] : self.cfg_fname = opts[opt]
|
193 |
|
|
else : usage()
|
194 |
|
|
pass
|
195 |
|
|
|
196 |
|
|
elif ( opt == '-name' ):
|
197 |
|
|
self.name = opts[opt]
|
198 |
|
|
pass
|
199 |
|
|
|
200 |
|
|
pass
|
201 |
|
|
|
202 |
|
|
# Set default cfg-fname
|
203 |
|
|
|
204 |
|
|
if self.cfg_fname == None:
|
205 |
|
|
if self.flag_continue:
|
206 |
|
|
self.cfg_fname = common.work_space.cfgFileName()
|
207 |
|
|
else:
|
208 |
|
|
self.cfg_fname = common.prog_name+'.cfg'
|
209 |
|
|
pass
|
210 |
|
|
pass
|
211 |
|
|
|
212 |
|
|
# Load cfg-file
|
213 |
|
|
|
214 |
|
|
if string.lower(self.cfg_fname) != 'none':
|
215 |
|
|
if os.path.exists(self.cfg_fname):
|
216 |
|
|
self.cfg_params = loadConfig(self.cfg_fname)
|
217 |
|
|
pass
|
218 |
|
|
else:
|
219 |
|
|
msg = 'cfg-file '+self.cfg_fname+' not found.'
|
220 |
|
|
raise CrabException(msg)
|
221 |
|
|
pass
|
222 |
|
|
pass
|
223 |
|
|
|
224 |
|
|
# process the [CRAB] section
|
225 |
|
|
|
226 |
|
|
lhp = len('CRAB.')
|
227 |
|
|
for k in self.cfg_params.keys():
|
228 |
|
|
if len(k) >= lhp and k[:lhp] == 'CRAB.':
|
229 |
|
|
opt = '-'+k[lhp:]
|
230 |
|
|
if len(opt) >= 3 and opt[:3] == '-__': continue
|
231 |
|
|
if opt not in opts.keys():
|
232 |
|
|
opts[opt] = self.cfg_params[k]
|
233 |
|
|
pass
|
234 |
|
|
pass
|
235 |
|
|
pass
|
236 |
|
|
|
237 |
|
|
return
|
238 |
|
|
|
239 |
nsmirnov |
1.3 |
def processOptions_(self, opts):
|
240 |
nsmirnov |
1.1 |
"""
|
241 |
|
|
Processes the command-line options.
|
242 |
|
|
"""
|
243 |
|
|
|
244 |
|
|
for opt in opts.keys():
|
245 |
|
|
val = opts[opt]
|
246 |
|
|
|
247 |
nsmirnov |
1.3 |
# Skip actions, they are processed later in initializeActions_()
|
248 |
|
|
if opt in self.main_actions:
|
249 |
|
|
self.cfg_params['CRAB.'+opt[1:]] = val
|
250 |
|
|
continue
|
251 |
|
|
if opt in self.aux_actions:
|
252 |
|
|
self.cfg_params['CRAB.'+opt[1:]] = val
|
253 |
|
|
continue
|
254 |
nsmirnov |
1.1 |
|
255 |
|
|
|
256 |
|
|
elif ( opt == '-cfg' ):
|
257 |
|
|
pass
|
258 |
|
|
|
259 |
|
|
elif ( opt in ('-continue', '-c') ):
|
260 |
nsmirnov |
1.4 |
# Already processed in processContinueOption_()
|
261 |
nsmirnov |
1.1 |
pass
|
262 |
|
|
|
263 |
|
|
elif ( opt == '-jobtype' ):
|
264 |
|
|
if val : self.job_type_name = string.upper(val)
|
265 |
|
|
else : usage()
|
266 |
|
|
pass
|
267 |
|
|
|
268 |
|
|
elif ( opt == '-Q' ):
|
269 |
|
|
self.flag_quiet = 1
|
270 |
|
|
pass
|
271 |
|
|
|
272 |
|
|
elif ( opt == '-debug' ):
|
273 |
slacapra |
1.6 |
if val: self.debug_level = int(val)
|
274 |
|
|
else: self.debug_level = 1
|
275 |
nsmirnov |
1.1 |
pass
|
276 |
|
|
|
277 |
|
|
elif ( opt == '-scheduler' ):
|
278 |
|
|
if val: self.scheduler_name = val
|
279 |
|
|
else:
|
280 |
|
|
print common.prog_name+". No value for '-scheduler'."
|
281 |
|
|
usage()
|
282 |
|
|
pass
|
283 |
|
|
pass
|
284 |
|
|
|
285 |
nsmirnov |
1.3 |
elif string.find(opt,'.') == -1:
|
286 |
|
|
print common.prog_name+'. Unrecognized option '+opt
|
287 |
|
|
usage()
|
288 |
|
|
pass
|
289 |
nsmirnov |
1.1 |
|
290 |
nsmirnov |
1.3 |
# Override config parameters from INI-file with cmd-line params
|
291 |
|
|
if string.find(opt,'.') == -1 :
|
292 |
|
|
self.cfg_params['CRAB.'+opt[1:]] = val
|
293 |
nsmirnov |
1.1 |
pass
|
294 |
nsmirnov |
1.3 |
else:
|
295 |
nsmirnov |
1.1 |
# Command line parameters in the form -SECTION.ENTRY=VALUE
|
296 |
|
|
self.cfg_params[opt[1:]] = val
|
297 |
|
|
pass
|
298 |
|
|
pass
|
299 |
|
|
return
|
300 |
|
|
|
301 |
slacapra |
1.8 |
def parseRange_(self, aRange):
|
302 |
nsmirnov |
1.4 |
"""
|
303 |
slacapra |
1.8 |
Takes as the input a string with a range defined in any of the following
|
304 |
|
|
way, including combination, and return a tuple with the ints defined
|
305 |
|
|
according to following table. A consistency check is done.
|
306 |
|
|
NB: the first job is "1", not "0".
|
307 |
|
|
'all' -> [1,2,..., NJobs]
|
308 |
|
|
'' -> [1,2,..., NJobs]
|
309 |
|
|
'n1' -> [n1]
|
310 |
|
|
'n1-n2' -> [n1, n1+1, n1+2, ..., n2-1, n2]
|
311 |
|
|
'n1,n2' -> [n1, n2]
|
312 |
|
|
'n1,n2-n3' -> [n1, n2, n2+1, n2+2, ..., n3-1, n3]
|
313 |
|
|
"""
|
314 |
|
|
result = []
|
315 |
|
|
|
316 |
slacapra |
1.9 |
common.logger.debug(5,"parseRange_ "+str(aRange))
|
317 |
|
|
if aRange=='all' or aRange==None or aRange=='':
|
318 |
slacapra |
1.8 |
result=range(0,common.jobDB.nJobs())
|
319 |
|
|
return result
|
320 |
slacapra |
1.9 |
elif aRange=='0':
|
321 |
|
|
return result
|
322 |
slacapra |
1.8 |
|
323 |
|
|
subRanges = string.split(aRange, ',')
|
324 |
|
|
for subRange in subRanges:
|
325 |
|
|
result = result+self.parseSimpleRange_(subRange)
|
326 |
|
|
|
327 |
|
|
if self.checkUniqueness_(result):
|
328 |
|
|
return result
|
329 |
|
|
else:
|
330 |
|
|
print "Error ", result
|
331 |
|
|
return []
|
332 |
|
|
|
333 |
|
|
def checkUniqueness_(self, list):
|
334 |
|
|
"""
|
335 |
slacapra |
1.9 |
check if a list contains only unique elements
|
336 |
slacapra |
1.8 |
"""
|
337 |
|
|
|
338 |
|
|
uniqueList = []
|
339 |
|
|
# use a list comprehension statement (takes a while to understand)
|
340 |
|
|
|
341 |
|
|
[uniqueList.append(it) for it in list if not uniqueList.count(it)]
|
342 |
|
|
|
343 |
|
|
return (len(list)==len(uniqueList))
|
344 |
|
|
|
345 |
|
|
def parseSimpleRange_(self, aRange):
|
346 |
|
|
"""
|
347 |
|
|
Takes as the input a string with two integers separated by
|
348 |
|
|
the minus sign and returns the tuple with these numbers:
|
349 |
|
|
'n1-n2' -> [n1, n1+1, n1+2, ..., n2-1, n2]
|
350 |
|
|
'n1' -> [n1]
|
351 |
|
|
"""
|
352 |
|
|
(start, end) = (None, None)
|
353 |
|
|
|
354 |
|
|
result = []
|
355 |
|
|
minus = string.find(aRange, '-')
|
356 |
|
|
if ( minus < 0 ):
|
357 |
|
|
if isInt(aRange) and int(aRange)>0:
|
358 |
|
|
result.append(int(aRange)-1)
|
359 |
|
|
else:
|
360 |
slacapra |
1.9 |
common.logger.message("parseSimpleRange_ ERROR "+aRange)
|
361 |
nsmirnov |
1.4 |
pass
|
362 |
|
|
else:
|
363 |
slacapra |
1.8 |
(start, end) = string.split(aRange, '-')
|
364 |
|
|
if isInt(start) and isInt(end) and int(start)>0 and int(start)<int(end):
|
365 |
|
|
result=range(int(start)-1, int(end))
|
366 |
|
|
else:
|
367 |
|
|
print "ERROR ", start, end
|
368 |
|
|
|
369 |
|
|
return result
|
370 |
nsmirnov |
1.4 |
|
371 |
nsmirnov |
1.3 |
def initializeActions_(self, opts):
|
372 |
nsmirnov |
1.1 |
"""
|
373 |
|
|
For each user action instantiate a corresponding
|
374 |
|
|
object and put it in the action dictionary.
|
375 |
|
|
"""
|
376 |
|
|
|
377 |
|
|
for opt in opts.keys():
|
378 |
|
|
val = opts[opt]
|
379 |
|
|
|
380 |
|
|
if ( opt == '-create' ):
|
381 |
|
|
if val:
|
382 |
|
|
if ( isInt(val) ):
|
383 |
|
|
ncjobs = int(val)
|
384 |
|
|
elif ( val == 'all'):
|
385 |
|
|
ncjobs = val
|
386 |
|
|
else:
|
387 |
nsmirnov |
1.5 |
msg = 'Bad creation bunch size <'+str(val)+'>\n'
|
388 |
|
|
msg += ' Must be an integer or "all"'
|
389 |
slacapra |
1.8 |
msg += ' Generic range is not allowed"'
|
390 |
nsmirnov |
1.5 |
raise CrabException(msg)
|
391 |
nsmirnov |
1.1 |
pass
|
392 |
|
|
else: ncjobs = 'all'
|
393 |
|
|
|
394 |
|
|
if ncjobs != 0:
|
395 |
nsmirnov |
1.3 |
# Instantiate Creator object
|
396 |
nsmirnov |
1.1 |
creator = Creator(self.job_type_name,
|
397 |
|
|
self.cfg_params,
|
398 |
|
|
ncjobs)
|
399 |
|
|
self.actions[opt] = creator
|
400 |
nsmirnov |
1.3 |
|
401 |
|
|
# Initialize the JobDB object if needed
|
402 |
nsmirnov |
1.1 |
if not self.flag_continue:
|
403 |
|
|
common.jobDB.create(creator.nJobs())
|
404 |
|
|
pass
|
405 |
nsmirnov |
1.3 |
|
406 |
|
|
# Create and initialize JobList
|
407 |
|
|
|
408 |
|
|
common.job_list = JobList(common.jobDB.nJobs(),
|
409 |
|
|
creator.jobType())
|
410 |
|
|
|
411 |
|
|
common.job_list.setScriptNames(self.job_type_name+'.sh')
|
412 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
413 |
|
|
creator.jobType().setSteeringCardsNames()
|
414 |
slacapra |
1.9 |
|
415 |
|
|
creator.writeJobsSpecsToDB()
|
416 |
nsmirnov |
1.1 |
pass
|
417 |
nsmirnov |
1.3 |
pass
|
418 |
nsmirnov |
1.1 |
|
419 |
|
|
elif ( opt == '-submit' ):
|
420 |
nsmirnov |
1.7 |
nj_list = self.parseRange_(val)
|
421 |
nsmirnov |
1.5 |
|
422 |
|
|
if len(nj_list) != 0:
|
423 |
nsmirnov |
1.3 |
# Instantiate Submitter object
|
424 |
nsmirnov |
1.5 |
self.actions[opt] = Submitter(self.cfg_params, nj_list)
|
425 |
nsmirnov |
1.3 |
|
426 |
|
|
# Create and initialize JobList
|
427 |
|
|
|
428 |
|
|
if len(common.job_list) == 0 :
|
429 |
|
|
common.job_list = JobList(common.jobDB.nJobs(),
|
430 |
|
|
None)
|
431 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
432 |
|
|
pass
|
433 |
nsmirnov |
1.1 |
pass
|
434 |
nsmirnov |
1.3 |
pass
|
435 |
nsmirnov |
1.1 |
|
436 |
nsmirnov |
1.4 |
elif ( opt == '-list' ):
|
437 |
slacapra |
1.8 |
jobs = self.parseRange_(val)
|
438 |
|
|
|
439 |
|
|
common.jobDB.dump(jobs)
|
440 |
nsmirnov |
1.4 |
pass
|
441 |
|
|
|
442 |
|
|
elif ( opt == '-status' ):
|
443 |
slacapra |
1.8 |
jobs = self.parseRange_(val)
|
444 |
|
|
|
445 |
slacapra |
1.9 |
if len(jobs) != 0:
|
446 |
|
|
# Instantiate Submitter object
|
447 |
|
|
self.actions[opt] = Status(self.cfg_params, jobs)
|
448 |
nsmirnov |
1.1 |
pass
|
449 |
|
|
pass
|
450 |
|
|
|
451 |
nsmirnov |
1.4 |
elif ( opt == '-kill' ):
|
452 |
slacapra |
1.9 |
if val:
|
453 |
|
|
jobs = self.parseRange_(val)
|
454 |
slacapra |
1.8 |
|
455 |
slacapra |
1.9 |
for nj in jobs:
|
456 |
|
|
st = common.jobDB.status(nj)
|
457 |
|
|
if st == 'S':
|
458 |
|
|
jid = common.jobDB.jobId(nj)
|
459 |
|
|
common.logger.message("Killing job # "+`(nj+1)`)
|
460 |
|
|
common.scheduler.cancel(jid)
|
461 |
|
|
common.jobDB.setStatus(nj, 'K')
|
462 |
|
|
pass
|
463 |
nsmirnov |
1.4 |
pass
|
464 |
slacapra |
1.9 |
|
465 |
|
|
common.jobDB.save()
|
466 |
nsmirnov |
1.1 |
pass
|
467 |
slacapra |
1.9 |
else:
|
468 |
|
|
common.logger.message("Warning: with '-kill' you _MUST_ specify a job range or 'all'")
|
469 |
nsmirnov |
1.1 |
|
470 |
slacapra |
1.8 |
elif ( opt == '-getoutput' ):
|
471 |
|
|
jobs = self.parseRange_(val)
|
472 |
|
|
|
473 |
|
|
for nj in jobs:
|
474 |
nsmirnov |
1.4 |
st = common.jobDB.status(nj)
|
475 |
|
|
if st == 'S':
|
476 |
|
|
jid = common.jobDB.jobId(nj)
|
477 |
slacapra |
1.9 |
currStatus = common.scheduler.queryStatus(jid)
|
478 |
|
|
if currStatus=="Done":
|
479 |
|
|
dir = common.scheduler.getOutput(jid)
|
480 |
|
|
common.jobDB.setStatus(nj, 'Y')
|
481 |
|
|
|
482 |
|
|
# Rename the directory with results to smth readable
|
483 |
|
|
new_dir = common.work_space.resDir()
|
484 |
|
|
try:
|
485 |
|
|
files = os.listdir(dir)
|
486 |
|
|
for file in files:
|
487 |
|
|
os.rename(dir+'/'+file, new_dir+'/'+file)
|
488 |
|
|
os.rmdir(dir)
|
489 |
|
|
except OSError, e:
|
490 |
|
|
msg = 'rename files from '+dir+' to '+new_dir+' error: '
|
491 |
|
|
msg += str(e)
|
492 |
|
|
common.logger.message(msg)
|
493 |
|
|
# ignore error
|
494 |
|
|
pass
|
495 |
|
|
|
496 |
nsmirnov |
1.4 |
|
497 |
slacapra |
1.9 |
msg = 'Results of Job # '+`(nj+1)`+' are in '+new_dir
|
498 |
|
|
common.logger.message(msg)
|
499 |
|
|
else:
|
500 |
|
|
msg = 'Job # '+`(nj+1)`+' submitted but still status '+currStatus+' not possible to get output'
|
501 |
nsmirnov |
1.4 |
common.logger.message(msg)
|
502 |
|
|
pass
|
503 |
slacapra |
1.8 |
else:
|
504 |
slacapra |
1.9 |
common.logger.message('Jobs #'+`(nj+1)`+' has status '+st+' not possible to get output')
|
505 |
nsmirnov |
1.4 |
pass
|
506 |
|
|
|
507 |
|
|
common.jobDB.save()
|
508 |
|
|
pass
|
509 |
|
|
|
510 |
|
|
elif ( opt == '-resubmit' ):
|
511 |
slacapra |
1.8 |
jobs = self.parseRange_(val)
|
512 |
|
|
|
513 |
|
|
# create a list of jobs to be resubmitted.
|
514 |
|
|
|
515 |
|
|
nj_list = []
|
516 |
|
|
for nj in jobs:
|
517 |
|
|
st = common.jobDB.status(nj)
|
518 |
|
|
|
519 |
slacapra |
1.9 |
if st in ['C','K','A']:
|
520 |
|
|
nj_list.append(nj)
|
521 |
|
|
elif st == 'Y':
|
522 |
|
|
# here I would like to move the old output to a new place
|
523 |
|
|
# I would need the name of the output files.
|
524 |
|
|
# these infos are in the jobType class, which is not accessible from here!
|
525 |
|
|
outSandbox = common.jobDB.outputSandbox(nj)
|
526 |
|
|
|
527 |
|
|
resDir = common.work_space.resDir()
|
528 |
|
|
resDirSave = resDir + self.current_time
|
529 |
|
|
os.mkdir(resDirSave)
|
530 |
|
|
|
531 |
|
|
for file in outSandbox:
|
532 |
|
|
if os.path.exists(resDir+'/'+file):
|
533 |
|
|
os.rename(resDir+'/'+file, resDirSave+'/'+file)
|
534 |
|
|
common.logger.message('Output file '+file+' moved to '+resDirSave)
|
535 |
|
|
pass
|
536 |
|
|
|
537 |
|
|
|
538 |
|
|
else:
|
539 |
|
|
common.logger.message('Job #'+str(nj+1)+' has status '+crabJobStatusToString(st)+' must be "killed" before resubmission')
|
540 |
slacapra |
1.8 |
pass
|
541 |
slacapra |
1.9 |
raise CrabException('resubmit')
|
542 |
slacapra |
1.8 |
|
543 |
|
|
if len(nj_list) != 0:
|
544 |
|
|
# Instantiate Submitter object
|
545 |
|
|
self.actions[opt] = Submitter(self.cfg_params, nj_list)
|
546 |
|
|
|
547 |
|
|
# Create and initialize JobList
|
548 |
|
|
|
549 |
|
|
if len(common.job_list) == 0 :
|
550 |
|
|
common.job_list = JobList(common.jobDB.nJobs(),
|
551 |
|
|
None)
|
552 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
553 |
|
|
pass
|
554 |
|
|
pass
|
555 |
|
|
pass
|
556 |
|
|
|
557 |
|
|
elif ( opt == '-cancelAndResubmit' ):
|
558 |
|
|
jobs = self.parseRange_(val)
|
559 |
nsmirnov |
1.5 |
|
560 |
nsmirnov |
1.7 |
# Cancel submitted jobs.
|
561 |
nsmirnov |
1.5 |
|
562 |
slacapra |
1.8 |
nj_list = []
|
563 |
|
|
for nj in jobs:
|
564 |
nsmirnov |
1.5 |
st = common.jobDB.status(nj)
|
565 |
|
|
if st == 'S':
|
566 |
|
|
jid = common.jobDB.jobId(nj)
|
567 |
|
|
common.scheduler.cancel(jid)
|
568 |
|
|
st = 'K'
|
569 |
|
|
common.jobDB.setStatus(nj, st)
|
570 |
|
|
pass
|
571 |
nsmirnov |
1.7 |
pass
|
572 |
nsmirnov |
1.5 |
|
573 |
slacapra |
1.8 |
if st != 'X': nj_list.append(nj)
|
574 |
|
|
pass
|
575 |
nsmirnov |
1.5 |
|
576 |
|
|
if len(nj_list) != 0:
|
577 |
|
|
# Instantiate Submitter object
|
578 |
|
|
self.actions[opt] = Submitter(self.cfg_params, nj_list)
|
579 |
|
|
|
580 |
|
|
# Create and initialize JobList
|
581 |
|
|
|
582 |
|
|
if len(common.job_list) == 0 :
|
583 |
|
|
common.job_list = JobList(common.jobDB.nJobs(),
|
584 |
|
|
None)
|
585 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
586 |
|
|
pass
|
587 |
|
|
pass
|
588 |
nsmirnov |
1.4 |
pass
|
589 |
|
|
|
590 |
slacapra |
1.8 |
elif ( opt == '-check' ):
|
591 |
|
|
jobs = self.parseRange_(val)
|
592 |
|
|
nj_list = []
|
593 |
|
|
for nj in jobs:
|
594 |
|
|
st = common.jobDB.status(nj)
|
595 |
|
|
if st != 'C': nj_list.append(nj)
|
596 |
|
|
pass
|
597 |
|
|
|
598 |
|
|
if len(nj_list) != 0:
|
599 |
|
|
# Instantiate Submitter object
|
600 |
|
|
self.actions[opt] = Checker(self.cfg_params, nj_list)
|
601 |
|
|
|
602 |
|
|
# Create and initialize JobList
|
603 |
|
|
|
604 |
|
|
if len(common.job_list) == 0 :
|
605 |
|
|
common.job_list = JobList(common.jobDB.nJobs(), None)
|
606 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
607 |
|
|
pass
|
608 |
|
|
pass
|
609 |
|
|
|
610 |
slacapra |
1.9 |
elif ( opt == '-postMortem' ):
|
611 |
|
|
jobs = self.parseRange_(val)
|
612 |
|
|
nj_list = []
|
613 |
|
|
for nj in jobs:
|
614 |
|
|
st = common.jobDB.status(nj)
|
615 |
|
|
if st in ['Y','A']: nj_list.append(nj)
|
616 |
|
|
pass
|
617 |
|
|
|
618 |
|
|
if len(nj_list) != 0:
|
619 |
|
|
# Instantiate Submitter object
|
620 |
|
|
self.actions[opt] = PostMortem(self.cfg_params, nj_list)
|
621 |
|
|
|
622 |
|
|
# Create and initialize JobList
|
623 |
|
|
|
624 |
|
|
if len(common.job_list) == 0 :
|
625 |
|
|
common.job_list = JobList(common.jobDB.nJobs(), None)
|
626 |
|
|
common.job_list.setJDLNames(self.job_type_name+'.jdl')
|
627 |
|
|
pass
|
628 |
|
|
pass
|
629 |
|
|
|
630 |
|
|
elif ( opt == '-clean' ):
|
631 |
|
|
if val != None:
|
632 |
|
|
raise CrabException("No range allowed for '-clean'")
|
633 |
|
|
|
634 |
|
|
submittedJobs=0
|
635 |
|
|
for nj in range(0,common.jobDB.nJobs()):
|
636 |
|
|
st = common.jobDB.status(nj)
|
637 |
|
|
if st == 'S':
|
638 |
|
|
submittedJobs = submittedJobs+1
|
639 |
nsmirnov |
1.1 |
pass
|
640 |
slacapra |
1.9 |
|
641 |
|
|
if submittedJobs:
|
642 |
|
|
raise CrabException("There are still "+str(submittedJobs)+" jobs submitted. Kill them before '-clean'")
|
643 |
|
|
|
644 |
|
|
msg = 'directory '+common.work_space.topDir()+' removed'
|
645 |
|
|
common.work_space.delete()
|
646 |
|
|
common.logger.message(msg)
|
647 |
|
|
|
648 |
nsmirnov |
1.1 |
pass
|
649 |
slacapra |
1.9 |
|
650 |
nsmirnov |
1.1 |
pass
|
651 |
|
|
return
|
652 |
|
|
|
653 |
nsmirnov |
1.3 |
def createWorkingSpace_(self):
|
654 |
slacapra |
1.9 |
new_dir = ''
|
655 |
|
|
|
656 |
|
|
try:
|
657 |
|
|
new_dir = self.cfg_params['USER.ui_working_dir']
|
658 |
|
|
except KeyError:
|
659 |
|
|
new_dir = common.prog_name + '_' + self.name + '_' + self.current_time
|
660 |
|
|
new_dir = self.cwd + new_dir
|
661 |
|
|
pass
|
662 |
|
|
print new_dir
|
663 |
|
|
|
664 |
nsmirnov |
1.1 |
common.work_space = WorkSpace(new_dir)
|
665 |
|
|
common.work_space.create()
|
666 |
|
|
return
|
667 |
|
|
|
668 |
nsmirnov |
1.3 |
def loadConfiguration_(self, opts):
|
669 |
nsmirnov |
1.1 |
|
670 |
|
|
save_opts = common.work_space.loadSavedOptions()
|
671 |
|
|
|
672 |
|
|
# Override saved options with new command-line options
|
673 |
|
|
|
674 |
|
|
for k in opts.keys():
|
675 |
|
|
save_opts[k] = opts[k]
|
676 |
|
|
pass
|
677 |
|
|
|
678 |
|
|
# Return updated options
|
679 |
|
|
return save_opts
|
680 |
|
|
|
681 |
nsmirnov |
1.3 |
def createLogger_(self, args):
|
682 |
nsmirnov |
1.1 |
|
683 |
|
|
log = Logger()
|
684 |
|
|
log.quiet(self.flag_quiet)
|
685 |
|
|
log.setDebugLevel(self.debug_level)
|
686 |
|
|
log.write(args+'\n')
|
687 |
nsmirnov |
1.3 |
log.message(self.headerString_())
|
688 |
nsmirnov |
1.1 |
log.flush()
|
689 |
|
|
common.logger = log
|
690 |
|
|
return
|
691 |
|
|
|
692 |
nsmirnov |
1.3 |
def updateHistory_(self, args):
|
693 |
nsmirnov |
1.1 |
history_fname = common.prog_name+'.history'
|
694 |
|
|
history_file = open(history_fname, 'a')
|
695 |
|
|
history_file.write(self.current_time+': '+args+'\n')
|
696 |
|
|
history_file.close()
|
697 |
|
|
return
|
698 |
|
|
|
699 |
nsmirnov |
1.3 |
def headerString_(self):
|
700 |
nsmirnov |
1.1 |
"""
|
701 |
|
|
Creates a string describing program options either given in
|
702 |
|
|
the command line or their default values.
|
703 |
|
|
"""
|
704 |
|
|
header = common.prog_name + ' (version ' + common.prog_version_str + \
|
705 |
|
|
') running on ' + \
|
706 |
|
|
time.ctime(time.time())+'\n\n' + \
|
707 |
|
|
common.prog_name+'. Working options:\n'
|
708 |
|
|
header = header +\
|
709 |
|
|
' scheduler ' + self.scheduler_name + '\n'+\
|
710 |
|
|
' job type ' + self.job_type_name + '\n'+\
|
711 |
|
|
' working directory ' + common.work_space.topDir()\
|
712 |
|
|
+ '\n'
|
713 |
|
|
return header
|
714 |
|
|
|
715 |
nsmirnov |
1.3 |
def createScheduler_(self):
|
716 |
nsmirnov |
1.1 |
"""
|
717 |
|
|
Creates a scheduler object instantiated by its name.
|
718 |
|
|
"""
|
719 |
|
|
klass_name = 'Scheduler' + string.capitalize(self.scheduler_name)
|
720 |
|
|
file_name = klass_name
|
721 |
|
|
try:
|
722 |
|
|
klass = importName(file_name, klass_name)
|
723 |
|
|
except KeyError:
|
724 |
|
|
msg = 'No `class '+klass_name+'` found in file `'+file_name+'.py`'
|
725 |
|
|
raise CrabException(msg)
|
726 |
|
|
except ImportError, e:
|
727 |
|
|
msg = 'Cannot create scheduler '+self.scheduler_name
|
728 |
|
|
msg += ' (file: '+file_name+', class '+klass_name+'):\n'
|
729 |
|
|
msg += str(e)
|
730 |
|
|
raise CrabException(msg)
|
731 |
|
|
|
732 |
|
|
common.scheduler = klass()
|
733 |
|
|
common.scheduler.configure(self.cfg_params)
|
734 |
|
|
return
|
735 |
|
|
|
736 |
|
|
def run(self):
|
737 |
|
|
"""
|
738 |
|
|
For each
|
739 |
|
|
"""
|
740 |
|
|
|
741 |
|
|
for act in self.main_actions:
|
742 |
|
|
if act in self.actions.keys(): self.actions[act].run()
|
743 |
|
|
pass
|
744 |
|
|
|
745 |
|
|
for act in self.aux_actions:
|
746 |
|
|
if act in self.actions.keys(): self.actions[act].run()
|
747 |
|
|
pass
|
748 |
|
|
return
|
749 |
|
|
|
750 |
|
|
###########################################################################
|
751 |
|
|
def processHelpOptions(opts):
|
752 |
|
|
|
753 |
|
|
for opt in opts.keys():
|
754 |
|
|
if opt == '-v':
|
755 |
nsmirnov |
1.7 |
print Crab.version()
|
756 |
nsmirnov |
1.1 |
return 1
|
757 |
|
|
if opt in ('-h','-help','--help') :
|
758 |
|
|
if opts[opt] : help(opts[opt])
|
759 |
|
|
else: help()
|
760 |
|
|
return 1
|
761 |
|
|
|
762 |
|
|
return 0
|
763 |
|
|
|
764 |
|
|
###########################################################################
|
765 |
|
|
if __name__ == '__main__':
|
766 |
|
|
|
767 |
|
|
# Parse command-line options and create a dictionary with
|
768 |
|
|
# key-value pairs.
|
769 |
|
|
|
770 |
|
|
options = parseOptions(sys.argv[1:])
|
771 |
|
|
|
772 |
|
|
# Process "help" options, such as '-help', '-version'
|
773 |
|
|
|
774 |
|
|
if processHelpOptions(options): sys.exit(0)
|
775 |
|
|
|
776 |
|
|
# Create, initialize, and run a Crab object
|
777 |
|
|
|
778 |
|
|
try:
|
779 |
nsmirnov |
1.3 |
crab = Crab(options)
|
780 |
nsmirnov |
1.1 |
crab.run()
|
781 |
|
|
except CrabException, e:
|
782 |
|
|
print '\n' + common.prog_name + ': ' + str(e) + '\n'
|
783 |
|
|
if common.logger:
|
784 |
|
|
common.logger.write('ERROR: '+str(e)+'\n')
|
785 |
|
|
pass
|
786 |
|
|
pass
|
787 |
|
|
|
788 |
|
|
pass
|