ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/multicrab.py
Revision: 1.1
Committed: Fri Jul 18 16:14:53 2008 UTC (16 years, 9 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
Log Message:
first upload of multicrab

File Contents

# User Rev Content
1 slacapra 1.1 #!/usr/bin/env python
2     import sys, os, time, string, shutil
3     from crab_util import *
4     from crab import *
5     import common
6    
7     ###########################################################################
8     class MultiCrab:
9     def __init__(self, opts):
10     self.prog_name='multicrab'
11     # Configuration file
12     self.cfg_fname = None
13     # Continuation flag
14     self.flag_continue = 0
15     self.continue_dir = None
16     self.processContinueOption_(opts)
17    
18     self.processIniFile_(opts)
19     # configuration
20     self.opts=opts
21    
22     if not self.flag_continue:
23     self.createWorkSpace()
24    
25     print self.prog_name + ' running on ' + time.ctime(time.time())
26     print ' working directory ' + self.continue_dir
27    
28     crabs=[]
29    
30     def processContinueOption_(self,opts):
31    
32     # Look for the '-continue' option.
33    
34     for opt in opts.keys():
35     if ( opt in ('-continue','-c') ):
36     self.flag_continue = 1
37     val = opts[opt]
38     if val:
39     if val[0] == '/': self.continue_dir = val # abs path
40     else: self.continue_dir = os.getcwd() + '/' + val # rel path
41     pass
42     break
43     pass
44    
45     # Look for actions which has sense only with '-continue'
46    
47     if "-create" not in opts.keys() :
48     self.flag_continue = 1
49    
50     if not self.flag_continue: return
51    
52     if not self.continue_dir:
53     prefix = self.prog_name + '_'
54     self.continue_dir = findLastWorkDir(prefix)
55     pass
56    
57     if not self.continue_dir:
58     raise CrabException('Cannot find last working directory.')
59    
60     if not os.path.exists(self.continue_dir):
61     msg = 'Cannot continue because the working directory <'
62     msg += self.continue_dir
63     msg += '> does not exist.'
64     raise CrabException(msg)
65    
66     return
67    
68     def createWorkSpace(self):
69     # create WorkingDir for Multicrab
70     if 'MULTICRAB.working_dir' in self.opts.keys():
71     self.continue_dir = os.path.abspath(self.opts['MULTICRAB.working_dir'])
72     else:
73     current_time = time.strftime('%y%m%d_%H%M%S', time.localtime(time.time()))
74     self.continue_dir = os.getcwd() + '/' + self.prog_name + '_' + current_time
75    
76     if self.continue_dir and not os.path.exists(self.continue_dir):
77     try:
78     os.mkdir(self.continue_dir)
79     except OSError:
80     msg = 'Cannot create '+str(self.continue_dir) +' directory.\n'
81     raise CrabException(msg)
82     pass
83     else:
84     msg = 'Directory '+str(self.continue_dir) +' already exist.\n'
85     raise CrabException(msg)
86    
87     shutil.copyfile('multicrab.cfg',self.continue_dir+'/multicrab.cfg')
88    
89     return
90    
91     def processIniFile_(self, opts):
92     """
93     Processes a configuration INI-file.
94     """
95    
96     # Extract cfg-file name from the cmd-line options.
97    
98     for opt in opts.keys():
99     if ( opt == '-cfg' ):
100     if self.flag_continue:
101     raise CrabException('-continue and -cfg cannot coexist.')
102     if opts[opt] : self.cfg_fname = opts[opt]
103     else : processHelpOptions()
104     pass
105     pass
106    
107     # Set default cfg-fname
108    
109     if self.cfg_fname == None:
110     if self.flag_continue:
111     self.cfg_fname = self.continue_dir + '/multicrab.cfg'
112     else:
113     self.cfg_fname = 'multicrab.cfg'
114     pass
115     pass
116    
117     # Load cfg-file
118    
119     if self.cfg_fname != None:
120     if os.path.exists(self.cfg_fname):
121     self.cfg_params = self.loadMultiConfig(self.cfg_fname)
122     pass
123     else:
124     msg = 'cfg-file '+self.cfg_fname+' not found.'
125     raise CrabException(msg)
126     pass
127     pass
128    
129     # process the [CRAB] section
130    
131     lhp = len('MULTICRAB.')
132     for k in self.cfg_params.keys():
133     if len(k) >= lhp and k[:lhp] == 'MULTICRAB.':
134     opt = '-'+k[lhp:]
135     if len(opt) >= 3 and opt[:3] == '-__': continue
136     if opt not in opts.keys():
137     opts[opt] = self.cfg_params[k]
138     pass
139     pass
140     pass
141    
142     self.cfg_params_dataset = {}
143     common_opts = []
144     # first get common sections
145     for sec in self.cfg_params:
146     if sec in ['MULTICRAB']:
147     cfg_common=self.cfg_params[sec]
148     continue
149     if sec in ['COMMON']:
150     common_opts.append(self.cfg_params[sec])
151     continue
152     pass
153    
154     # then Dataset's specific
155     for sec in self.cfg_params:
156     if sec in ['MULTICRAB', 'COMMON']: continue
157     self.cfg_params_dataset[sec]=self.cfg_params[sec]
158     # add common to all dataset
159     for opt in common_opts:
160     self.cfg_params_dataset[sec]=opt
161     pass
162    
163     self.cfg=cfg_common['cfg']
164    
165     return
166    
167     def loadMultiConfig(self, file):
168     """
169     returns a dictionary with keys of the form
170     <section>.<option> and the corresponding values
171     """
172     config={}
173     cp = ConfigParser.ConfigParser()
174     cp.read(file)
175     for sec in cp.sections():
176     # print 'Section',sec
177     config[sec]={}
178     for opt in cp.options(sec):
179     #print 'config['+sec+'.'+opt+'] = '+string.strip(cp.get(sec,opt))
180     config[sec][opt] = string.strip(cp.get(sec,opt))
181     return config
182    
183     def run(self):
184     #run crabs
185     for sec in self.cfg_params_dataset:
186     options={}
187     if self.flag_continue:
188     options['-c']=sec
189     # DatasetName to be used
190     options['-USER.ui_working_dir']=sec
191     # options from multicrab.cfg
192     for opt in self.cfg_params_dataset[sec]:
193     tmp="-"+string.upper(opt.split(".")[0])+"."+opt.split(".")[1]
194     options[tmp]=self.cfg_params_dataset[sec][opt]
195     # Input options (command)
196     for opt in self.opts:
197     options[opt]=self.opts[opt]
198     try:
199     # print options
200     crab = Crab(options)
201     crab.run()
202     common.apmon.free()
203     except CrabException, e:
204     print '\n' + common.prog_name + ': ' + str(e) + '\n'
205     if common.logger:
206     common.logger.write('ERROR: '+str(e)+'\n')
207     pass
208     pass
209     pass
210     pass
211    
212     #common.apmon.free()
213    
214     ###########################################################################
215     if __name__ == '__main__':
216     ## Get rid of some useless warning
217     try:
218     import warnings
219     warnings.simplefilter("ignore", RuntimeWarning)
220     except ImportError:
221     pass # too bad, you'll get the warning
222    
223     # Parse command-line options and create a dictionary with
224     # key-value pairs.
225     options = parseOptions(sys.argv[1:])
226    
227     # Process "help" options, such as '-help', '-version'
228     if processHelpOptions(options) : sys.exit(0)
229    
230     # Create, initialize, and run a Crab object
231     try:
232     multicrab = MultiCrab(options)
233     multicrab.run()
234     except CrabException, e:
235     print '\n' + common.prog_name + ': ' + str(e) + '\n'
236    
237     pass