ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.80
Committed: Wed Sep 27 16:18:43 2006 UTC (18 years, 7 months ago) by gutsche
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_1_3_0_pre6
Changes since 1.79: +1 -9 lines
Log Message:
deactivate pre-parsing of job status before handing over list of jobs to status check in scheduler

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