ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.172
Committed: Sun Jun 8 16:01:17 2008 UTC (16 years, 10 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.171: +1 -4 lines
Log Message:
fixed bug on already existing ui working dir check

File Contents

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