ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.92
Committed: Mon Nov 20 14:09:21 2006 UTC (18 years, 5 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_1_4_1
Branch point for: branch_1_4_1
Changes since 1.91: +7 -3 lines
Log Message:
add filtering for RuntimeWarning

File Contents

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