ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.175
Committed: Tue Jun 10 14:00:41 2008 UTC (16 years, 10 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_3_0_pre6, CRAB_2_3_0_pre1, CRAB_2_2_2_pre5
Changes since 1.174: +0 -1 lines
Log Message:
remove unused os.environ

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 mcinquil 1.173 '-copyLocal', '-renewProxy' ]
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     pass
230     else:
231     msg = 'cfg-file '+self.cfg_fname+' not found.'
232     raise CrabException(msg)
233     pass
234     pass
235    
236     # process the [CRAB] section
237    
238     lhp = len('CRAB.')
239     for k in self.cfg_params.keys():
240     if len(k) >= lhp and k[:lhp] == 'CRAB.':
241     opt = '-'+k[lhp:]
242     if len(opt) >= 3 and opt[:3] == '-__': continue
243     if opt not in opts.keys():
244     opts[opt] = self.cfg_params[k]
245     pass
246     pass
247     pass
248    
249     return
250    
251 nsmirnov 1.3 def processOptions_(self, opts):
252 nsmirnov 1.1 """
253     Processes the command-line options.
254     """
255    
256     for opt in opts.keys():
257     val = opts[opt]
258    
259 nsmirnov 1.3 # Skip actions, they are processed later in initializeActions_()
260     if opt in self.main_actions:
261     self.cfg_params['CRAB.'+opt[1:]] = val
262     continue
263     if opt in self.aux_actions:
264     self.cfg_params['CRAB.'+opt[1:]] = val
265     continue
266 ewv 1.169
267 spiga 1.107 elif ( opt == '-server_name' ):
268 farinafa 1.154 # TODO
269 spiga 1.107 pass
270 nsmirnov 1.1
271     elif ( opt == '-cfg' ):
272     pass
273    
274     elif ( opt in ('-continue', '-c') ):
275 nsmirnov 1.4 # Already processed in processContinueOption_()
276 nsmirnov 1.1 pass
277    
278     elif ( opt == '-jobtype' ):
279     if val : self.job_type_name = string.upper(val)
280 slacapra 1.144 else : processHelpOptions()
281 nsmirnov 1.1 pass
282    
283     elif ( opt == '-Q' ):
284     self.flag_quiet = 1
285     pass
286    
287     elif ( opt == '-debug' ):
288 slacapra 1.6 if val: self.debug_level = int(val)
289     else: self.debug_level = 1
290 nsmirnov 1.1 pass
291    
292     elif ( opt == '-scheduler' ):
293     pass
294 slacapra 1.22
295 nsmirnov 1.3 elif string.find(opt,'.') == -1:
296     print common.prog_name+'. Unrecognized option '+opt
297 slacapra 1.144 processHelpOptions()
298 nsmirnov 1.3 pass
299 nsmirnov 1.1
300 nsmirnov 1.3 # Override config parameters from INI-file with cmd-line params
301     if string.find(opt,'.') == -1 :
302     self.cfg_params['CRAB.'+opt[1:]] = val
303 nsmirnov 1.1 pass
304 nsmirnov 1.3 else:
305 nsmirnov 1.1 # Command line parameters in the form -SECTION.ENTRY=VALUE
306     self.cfg_params[opt[1:]] = val
307     pass
308     pass
309     return
310    
311 slacapra 1.8 def parseRange_(self, aRange):
312 nsmirnov 1.4 """
313 slacapra 1.8 Takes as the input a string with a range defined in any of the following
314     way, including combination, and return a tuple with the ints defined
315     according to following table. A consistency check is done.
316     NB: the first job is "1", not "0".
317     'all' -> [1,2,..., NJobs]
318     '' -> [1,2,..., NJobs]
319     'n1' -> [n1]
320     'n1-n2' -> [n1, n1+1, n1+2, ..., n2-1, n2]
321     'n1,n2' -> [n1, n2]
322     'n1,n2-n3' -> [n1, n2, n2+1, n2+2, ..., n3-1, n3]
323     """
324     result = []
325 farinafa 1.128
326 slacapra 1.9 common.logger.debug(5,"parseRange_ "+str(aRange))
327     if aRange=='all' or aRange==None or aRange=='':
328 spiga 1.166 result=range(1,common._db.nJobs()+1)
329 slacapra 1.8 return result
330 slacapra 1.9 elif aRange=='0':
331     return result
332 slacapra 1.8
333 farinafa 1.128 subRanges = str(aRange).split(',') # DEPRECATED # Fabio #string.split(aRange, ',')
334 slacapra 1.8 for subRange in subRanges:
335     result = result+self.parseSimpleRange_(subRange)
336    
337     if self.checkUniqueness_(result):
338     return result
339     else:
340 mcinquil 1.132 common.logger.message( "Error " +str(result) )
341 slacapra 1.8 return []
342    
343     def checkUniqueness_(self, list):
344     """
345 slacapra 1.9 check if a list contains only unique elements
346 slacapra 1.8 """
347    
348     uniqueList = []
349    
350     [uniqueList.append(it) for it in list if not uniqueList.count(it)]
351    
352     return (len(list)==len(uniqueList))
353    
354 spiga 1.107
355 slacapra 1.8 def parseSimpleRange_(self, aRange):
356     """
357     Takes as the input a string with two integers separated by
358     the minus sign and returns the tuple with these numbers:
359     'n1-n2' -> [n1, n1+1, n1+2, ..., n2-1, n2]
360     'n1' -> [n1]
361     """
362     (start, end) = (None, None)
363 ewv 1.169
364 slacapra 1.8 result = []
365 farinafa 1.128 minus = str(aRange).find('-') #DEPRECATED #Fabio #string.find(aRange, '-')
366 slacapra 1.8 if ( minus < 0 ):
367 spiga 1.167 if isInt(aRange) and int(aRange)>0 :
368 slacapra 1.62 result.append(int(aRange))
369 slacapra 1.8 else:
370 spiga 1.47 common.logger.message("parseSimpleRange_ ERROR "+aRange)
371 slacapra 1.144 processHelpOptions()
372 spiga 1.167 raise CrabException("parseSimpleRange_ ERROR "+aRange)
373 spiga 1.47 pass
374 ewv 1.169
375 nsmirnov 1.4 pass
376     else:
377 farinafa 1.128 (start, end) = str(aRange).split('-')
378 slacapra 1.8 if isInt(start) and isInt(end) and int(start)>0 and int(start)<int(end):
379 ewv 1.169 result=range(int(start), int(end)+1)
380 slacapra 1.8 else:
381 spiga 1.167 common.logger.write("parseSimpleRange_ ERROR "+start+end)
382     processHelpOptions()
383     raise CrabException("parseSimpleRange_ ERROR "+start+end)
384 slacapra 1.8
385     return result
386 nsmirnov 1.4
387 nsmirnov 1.3 def initializeActions_(self, opts):
388 nsmirnov 1.1 """
389     For each user action instantiate a corresponding
390     object and put it in the action dictionary.
391     """
392 spiga 1.15
393     for opt in opts.keys():
394 ewv 1.169
395 nsmirnov 1.1 val = opts[opt]
396 ewv 1.169
397    
398 spiga 1.15 if ( opt == '-create' ):
399 spiga 1.174 if self.flag_continue:
400     msg = 'Cannot create an existing project. If you want to extend it (to analyze new fileBloks) use: \n'
401     msg += ' crab -extend '
402     raise CrabException(msg)
403 gutsche 1.83 if val and val != 'all':
404     msg = 'Per default, CRAB will create all jobs as specified in the crab.cfg file, not the command line!'
405     common.logger.message(msg)
406     msg = 'Submission will still take into account the number of jobs specified on the command line!\n'
407     common.logger.message(msg)
408 slacapra 1.82 ncjobs = 'all'
409 farinafa 1.138
410 slacapra 1.144 from Creator import Creator
411 slacapra 1.82 # Instantiate Creator object
412     self.creator = Creator(self.job_type_name,
413     self.cfg_params,
414     ncjobs)
415     self.actions[opt] = self.creator
416    
417 spiga 1.174 # create jobs in the DB
418     common._db.createJobs_(self.creator.nJobsL())
419 nsmirnov 1.3
420 slacapra 1.82 # Create and initialize JobList
421 ewv 1.169 common.job_list = JobList(common._db.nJobs(),
422 slacapra 1.82 self.creator.jobType())
423 spiga 1.151
424 ewv 1.169 taskinfo={}
425     taskinfo['scriptName'] = common.work_space.jobDir()+"/"+self.job_type_name+'.sh'
426     taskinfo['cfgName'] = common.work_space.jobDir()+"/"+self.creator.jobType().configFilename()
427    
428 slacapra 1.82 common.job_list.setScriptNames(self.job_type_name+'.sh')
429     common.job_list.setCfgNames(self.creator.jobType().configFilename())
430     self.creator.writeJobsSpecsToDB()
431 ewv 1.169 common._db.updateTask_(taskinfo)
432 nsmirnov 1.3 pass
433 spiga 1.174
434     if ( opt == '-extend' ):
435    
436     if val and val != 'all':
437     self.parseRange_(val)
438     msg = 'Per default, CRAB will extend the task with all jobs as specified in the crab.cfg file, not the command line!'
439     msg += 'Submission will still take into account the command line\n'
440     common.logger.message(msg)
441    
442     skip_blocks = True
443     ncjobs = 'all'
444     isNew=False
445     firstJob=common._db.nJobs()
446    
447     from Creator import Creator
448     # Instantiate Creator object
449     self.creator = Creator(self.job_type_name,
450     self.cfg_params,
451     ncjobs, skip_blocks, isNew, firstJob)
452     self.actions[opt] = self.creator
453    
454     # create jobs in the DB
455     common._db.createJobs_(self.creator.nJobsL(),isNew)
456    
457     # Create and initialize JobList
458     common.job_list = JobList(common._db.nJobs(),
459     self.creator.jobType())
460    
461     self.creator.writeJobsSpecsToDB(firstJob)
462     pass
463    
464 nsmirnov 1.1
465     elif ( opt == '-submit' ):
466 slacapra 1.150 ## Dealt with val == int so that -submit N means submit N jobs and not job # N
467 spiga 1.107 if (self.UseServer== 1):
468 mcinquil 1.119 from SubmitterServer import SubmitterServer
469 farinafa 1.125 self.actions[opt] = SubmitterServer(self.cfg_params, self.parseRange_(val), val)
470 spiga 1.107 else:
471 slacapra 1.146 from Submitter import Submitter
472     # Instantiate Submitter object
473     self.actions[opt] = Submitter(self.cfg_params, self.parseRange_(val), val)
474     # Create and initialize JobList
475     if len(common.job_list) == 0 :
476 ewv 1.169 common.job_list = JobList(common._db.nJobs(),
477 slacapra 1.146 None)
478 slacapra 1.23 pass
479 nsmirnov 1.1 pass
480    
481 nsmirnov 1.4 elif ( opt == '-list' ):
482 spiga 1.151 '''
483 ewv 1.169 Print the relevant infos of a range-all jobs/task
484     '''
485 slacapra 1.8 jobs = self.parseRange_(val)
486    
487 ewv 1.169 common._db.dump(jobs)
488 nsmirnov 1.4 pass
489    
490 slacapra 1.67 elif ( opt == '-printId' ):
491 spiga 1.151 '''
492     Print the unique name of the task if crab is used as client
493 ewv 1.169 Print the SID list of all the jobs
494     '''
495     common._db.queryID(self.UseServer)
496 slacapra 1.67
497 nsmirnov 1.4 elif ( opt == '-status' ):
498 ewv 1.169 from Status import Status
499 spiga 1.107 if (self.UseServer== 1):
500 mcinquil 1.119 from StatusServer import StatusServer
501 spiga 1.107 self.actions[opt] = StatusServer(self.cfg_params)
502     else:
503     jobs = self.parseRange_(val)
504 ewv 1.169
505 spiga 1.107 if len(jobs) != 0:
506 spiga 1.153 self.actions[opt] = Status(self.cfg_params)
507 spiga 1.107 pass
508 slacapra 1.22
509 nsmirnov 1.4 elif ( opt == '-kill' ):
510 slacapra 1.8
511 slacapra 1.148 if val:
512     if val =='all':
513 slacapra 1.161 jobs = common._db.nJobs("list")
514 slacapra 1.148 else:
515     jobs = self.parseRange_(val)
516     pass
517     else:
518     raise CrabException("Warning: with '-kill' you _MUST_ specify a job range or 'all'")
519     pass
520    
521 corvo 1.109 if (self.UseServer== 1):
522 slacapra 1.148 from KillerServer import KillerServer
523 ewv 1.169 self.actions[opt] = KillerServer(self.cfg_params,jobs)
524 corvo 1.109 else:
525 slacapra 1.161 from Killer import Killer
526 spiga 1.162 self.actions[opt] = Killer(self.cfg_params,jobs)
527 nsmirnov 1.1
528 farinafa 1.120
529 slacapra 1.70 elif ( opt == '-getoutput' or opt == '-get'):
530 ewv 1.169
531 spiga 1.162 if val=='all' or val==None or val=='':
532 ewv 1.169 jobs = 'all'
533 spiga 1.162 else:
534     jobs = self.parseRange_(val)
535    
536 spiga 1.107 if (self.UseServer== 1):
537 mcinquil 1.119 from GetOutputServer import GetOutputServer
538 spiga 1.162 self.actions[opt] = GetOutputServer(self.cfg_params,jobs)
539 spiga 1.20 else:
540 spiga 1.162 from GetOutput import GetOutput
541 spiga 1.159 self.actions[opt] = GetOutput(self.cfg_params,jobs)
542 nsmirnov 1.4
543     elif ( opt == '-resubmit' ):
544 slacapra 1.11 if val:
545 slacapra 1.150 if val=='all':
546 spiga 1.164 jobs = common._db.nJobs('list')
547 slacapra 1.150 else:
548     jobs = self.parseRange_(val)
549 slacapra 1.8
550 spiga 1.164 if (self.UseServer== 1):
551     from ResubmitterServer import ResubmitterServer
552     self.actions[opt] = ResubmitterServer(self.cfg_params, jobs)
553     else:
554     from Resubmitter import Resubmitter
555     self.actions[opt] = Resubmitter(self.cfg_params, jobs)
556 slacapra 1.11 else:
557     common.logger.message("Warning: with '-resubmit' you _MUST_ specify a job range or 'all'")
558 spiga 1.60 common.logger.message("WARNING: _all_ job specified in the range will be resubmitted!!!")
559 slacapra 1.11 pass
560 slacapra 1.8 pass
561 gutsche 1.61
562 slacapra 1.163 elif ( opt in ['-testJdl','-listMatch', '-match']):
563 slacapra 1.8 jobs = self.parseRange_(val)
564    
565 slacapra 1.163 if len(jobs) != 0:
566 slacapra 1.77 # Instantiate Checker object
567 ewv 1.169 from Checker import Checker
568 slacapra 1.163 self.actions[opt] = Checker(self.cfg_params, jobs)
569 slacapra 1.8
570 slacapra 1.9 elif ( opt == '-postMortem' ):
571 corvo 1.95
572 spiga 1.162 if val:
573     if val =='all':
574     jobs = common._db.nJobs("list")
575     else:
576     jobs = self.parseRange_(val)
577     pass
578     else:
579     raise CrabException("Warning: please specify a job range or 'all'")
580     pass
581    
582 spiga 1.108 if (self.UseServer== 1):
583 mcinquil 1.119 from PostMortemServer import PostMortemServer
584 spiga 1.162 self.actions[opt] = PostMortemServer(self.cfg_params,jobs)
585 ewv 1.169 else:
586 spiga 1.162 from PostMortem import PostMortem
587     self.actions[opt] = PostMortem(self.cfg_params, jobs)
588 slacapra 1.9
589     elif ( opt == '-clean' ):
590     if val != None:
591     raise CrabException("No range allowed for '-clean'")
592 mcinquil 1.118 if (self.UseServer== 1):
593 mcinquil 1.119 from CleanerServer import CleanerServer
594 mcinquil 1.118 self.actions[opt] = CleanerServer(self.cfg_params)
595     else:
596 slacapra 1.144 from Cleaner import Cleaner
597 mcinquil 1.118 self.actions[opt] = Cleaner(self.cfg_params)
598 spiga 1.165
599 spiga 1.170 elif ( opt in ['-printJdl','-createJdl']):
600 slacapra 1.171 """
601     Materialize JDL
602     """
603     ## Temporary:
604     if opt == '-printJdl':
605     common.logger.message("WARNING: -printJdl option is deprecated : please use -createJdl \n")
606     if val =='all' or val == None or val == '':
607     jobs = common._db.nJobs("list")
608     else:
609     jobs = self.parseRange_(val)
610     pass
611     from JdlWriter import JdlWriter
612     self.actions[opt] = JdlWriter(self.cfg_params, jobs)
613 spiga 1.165
614 fanzago 1.111 elif ( opt == '-publish' ):
615 slacapra 1.141 from Publisher import Publisher
616 slacapra 1.116 self.actions[opt] = Publisher(self.cfg_params)
617    
618 slacapra 1.171 elif ( opt == '-copyLocal' ):
619     if val =='all' or val == None or val == '':
620     jobs = common._db.nJobs("list")
621     else:
622     jobs = self.parseRange_(val)
623     pass
624     from CopyLocal import CopyLocal
625     self.actions[opt] = CopyLocal(self.cfg_params, jobs)
626    
627 mcinquil 1.173 elif ( opt == '-renewProxy' ):
628     if (self.UseServer== 1):
629     from ProxyRenewServer import ProxyRenewServer
630     self.actions[opt] = ProxyRenewServer(self.cfg_params)
631     else:
632     msg = "The option [-renewProxy] can be used only with the server modality!"
633     raise CrabException(msg)
634 nsmirnov 1.1 pass
635     return
636    
637 nsmirnov 1.3 def createWorkingSpace_(self):
638 slacapra 1.9 new_dir = ''
639    
640 slacapra 1.144 if self.cfg_params.has_key('USER.ui_working_dir') :
641 spiga 1.172 new_dir =os.path.abspath(self.cfg_params['USER.ui_working_dir'])
642 slacapra 1.144 else:
643     new_dir = self.cwd + common.prog_name + '_' + self.name + '_' + self.current_time
644     pass
645     if os.path.exists(new_dir):
646     if os.listdir(new_dir):
647     msg = new_dir + ' already exists and is not empty. Please remove it before create new task'
648     raise CrabException(msg)
649    
650 fanzago 1.49 common.work_space = WorkSpace(new_dir, self.cfg_params)
651 nsmirnov 1.1 common.work_space.create()
652 slacapra 1.144
653 nsmirnov 1.1 return
654    
655 nsmirnov 1.3 def loadConfiguration_(self, opts):
656 nsmirnov 1.1
657     save_opts = common.work_space.loadSavedOptions()
658    
659     # Override saved options with new command-line options
660    
661     for k in opts.keys():
662     save_opts[k] = opts[k]
663     pass
664    
665     # Return updated options
666     return save_opts
667    
668 nsmirnov 1.3 def createLogger_(self, args):
669 nsmirnov 1.1
670     log = Logger()
671     log.quiet(self.flag_quiet)
672     log.setDebugLevel(self.debug_level)
673     log.write(args+'\n')
674 nsmirnov 1.3 log.message(self.headerString_())
675 nsmirnov 1.1 log.flush()
676     common.logger = log
677     return
678    
679 nsmirnov 1.3 def updateHistory_(self, args):
680 nsmirnov 1.1 history_fname = common.prog_name+'.history'
681     history_file = open(history_fname, 'a')
682     history_file.write(self.current_time+': '+args+'\n')
683     history_file.close()
684     return
685    
686 nsmirnov 1.3 def headerString_(self):
687 nsmirnov 1.1 """
688     Creates a string describing program options either given in
689     the command line or their default values.
690     """
691     header = common.prog_name + ' (version ' + common.prog_version_str + \
692     ') running on ' + \
693     time.ctime(time.time())+'\n\n' + \
694     common.prog_name+'. Working options:\n'
695     header = header +\
696 slacapra 1.85 ' scheduler ' + self.cfg_params['CRAB.scheduler'] + '\n'+\
697 nsmirnov 1.1 ' job type ' + self.job_type_name + '\n'+\
698     ' working directory ' + common.work_space.topDir()\
699     + '\n'
700     return header
701 ewv 1.169
702 nsmirnov 1.3 def createScheduler_(self):
703 nsmirnov 1.1 """
704     Creates a scheduler object instantiated by its name.
705     """
706 slacapra 1.147 if not self.cfg_params.has_key("CRAB.scheduler"):
707     msg = 'No real scheduler selected: edg, lsf ...'
708     msg = msg + 'Please specify a scheduler type in the crab cfg file'
709     raise CrabException(msg)
710     self.scheduler_name = self.cfg_params["CRAB.scheduler"]
711 spiga 1.160 ### just temporary... will disappear
712 ewv 1.169 if self.scheduler_name.lower()=='glitecoll': self.scheduler_name='glite'
713 slacapra 1.147
714     klass_name = 'Scheduler' + string.capitalize(self.scheduler_name)
715 nsmirnov 1.1 file_name = klass_name
716     try:
717     klass = importName(file_name, klass_name)
718     except KeyError:
719     msg = 'No `class '+klass_name+'` found in file `'+file_name+'.py`'
720     raise CrabException(msg)
721     except ImportError, e:
722 slacapra 1.101 msg = 'Cannot create scheduler Boss'
723 nsmirnov 1.1 msg += ' (file: '+file_name+', class '+klass_name+'):\n'
724     msg += str(e)
725     raise CrabException(msg)
726    
727     common.scheduler = klass()
728     common.scheduler.configure(self.cfg_params)
729     return
730    
731     def run(self):
732     """
733 ewv 1.169 For each
734 nsmirnov 1.1 """
735    
736     for act in self.main_actions:
737     if act in self.actions.keys(): self.actions[act].run()
738     pass
739    
740     for act in self.aux_actions:
741     if act in self.actions.keys(): self.actions[act].run()
742     pass
743     return
744    
745     ###########################################################################
746 slacapra 1.144 def processHelpOptions(opts={}):
747     from crab_help import usage, help
748 nsmirnov 1.1
749 slacapra 1.11 if len(opts):
750     for opt in opts.keys():
751     if opt in ('-v', '-version', '--version') :
752 slacapra 1.144 print 'CRAB version: ',Crab.version()
753 slacapra 1.11 return 1
754     if opt in ('-h','-help','--help') :
755     if opts[opt] : help(opts[opt])
756     else: help()
757     return 1
758     else:
759     usage()
760 corvo 1.93
761 nsmirnov 1.1 return 0
762    
763 corvo 1.93 ###########################################################################
764 nsmirnov 1.1 if __name__ == '__main__':
765 slacapra 1.92 ## Get rid of some useless warning
766 slacapra 1.94 try:
767     import warnings
768     warnings.simplefilter("ignore", RuntimeWarning)
769 slacapra 1.163 except ImportError:
770 slacapra 1.94 pass # too bad, you'll get the warning
771 ewv 1.169
772 nsmirnov 1.1 # Parse command-line options and create a dictionary with
773     # key-value pairs.
774     options = parseOptions(sys.argv[1:])
775    
776     # Process "help" options, such as '-help', '-version'
777 slacapra 1.11 if processHelpOptions(options) : sys.exit(0)
778 nsmirnov 1.1
779     # Create, initialize, and run a Crab object
780     try:
781 nsmirnov 1.3 crab = Crab(options)
782 nsmirnov 1.1 crab.run()
783 slacapra 1.144 common.apmon.free()
784 nsmirnov 1.1 except CrabException, e:
785     print '\n' + common.prog_name + ': ' + str(e) + '\n'
786     if common.logger:
787     common.logger.write('ERROR: '+str(e)+'\n')
788     pass
789     pass
790    
791     pass