ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/crab.py
Revision: 1.10
Committed: Tue Jul 26 16:49:11 2005 UTC (19 years, 9 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_1_0_0_pre1
Changes since 1.9: +0 -1 lines
Log Message:
more changes

File Contents

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