ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.151
Committed: Mon Mar 3 20:29:45 2008 UTC (17 years, 2 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_2_0_pre1
Changes since 1.150: +82 -104 lines
Log Message:
removed jobDB and TaskDB usage. now works with sqlite

File Contents

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