ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/multicrab.py
Revision: 1.2
Committed: Mon Jul 21 11:11:07 2008 UTC (16 years, 9 months ago) by slacapra
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_3_2_pre1
Branch point for: AnaDataSet
Changes since 1.1: +6 -0 lines
Log Message:
change storage_path by adding section name at the end

File Contents

# Content
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 # read crab.cfg file and search for storage_path
166 cfg_params = loadConfig(self.cfg,{})
167 self.storage_path = cfg_params.get("USER.storage_path",None)
168 return
169
170 def loadMultiConfig(self, file):
171 """
172 returns a dictionary with keys of the form
173 <section>.<option> and the corresponding values
174 """
175 config={}
176 cp = ConfigParser.ConfigParser()
177 cp.read(file)
178 for sec in cp.sections():
179 # print 'Section',sec
180 config[sec]={}
181 for opt in cp.options(sec):
182 #print 'config['+sec+'.'+opt+'] = '+string.strip(cp.get(sec,opt))
183 config[sec][opt] = string.strip(cp.get(sec,opt))
184 return config
185
186 def run(self):
187 #run crabs
188 for sec in self.cfg_params_dataset:
189 options={}
190 if self.flag_continue:
191 options['-c']=sec
192 # DatasetName to be used
193 options['-USER.ui_working_dir']=sec
194 # options from multicrab.cfg
195 for opt in self.cfg_params_dataset[sec]:
196 tmp="-"+string.upper(opt.split(".")[0])+"."+opt.split(".")[1]
197 options[tmp]=self.cfg_params_dataset[sec][opt]
198 # add section to storage_path if exist in crab.cfg
199 if self.storage_path:
200 options["-USER.storage_path"]=self.storage_path+"/"+sec
201 # Input options (command)
202 for opt in self.opts:
203 options[opt]=self.opts[opt]
204 try:
205 # print options
206 crab = Crab(options)
207 crab.run()
208 common.apmon.free()
209 except CrabException, e:
210 print '\n' + common.prog_name + ': ' + str(e) + '\n'
211 if common.logger:
212 common.logger.write('ERROR: '+str(e)+'\n')
213 pass
214 pass
215 pass
216 pass
217
218 #common.apmon.free()
219
220 ###########################################################################
221 if __name__ == '__main__':
222 ## Get rid of some useless warning
223 try:
224 import warnings
225 warnings.simplefilter("ignore", RuntimeWarning)
226 except ImportError:
227 pass # too bad, you'll get the warning
228
229 # Parse command-line options and create a dictionary with
230 # key-value pairs.
231 options = parseOptions(sys.argv[1:])
232
233 # Process "help" options, such as '-help', '-version'
234 if processHelpOptions(options) : sys.exit(0)
235
236 # Create, initialize, and run a Crab object
237 try:
238 multicrab = MultiCrab(options)
239 multicrab.run()
240 except CrabException, e:
241 print '\n' + common.prog_name + ': ' + str(e) + '\n'
242
243 pass