ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cms_cmssw.py
Revision: 1.364
Committed: Thu Sep 23 15:10:23 2010 UTC (14 years, 7 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_7_6_pre1, CRAB_2_7_5_patch1, CRAB_2_7_5, CRAB_2_7_5_pre3, CRAB_2_7_5_pre2, CRAB_2_7_5_pre1
Changes since 1.363: +3 -45 lines
Log Message:
fixed for old executable_exit_status value, savannah bug 73035

File Contents

# User Rev Content
1 spiga 1.358
2 fanzago 1.364 __revision__ = "$Id: cms_cmssw.py,v 1.363 2010/08/11 19:28:12 ewv Exp $"
3     __version__ = "$Revision: 1.363 $"
4 spiga 1.358
5 slacapra 1.1 from JobType import JobType
6     from crab_exceptions import *
7     from crab_util import *
8     import common
9 ewv 1.363 import re
10 slacapra 1.1 import Scram
11 spiga 1.269 from Splitter import JobSplitter
12 ewv 1.355 from Downloader import Downloader
13 ewv 1.363 try:
14 fanzago 1.360 import json
15     except:
16     import simplejson as json
17 slacapra 1.1
18 spiga 1.293 from IMProv.IMProvNode import IMProvNode
19 ewv 1.355 from IMProv.IMProvLoader import loadIMProvFile
20 slacapra 1.105 import os, string, glob
21 ewv 1.355 from xml.dom import pulldom
22 slacapra 1.1
23     class Cmssw(JobType):
24 spiga 1.208 def __init__(self, cfg_params, ncjobs,skip_blocks, isNew):
25 slacapra 1.1 JobType.__init__(self, 'CMSSW')
26 spiga 1.304 common.logger.debug('CMSSW::__init__')
27 spiga 1.208 self.skip_blocks = skip_blocks
28 farinafa 1.346 self.argsList = 2
29 spiga 1.315 self.NumEvents=0
30 gutsche 1.3 self._params = {}
31     self.cfg_params = cfg_params
32 fanzago 1.360 ### FEDE FOR MULTI ###
33     self.var_filter=''
34 ewv 1.254
35 spiga 1.234 ### Temporary patch to automatically skip the ISB size check:
36 ewv 1.319 self.server = self.cfg_params.get('CRAB.server_name',None) or \
37     self.cfg_params.get('CRAB.use_server',0)
38 mcinquil 1.340 self.local = common.scheduler.name().upper() in ['LSF','CAF','CONDOR','SGE','PBS']
39 ewv 1.250 size = 9.5
40 ewv 1.333 if self.server or self.local:
41 ewv 1.319 size = 99999
42 spiga 1.306 self.MaxTarBallSize = float(self.cfg_params.get('GRID.maxtarballsize',size))
43 gutsche 1.72
44 gutsche 1.44 # number of jobs requested to be created, limit obj splitting
45 gutsche 1.38 self.ncjobs = ncjobs
46    
47 slacapra 1.1 self.scram = Scram.Scram(cfg_params)
48     self.additional_inbox_files = []
49     self.scriptExe = ''
50     self.executable = ''
51 slacapra 1.71 self.executable_arch = self.scram.getArch()
52 spiga 1.320 self.tgz_name = 'default.tgz'
53 corvo 1.56 self.scriptName = 'CMSSW.sh'
54 ewv 1.192 self.pset = ''
55 spiga 1.187 self.datasetPath = ''
56 gutsche 1.3
57 spiga 1.300 self.tgzNameWithPath = common.work_space.pathForTgz()+self.tgz_name
58 gutsche 1.50 # set FJR file name
59     self.fjrFileName = 'crab_fjr.xml'
60    
61 slacapra 1.1 self.version = self.scram.getSWVersion()
62 spiga 1.304 common.logger.log(10-1,"CMSSW version is: "+str(self.version))
63 spiga 1.324 version_array = self.version.split('_')
64     self.CMSSW_major = 0
65     self.CMSSW_minor = 0
66     self.CMSSW_patch = 0
67 ewv 1.182 try:
68 spiga 1.324 self.CMSSW_major = int(version_array[1])
69     self.CMSSW_minor = int(version_array[2])
70     self.CMSSW_patch = int(version_array[3])
71 ewv 1.182 except:
72 ewv 1.184 msg = "Cannot parse CMSSW version string: " + self.version + " for major and minor release number!"
73 ewv 1.182 raise CrabException(msg)
74    
75 ewv 1.334 if self.CMSSW_major < 2 or (self.CMSSW_major == 2 and self.CMSSW_minor < 1):
76     msg = "CRAB supports CMSSW >= 2_1_x only. Use an older CRAB version."
77 ewv 1.276 raise CrabException(msg)
78     """
79     As CMSSW versions are dropped we can drop more code:
80 ewv 1.334 2.x dropped: drop check for lumi range setting
81 ewv 1.276 """
82 ewv 1.355 self.checkCMSSWVersion()
83 slacapra 1.1 ### collect Data cards
84 gutsche 1.66
85 fanzago 1.221 ### Temporary: added to remove input file control in the case of PU
86 farinafa 1.224 self.dataset_pu = cfg_params.get('CMSSW.dataset_pu', None)
87 ewv 1.226
88 slacapra 1.153 tmp = cfg_params['CMSSW.datasetpath']
89 spiga 1.305 common.logger.log(10-1, "CMSSW::CMSSW(): datasetPath = "+tmp)
90 spiga 1.236
91     if tmp =='':
92     msg = "Error: datasetpath not defined "
93     raise CrabException(msg)
94     elif string.lower(tmp)=='none':
95 slacapra 1.153 self.datasetPath = None
96     self.selectNoInput = 1
97 fanzago 1.318 self.primaryDataset = 'null'
98 slacapra 1.153 else:
99     self.datasetPath = tmp
100     self.selectNoInput = 0
101 fanzago 1.338 ll = len(self.datasetPath.split("/"))
102     if (ll < 4):
103     msg = 'Your datasetpath has a invalid format ' + self.datasetPath + '\n'
104     msg += 'Expected a path in format /PRIMARY/PROCESSED/TIER1-TIER2 or /PRIMARY/PROCESSED/TIER/METHOD for ADS'
105     raise CrabException(msg)
106 fanzago 1.318 self.primaryDataset = self.datasetPath.split("/")[1]
107     self.dataTier = self.datasetPath.split("/")[2]
108 gutsche 1.5
109 ewv 1.326 # Analysis dataset is primary/processed/tier/definition
110 ewv 1.330 self.ads = False
111     if self.datasetPath:
112     self.ads = len(self.datasetPath.split("/")) > 4
113 spiga 1.354 self.lumiMask = self.cfg_params.get('CMSSW.lumi_mask',None)
114 ewv 1.356 self.lumiParams = self.cfg_params.get('CMSSW.total_number_of_lumis',None) or \
115     self.cfg_params.get('CMSSW.lumis_per_job',None)
116 spiga 1.358
117 ewv 1.327 # FUTURE: Can remove this check
118     if self.ads and self.CMSSW_major < 3:
119     common.logger.info('Warning: Analysis dataset support is incomplete in CMSSW 2_x.')
120     common.logger.info(' Only file level, not lumi level, granularity is supported.')
121    
122 spiga 1.288 self.debugWrap=''
123 fanzago 1.285 self.debug_wrapper = int(cfg_params.get('USER.debug_wrapper',0))
124     if self.debug_wrapper == 1: self.debugWrap='--debug'
125 slacapra 1.291
126 slacapra 1.1 ## now the application
127 ewv 1.313 self.managedGenerators = ['madgraph', 'comphep', 'lhe']
128 ewv 1.258 self.generator = cfg_params.get('CMSSW.generator','pythia').lower()
129 slacapra 1.153 self.executable = cfg_params.get('CMSSW.executable','cmsRun')
130 spiga 1.305 common.logger.log(10-1, "CMSSW::CMSSW(): executable = "+self.executable)
131 slacapra 1.1
132 slacapra 1.153 if not cfg_params.has_key('CMSSW.pset'):
133 slacapra 1.1 raise CrabException("PSet file missing. Cannot run cmsRun ")
134 slacapra 1.153 self.pset = cfg_params['CMSSW.pset']
135 spiga 1.305 common.logger.log(10-1, "Cmssw::Cmssw(): PSet file = "+self.pset)
136 slacapra 1.153 if self.pset.lower() != 'none' :
137     if (not os.path.exists(self.pset)):
138     raise CrabException("User defined PSet file "+self.pset+" does not exist")
139     else:
140     self.pset = None
141 slacapra 1.1
142     # output files
143 slacapra 1.53 ## stuff which must be returned always via sandbox
144     self.output_file_sandbox = []
145    
146     # add fjr report by default via sandbox
147     self.output_file_sandbox.append(self.fjrFileName)
148    
149     # other output files to be returned via sandbox or copied to SE
150 mcinquil 1.216 outfileflag = False
151 slacapra 1.153 self.output_file = []
152     tmp = cfg_params.get('CMSSW.output_file',None)
153     if tmp :
154 slacapra 1.207 self.output_file = [x.strip() for x in tmp.split(',')]
155 mcinquil 1.216 outfileflag = True #output found
156 slacapra 1.1
157 slacapra 1.153 self.scriptExe = cfg_params.get('USER.script_exe',None)
158     if self.scriptExe :
159 slacapra 1.176 if not os.path.isfile(self.scriptExe):
160     msg ="ERROR. file "+self.scriptExe+" not found"
161     raise CrabException(msg)
162     self.additional_inbox_files.append(string.strip(self.scriptExe))
163 slacapra 1.70
164 spiga 1.314 self.AdditionalArgs = cfg_params.get('USER.script_arguments',None)
165     if self.AdditionalArgs : self.AdditionalArgs = string.replace(self.AdditionalArgs,',',' ')
166    
167 spiga 1.42 if self.datasetPath == None and self.pset == None and self.scriptExe == '' :
168 slacapra 1.176 msg ="Error. script_exe not defined"
169     raise CrabException(msg)
170 spiga 1.42
171 ewv 1.226 # use parent files...
172 spiga 1.269 self.useParent = int(self.cfg_params.get('CMSSW.use_parent',0))
173 spiga 1.204
174 slacapra 1.1 ## additional input files
175 slacapra 1.153 if cfg_params.has_key('USER.additional_input_files'):
176 slacapra 1.29 tmpAddFiles = string.split(cfg_params['USER.additional_input_files'],',')
177 slacapra 1.70 for tmp in tmpAddFiles:
178     tmp = string.strip(tmp)
179     dirname = ''
180     if not tmp[0]=="/": dirname = "."
181 corvo 1.85 files = []
182     if string.find(tmp,"*")>-1:
183     files = glob.glob(os.path.join(dirname, tmp))
184     if len(files)==0:
185     raise CrabException("No additional input file found with this pattern: "+tmp)
186     else:
187     files.append(tmp)
188 slacapra 1.70 for file in files:
189     if not os.path.exists(file):
190     raise CrabException("Additional input file not found: "+file)
191 slacapra 1.45 pass
192 slacapra 1.105 self.additional_inbox_files.append(string.strip(file))
193 slacapra 1.1 pass
194     pass
195 spiga 1.304 common.logger.debug("Additional input files: "+str(self.additional_inbox_files))
196 slacapra 1.153 pass
197 gutsche 1.3
198 gutsche 1.35
199 ewv 1.160 ## New method of dealing with seeds
200     self.incrementSeeds = []
201     self.preserveSeeds = []
202     if cfg_params.has_key('CMSSW.preserve_seeds'):
203     tmpList = cfg_params['CMSSW.preserve_seeds'].split(',')
204     for tmp in tmpList:
205     tmp.strip()
206     self.preserveSeeds.append(tmp)
207     if cfg_params.has_key('CMSSW.increment_seeds'):
208     tmpList = cfg_params['CMSSW.increment_seeds'].split(',')
209     for tmp in tmpList:
210     tmp.strip()
211     self.incrementSeeds.append(tmp)
212    
213 fanzago 1.318 # Copy/return/publish
214 slacapra 1.153 self.copy_data = int(cfg_params.get('USER.copy_data',0))
215     self.return_data = int(cfg_params.get('USER.return_data',0))
216 fanzago 1.318 self.publish_data = int(cfg_params.get('USER.publish_data',0))
217     if (self.publish_data == 1):
218     if not cfg_params.has_key('USER.publish_data_name'):
219     raise CrabException('Cannot publish output data, because you did not specify USER.publish_data_name parameter in the crab.cfg file')
220     else:
221     self.processedDataset = cfg_params['USER.publish_data_name']
222 ewv 1.276
223     self.conf = {}
224     self.conf['pubdata'] = None
225 spiga 1.269 # number of jobs requested to be created, limit obj splitting DD
226 slacapra 1.1 #DBSDLS-start
227 ewv 1.131 ## Initialize the variables that are extracted from DBS/DLS and needed in other places of the code
228 slacapra 1.1 self.maxEvents=0 # max events available ( --> check the requested nb. of evts in Creator.py)
229     self.DBSPaths={} # all dbs paths requested ( --> input to the site local discovery script)
230 gutsche 1.35 self.jobDestination=[] # Site destination(s) for each job (list of lists)
231 slacapra 1.1 ## Perform the data location and discovery (based on DBS/DLS)
232 slacapra 1.9 ## SL: Don't if NONE is specified as input (pythia use case)
233 gutsche 1.35 blockSites = {}
234 spiga 1.342 #wmbs
235     self.automation = int(self.cfg_params.get('WMBS.automation',0))
236     if self.automation == 0:
237     if self.datasetPath:
238     blockSites = self.DataDiscoveryAndLocation(cfg_params)
239     #DBSDLS-end
240     self.conf['blockSites']=blockSites
241 ewv 1.347
242 spiga 1.342 ## Select Splitting
243     splitByRun = int(cfg_params.get('CMSSW.split_by_run',0))
244 ewv 1.347
245 spiga 1.342 if self.selectNoInput:
246     if self.pset == None:
247     self.algo = 'ForScript'
248     else:
249     self.algo = 'NoInput'
250     self.conf['managedGenerators']=self.managedGenerators
251     self.conf['generator']=self.generator
252 ewv 1.356 elif self.ads or self.lumiMask or self.lumiParams:
253 spiga 1.342 self.algo = 'LumiBased'
254 ewv 1.359 if splitByRun:
255     msg = "Cannot combine split by run with lumi_mask, ADS, " \
256     "or lumis_per_job. Use split by lumi mode instead."
257     raise CrabException(msg)
258    
259 spiga 1.342 elif splitByRun ==1:
260     self.algo = 'RunBased'
261 spiga 1.42 else:
262 spiga 1.342 self.algo = 'EventBased'
263     common.logger.debug("Job splitting method: %s" % self.algo)
264 ewv 1.347
265 spiga 1.342 splitter = JobSplitter(self.cfg_params,self.conf)
266     self.dict = splitter.Algos()[self.algo]()
267 gutsche 1.5
268 spiga 1.300 self.argsFile= '%s/arguments.xml'%common.work_space.shareDir()
269     self.rootArgsFilename= 'arguments'
270 spiga 1.208 # modify Pset only the first time
271 spiga 1.320 if isNew:
272     if self.pset != None: self.ModifyPset()
273 spiga 1.300
274 spiga 1.320 ## Prepare inputSandbox TarBall (only the first time)
275     self.tarNameWithPath = self.getTarBall(self.executable)
276 spiga 1.293
277    
278     def ModifyPset(self):
279     import PsetManipulator as pp
280 ewv 1.335
281     # If pycfg_params set, fake out the config script
282     # to make it think it was called with those args
283     pycfg_params = self.cfg_params.get('CMSSW.pycfg_params',None)
284     if pycfg_params:
285     trueArgv = sys.argv
286     sys.argv = [self.pset]
287     sys.argv.extend(pycfg_params.split(' '))
288 spiga 1.293 PsetEdit = pp.PsetManipulator(self.pset)
289 ewv 1.335 if pycfg_params: # Restore original sys.argv
290     sys.argv = trueArgv
291    
292 spiga 1.293 try:
293     # Add FrameworkJobReport to parameter-set, set max events.
294     # Reset later for data jobs by writeCFG which does all modifications
295 ewv 1.295 PsetEdit.maxEvent(1)
296 spiga 1.293 PsetEdit.skipEvent(0)
297     PsetEdit.psetWriter(self.configFilename())
298     ## If present, add TFileService to output files
299 slacapra 1.349 if not int(self.cfg_params.get('CMSSW.skip_tfileservice_output',0)):
300 spiga 1.293 tfsOutput = PsetEdit.getTFileService()
301     if tfsOutput:
302     if tfsOutput in self.output_file:
303 spiga 1.304 common.logger.debug("Output from TFileService "+tfsOutput+" already in output files")
304 spiga 1.293 else:
305     outfileflag = True #output found
306     self.output_file.append(tfsOutput)
307 spiga 1.304 common.logger.info("Adding "+tfsOutput+" (from TFileService) to list of output files")
308 spiga 1.293 pass
309     pass
310 fanzago 1.360
311     # If requested, add PoolOutputModule to output files
312     ### FEDE FOR MULTI ###
313     #edmOutput = PsetEdit.getPoolOutputModule()
314     edmOutputDict = PsetEdit.getPoolOutputModule()
315     common.logger.debug("(test) edmOutputDict = "+str(edmOutputDict))
316     filter_dict = {}
317     for key in edmOutputDict.keys():
318     filter_dict[key]=edmOutputDict[key]['dataset']
319     common.logger.debug("(test) filter_dict for multi = "+str(filter_dict))
320    
321     #### in CMSSW.sh: export var_filter
322    
323     self.var_filter = json.dumps(filter_dict)
324     common.logger.debug("(test) var_filter for multi = "+self.var_filter)
325 ewv 1.363
326 fanzago 1.360 edmOutput = edmOutputDict.keys()
327 spiga 1.293 if int(self.cfg_params.get('CMSSW.get_edm_output',0)):
328     if edmOutput:
329 ewv 1.321 for outputFile in edmOutput:
330     if outputFile in self.output_file:
331 ewv 1.325 common.logger.debug("Output from PoolOutputModule "+outputFile+" already in output files")
332 ewv 1.321 else:
333     self.output_file.append(outputFile)
334     common.logger.info("Adding "+outputFile+" (from PoolOutputModule) to list of output files")
335     # not requested, check anyhow to avoid accidental T2 overload
336 slacapra 1.297 else:
337 ewv 1.321 if edmOutput:
338     missedFiles = []
339     for outputFile in edmOutput:
340     if outputFile not in self.output_file:
341     missedFiles.append(outputFile)
342     if missedFiles:
343     msg = "ERROR: PoolOutputModule(s) are present in your ParameteSet %s \n"%self.pset
344     msg += " but the file(s) produced ( %s ) are not in the list of output files\n" % ', '.join(missedFiles)
345     msg += "WARNING: please remove them. If you want to keep them, add the file(s) to output_files or use CMSSW.get_edm_output = 1\n"
346     if int(self.cfg_params.get('CMSSW.ignore_edm_output',0)):
347     msg += " CMSSW.ignore_edm_output==1 : Hope you know what you are doing...\n"
348     common.logger.info(msg)
349 spiga 1.322 else :
350 ewv 1.321 raise CrabException(msg)
351 ewv 1.301
352     if (PsetEdit.getBadFilesSetting()):
353     msg = "WARNING: You have set skipBadFiles to True. This will continue processing on some errors and you may not be notified."
354 spiga 1.304 common.logger.info(msg)
355 ewv 1.301
356 slacapra 1.297 except CrabException, msg:
357 spiga 1.304 common.logger.info(str(msg))
358 slacapra 1.297 msg='Error while manipulating ParameterSet (see previous message, if any): exiting...'
359 spiga 1.293 raise CrabException(msg)
360    
361 ewv 1.363 valid = re.compile('^[\w\.\-]+$')
362     for fileName in self.output_file:
363     if not valid.match(fileName):
364     msg = "The file %s may only contain alphanumeric characters and -, _, ." % fileName
365     raise CrabException(msg)
366    
367 gutsche 1.3
368 slacapra 1.1 def DataDiscoveryAndLocation(self, cfg_params):
369    
370 slacapra 1.86 import DataDiscovery
371     import DataLocation
372 spiga 1.304 common.logger.log(10-1,"CMSSW::DataDiscoveryAndLocation()")
373 gutsche 1.3
374     datasetPath=self.datasetPath
375    
376 slacapra 1.1 ## Contact the DBS
377 spiga 1.304 common.logger.info("Contacting Data Discovery Services ...")
378 slacapra 1.1 try:
379 spiga 1.208 self.pubdata=DataDiscovery.DataDiscovery(datasetPath, cfg_params,self.skip_blocks)
380 slacapra 1.1 self.pubdata.fetchDBSInfo()
381    
382 slacapra 1.41 except DataDiscovery.NotExistingDatasetError, ex :
383 slacapra 1.1 msg = 'ERROR ***: failed Data Discovery in DBS : %s'%ex.getErrorMessage()
384     raise CrabException(msg)
385 slacapra 1.41 except DataDiscovery.NoDataTierinProvenanceError, ex :
386 slacapra 1.1 msg = 'ERROR ***: failed Data Discovery in DBS : %s'%ex.getErrorMessage()
387     raise CrabException(msg)
388 slacapra 1.41 except DataDiscovery.DataDiscoveryError, ex:
389 gutsche 1.66 msg = 'ERROR ***: failed Data Discovery in DBS : %s'%ex.getErrorMessage()
390 slacapra 1.1 raise CrabException(msg)
391    
392 gutsche 1.35 self.filesbyblock=self.pubdata.getFiles()
393 spiga 1.269 self.conf['pubdata']=self.pubdata
394 gutsche 1.3
395 slacapra 1.1 ## get max number of events
396 ewv 1.192 self.maxEvents=self.pubdata.getMaxEvents()
397 slacapra 1.1
398     ## Contact the DLS and build a list of sites hosting the fileblocks
399     try:
400 slacapra 1.41 dataloc=DataLocation.DataLocation(self.filesbyblock.keys(),cfg_params)
401 gutsche 1.6 dataloc.fetchDLSInfo()
402 slacapra 1.263
403 slacapra 1.41 except DataLocation.DataLocationError , ex:
404 slacapra 1.1 msg = 'ERROR ***: failed Data Location in DLS \n %s '%ex.getErrorMessage()
405     raise CrabException(msg)
406 ewv 1.131
407 slacapra 1.1
408 slacapra 1.270 unsorted_sites = dataloc.getSites()
409     sites = self.filesbyblock.fromkeys(self.filesbyblock,'')
410     for lfn in self.filesbyblock.keys():
411     if unsorted_sites.has_key(lfn):
412     sites[lfn]=unsorted_sites[lfn]
413     else:
414     sites[lfn]=[]
415    
416 slacapra 1.264 if len(sites)==0:
417 spiga 1.267 msg = 'ERROR ***: no location for any of the blocks of this dataset: \n\t %s \n'%datasetPath
418     msg += "\tMaybe the dataset is located only at T1's (or at T0), where analysis jobs are not allowed\n"
419     msg += "\tPlease check DataDiscovery page https://cmsweb.cern.ch/dbs_discovery/\n"
420 slacapra 1.264 raise CrabException(msg)
421    
422 gutsche 1.35 allSites = []
423     listSites = sites.values()
424 slacapra 1.63 for listSite in listSites:
425     for oneSite in listSite:
426 gutsche 1.35 allSites.append(oneSite)
427 slacapra 1.291 [allSites.append(it) for it in allSites if not allSites.count(it)]
428 ewv 1.295
429 gutsche 1.3
430 gutsche 1.92 # screen output
431 spiga 1.354 if self.ads or self.lumiMask:
432     common.logger.info("Requested (A)DS %s has %s block(s)." %
433 ewv 1.350 (datasetPath, len(self.filesbyblock.keys())))
434     else:
435     common.logger.info("Requested dataset: " + datasetPath + \
436     " has " + str(self.maxEvents) + " events in " + \
437     str(len(self.filesbyblock.keys())) + " blocks.\n")
438 gutsche 1.92
439 gutsche 1.35 return sites
440 ewv 1.131
441 spiga 1.42
442 spiga 1.208 def split(self, jobParams,firstJobID):
443 ewv 1.276
444 spiga 1.293 jobParams = self.dict['args']
445 spiga 1.269 njobs = self.dict['njobs']
446     self.jobDestination = self.dict['jobDestination']
447 ewv 1.131
448 ewv 1.333 if njobs == 0:
449     raise CrabException("Asked to split zero jobs: aborting")
450     if not self.server and not self.local and njobs > 500:
451     raise CrabException("The CRAB client will not submit more than 500 jobs. You must use the server mode.")
452 slacapra 1.263
453 gutsche 1.3 # create the empty structure
454     for i in range(njobs):
455     jobParams.append("")
456 ewv 1.131
457 spiga 1.165 listID=[]
458     listField=[]
459 spiga 1.293 listDictions=[]
460 spiga 1.300 exist= os.path.exists(self.argsFile)
461 spiga 1.208 for id in range(njobs):
462     job = id + int(firstJobID)
463 spiga 1.167 listID.append(job+1)
464 spiga 1.162 job_ToSave ={}
465 spiga 1.169 concString = ' '
466 spiga 1.165 argu=''
467 spiga 1.293 str_argu = str(job+1)
468 spiga 1.208 if len(jobParams[id]):
469 ewv 1.295 argu = {'JobID': job+1}
470 spiga 1.293 for i in range(len(jobParams[id])):
471     argu[self.dict['params'][i]]=jobParams[id][i]
472 spiga 1.315 if len(jobParams[id])==1: self.NumEvents = jobParams[id][i]
473 ewv 1.295 # just for debug
474 spiga 1.293 str_argu += concString.join(jobParams[id])
475 spiga 1.314 if argu != '': listDictions.append(argu)
476 ewv 1.347 job_ToSave['arguments']= '%d %d'%( (job+1), 0)
477 spiga 1.208 job_ToSave['dlsDestination']= self.jobDestination[id]
478 spiga 1.165 listField.append(job_ToSave)
479 slacapra 1.311 from ProdCommon.SiteDB.CmsSiteMapper import CmsSEMap
480     cms_se = CmsSEMap()
481 ewv 1.295 msg="Job %s Arguments: %s\n"%(str(job+1),str_argu)
482 spiga 1.293 msg+="\t Destination: %s "%(str(self.jobDestination[id]))
483 slacapra 1.311 SEDestination = [cms_se[dest] for dest in self.jobDestination[id]]
484     msg+="\t CMSDestination: %s "%(str(SEDestination))
485 spiga 1.307 common.logger.log(10-1,msg)
486 spiga 1.293 # write xml
487 ewv 1.295 if len(listDictions):
488 spiga 1.293 if exist==False: self.CreateXML()
489     self.addEntry(listDictions)
490 spiga 1.187 common._db.updateJob_(listID,listField)
491 spiga 1.293 return
492 ewv 1.313
493 spiga 1.293 def CreateXML(self):
494     """
495 ewv 1.295 """
496 spiga 1.300 result = IMProvNode( self.rootArgsFilename )
497     outfile = file( self.argsFile, 'w').write(str(result))
498 ewv 1.295 return
499 spiga 1.293
500     def addEntry(self, listDictions):
501     """
502     _addEntry_
503 ewv 1.295
504 spiga 1.293 add an entry to the xml file
505     """
506     ## load xml
507 spiga 1.300 improvDoc = loadIMProvFile(self.argsFile)
508 spiga 1.293 entrname= 'Job'
509     for dictions in listDictions:
510     report = IMProvNode(entrname , None, **dictions)
511     improvDoc.addNode(report)
512 spiga 1.300 outfile = file( self.argsFile, 'w').write(str(improvDoc))
513 gutsche 1.3 return
514 ewv 1.131
515 gutsche 1.3 def numberOfJobs(self):
516 spiga 1.342 #wmbs
517 ewv 1.347 if self.automation==0:
518 spiga 1.342 return self.dict['njobs']
519     else:
520     return None
521 ewv 1.347
522 slacapra 1.1 def getTarBall(self, exe):
523     """
524     Return the TarBall with lib and exe
525     """
526 spiga 1.320 self.tgzNameWithPath = common.work_space.pathForTgz()+self.tgz_name
527     if os.path.exists(self.tgzNameWithPath):
528     return self.tgzNameWithPath
529 slacapra 1.1
530     # Prepare a tar gzipped file with user binaries.
531     self.buildTar_(exe)
532    
533 spiga 1.320 return string.strip(self.tgzNameWithPath)
534 slacapra 1.1
535     def buildTar_(self, executable):
536    
537     # First of all declare the user Scram area
538     swArea = self.scram.getSWArea_()
539     swReleaseTop = self.scram.getReleaseTop_()
540 ewv 1.131
541 slacapra 1.1 ## check if working area is release top
542     if swReleaseTop == '' or swArea == swReleaseTop:
543 spiga 1.304 common.logger.debug("swArea = "+swArea+" swReleaseTop ="+swReleaseTop)
544 slacapra 1.1 return
545    
546 slacapra 1.61 import tarfile
547     try: # create tar ball
548 spiga 1.320 tar = tarfile.open(self.tgzNameWithPath, "w:gz")
549 slacapra 1.61 ## First find the executable
550 slacapra 1.86 if (self.executable != ''):
551 slacapra 1.61 exeWithPath = self.scram.findFile_(executable)
552     if ( not exeWithPath ):
553     raise CrabException('User executable '+executable+' not found')
554 ewv 1.131
555 slacapra 1.61 ## then check if it's private or not
556     if exeWithPath.find(swReleaseTop) == -1:
557     # the exe is private, so we must ship
558 spiga 1.304 common.logger.debug("Exe "+exeWithPath+" to be tarred")
559 slacapra 1.61 path = swArea+'/'
560 corvo 1.85 # distinguish case when script is in user project area or given by full path somewhere else
561     if exeWithPath.find(path) >= 0 :
562     exe = string.replace(exeWithPath, path,'')
563 slacapra 1.129 tar.add(path+exe,exe)
564 corvo 1.85 else :
565     tar.add(exeWithPath,os.path.basename(executable))
566 slacapra 1.61 pass
567     else:
568     # the exe is from release, we'll find it on WN
569     pass
570 ewv 1.131
571 slacapra 1.61 ## Now get the libraries: only those in local working area
572 slacapra 1.256 tar.dereference=True
573 slacapra 1.61 libDir = 'lib'
574     lib = swArea+'/' +libDir
575 spiga 1.304 common.logger.debug("lib "+lib+" to be tarred")
576 slacapra 1.61 if os.path.exists(lib):
577     tar.add(lib,libDir)
578 ewv 1.131
579 slacapra 1.61 ## Now check if module dir is present
580     moduleDir = 'module'
581     module = swArea + '/' + moduleDir
582     if os.path.isdir(module):
583     tar.add(module,moduleDir)
584 slacapra 1.256 tar.dereference=False
585 slacapra 1.61
586     ## Now check if any data dir(s) is present
587 spiga 1.179 self.dataExist = False
588 slacapra 1.212 todo_list = [(i, i) for i in os.listdir(swArea+"/src")]
589 slacapra 1.206 while len(todo_list):
590     entry, name = todo_list.pop()
591 slacapra 1.211 if name.startswith('crab_0_') or name.startswith('.') or name == 'CVS':
592 slacapra 1.206 continue
593 slacapra 1.212 if os.path.isdir(swArea+"/src/"+entry):
594 slacapra 1.206 entryPath = entry + '/'
595 slacapra 1.212 todo_list += [(entryPath + i, i) for i in os.listdir(swArea+"/src/"+entry)]
596 slacapra 1.206 if name == 'data':
597     self.dataExist=True
598 spiga 1.304 common.logger.debug("data "+entry+" to be tarred")
599 slacapra 1.212 tar.add(swArea+"/src/"+entry,"src/"+entry)
600 slacapra 1.206 pass
601     pass
602 ewv 1.182
603 spiga 1.179 ### CMSSW ParameterSet
604     if not self.pset is None:
605     cfg_file = common.work_space.jobDir()+self.configFilename()
606 ewv 1.357 pickleFile = common.work_space.jobDir()+self.configFilename() + '.pkl'
607 ewv 1.182 tar.add(cfg_file,self.configFilename())
608 ewv 1.357 tar.add(pickleFile,self.configFilename() + '.pkl')
609 ewv 1.313
610 spiga 1.309 try:
611     crab_cfg_file = common.work_space.shareDir()+'/crab.cfg'
612     tar.add(crab_cfg_file,'crab.cfg')
613     except:
614     pass
615 fanzago 1.93
616 fanzago 1.152 ## Add ProdCommon dir to tar
617 slacapra 1.211 prodcommonDir = './'
618     prodcommonPath = os.environ['CRABDIR'] + '/' + 'external/'
619 spiga 1.244 neededStuff = ['ProdCommon/__init__.py','ProdCommon/FwkJobRep', 'ProdCommon/CMSConfigTools', \
620 spiga 1.298 'ProdCommon/Core', 'ProdCommon/MCPayloads', 'IMProv', 'ProdCommon/Storage', \
621     'WMCore/__init__.py','WMCore/Algorithms']
622 slacapra 1.214 for file in neededStuff:
623     tar.add(prodcommonPath+file,prodcommonDir+file)
624 spiga 1.179
625     ##### ML stuff
626     ML_file_list=['report.py', 'DashboardAPI.py', 'Logger.py', 'ProcInfo.py', 'apmon.py']
627     path=os.environ['CRABDIR'] + '/python/'
628     for file in ML_file_list:
629     tar.add(path+file,file)
630    
631     ##### Utils
632 spiga 1.238 Utils_file_list=['parseCrabFjr.py','writeCfg.py', 'fillCrabFjr.py','cmscp.py']
633 spiga 1.179 for file in Utils_file_list:
634     tar.add(path+file,file)
635 ewv 1.131
636 ewv 1.182 ##### AdditionalFiles
637 slacapra 1.253 tar.dereference=True
638 spiga 1.179 for file in self.additional_inbox_files:
639     tar.add(file,string.split(file,'/')[-1])
640 slacapra 1.253 tar.dereference=False
641 spiga 1.320 common.logger.log(10-1,"Files in "+self.tgzNameWithPath+" : "+str(tar.getnames()))
642 ewv 1.182
643 slacapra 1.61 tar.close()
644 mcinquil 1.241 except IOError, exc:
645 spiga 1.320 msg = 'Could not create tar-ball %s \n'%self.tgzNameWithPath
646 spiga 1.304 msg += str(exc)
647     raise CrabException(msg)
648 mcinquil 1.241 except tarfile.TarError, exc:
649 spiga 1.320 msg = 'Could not create tar-ball %s \n'%self.tgzNameWithPath
650 spiga 1.304 msg += str(exc)
651     raise CrabException(msg)
652 spiga 1.300
653 gutsche 1.72 tarballinfo = os.stat(self.tgzNameWithPath)
654     if ( tarballinfo.st_size > self.MaxTarBallSize*1024*1024 ) :
655 spiga 1.238 msg = 'Input sandbox size of ' + str(float(tarballinfo.st_size)/1024.0/1024.0) + ' MB is larger than the allowed ' + str(self.MaxTarBallSize) \
656 ewv 1.250 +'MB input sandbox limit \n'
657 spiga 1.238 msg += ' and not supported by the direct GRID submission system.\n'
658     msg += ' Please use the CRAB server mode by setting server_name=<NAME> in section [CRAB] of your crab.cfg.\n'
659 spiga 1.358 msg += ' For further infos please see https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabServerForUsers#Server_available_for_users'
660 spiga 1.238 raise CrabException(msg)
661 gutsche 1.72
662 slacapra 1.61 ## create tar-ball with ML stuff
663 slacapra 1.97
664 spiga 1.165 def wsSetupEnvironment(self, nj=0):
665 slacapra 1.1 """
666     Returns part of a job script which prepares
667     the execution environment for the job 'nj'.
668     """
669 ewv 1.334 psetName = 'pset.py'
670    
671 slacapra 1.1 # Prepare JobType-independent part
672 ewv 1.160 txt = '\n#Written by cms_cmssw::wsSetupEnvironment\n'
673 fanzago 1.133 txt += 'echo ">>> setup environment"\n'
674 ewv 1.347 txt += 'echo "set SCRAM ARCH to ' + self.executable_arch + '"\n'
675     txt += 'export SCRAM_ARCH=' + self.executable_arch + '\n'
676     txt += 'echo "SCRAM_ARCH = $SCRAM_ARCH"\n'
677 spiga 1.290 txt += 'if [ $middleware == LCG ] || [ $middleware == CAF ] || [ $middleware == LSF ]; then \n'
678 gutsche 1.3 txt += self.wsSetupCMSLCGEnvironment_()
679 ewv 1.283 txt += 'elif [ $middleware == OSG ]; then\n'
680 gutsche 1.43 txt += ' WORKING_DIR=`/bin/mktemp -d $OSG_WN_TMP/cms_XXXXXXXXXXXX`\n'
681 ewv 1.132 txt += ' if [ ! $? == 0 ] ;then\n'
682 fanzago 1.161 txt += ' echo "ERROR ==> OSG $WORKING_DIR could not be created on WN `hostname`"\n'
683     txt += ' job_exit_code=10016\n'
684     txt += ' func_exit\n'
685 gutsche 1.3 txt += ' fi\n'
686 fanzago 1.133 txt += ' echo ">>> Created working directory: $WORKING_DIR"\n'
687 gutsche 1.3 txt += '\n'
688     txt += ' echo "Change to working directory: $WORKING_DIR"\n'
689     txt += ' cd $WORKING_DIR\n'
690 fanzago 1.133 txt += ' echo ">>> current directory (WORKING_DIR): $WORKING_DIR"\n'
691 ewv 1.131 txt += self.wsSetupCMSOSGEnvironment_()
692 spiga 1.282 #Setup SGE Environment
693 ewv 1.283 txt += 'elif [ $middleware == SGE ]; then\n'
694 spiga 1.282 txt += self.wsSetupCMSLCGEnvironment_()
695    
696 edelmann 1.289 txt += 'elif [ $middleware == ARC ]; then\n'
697     txt += self.wsSetupCMSLCGEnvironment_()
698    
699 mcinquil 1.340 #Setup PBS Environment
700 mcinquil 1.343 txt += 'elif [ $middleware == PBS ]; then\n'
701 mcinquil 1.340 txt += self.wsSetupCMSLCGEnvironment_()
702    
703 gutsche 1.3 txt += 'fi\n'
704 slacapra 1.1
705     # Prepare JobType-specific part
706     scram = self.scram.commandName()
707     txt += '\n\n'
708 fanzago 1.133 txt += 'echo ">>> specific cmssw setup environment:"\n'
709     txt += 'echo "CMSSW_VERSION = '+self.version+'"\n'
710 slacapra 1.1 txt += scram+' project CMSSW '+self.version+'\n'
711     txt += 'status=$?\n'
712     txt += 'if [ $status != 0 ] ; then\n'
713 fanzago 1.161 txt += ' echo "ERROR ==> CMSSW '+self.version+' not found on `hostname`" \n'
714     txt += ' job_exit_code=10034\n'
715 fanzago 1.163 txt += ' func_exit\n'
716 slacapra 1.1 txt += 'fi \n'
717     txt += 'cd '+self.version+'\n'
718 spiga 1.277 txt += 'SOFTWARE_DIR=`pwd`; export SOFTWARE_DIR\n'
719 fanzago 1.133 txt += 'echo ">>> current directory (SOFTWARE_DIR): $SOFTWARE_DIR" \n'
720 slacapra 1.1 txt += 'eval `'+scram+' runtime -sh | grep -v SCRAMRT_LSB_JOBNAME`\n'
721 fanzago 1.180 txt += 'if [ $? != 0 ] ; then\n'
722     txt += ' echo "ERROR ==> Problem with the command: "\n'
723     txt += ' echo "eval \`'+scram+' runtime -sh | grep -v SCRAMRT_LSB_JOBNAME \` at `hostname`"\n'
724     txt += ' job_exit_code=10034\n'
725     txt += ' func_exit\n'
726     txt += 'fi \n'
727 slacapra 1.1 # Handle the arguments:
728     txt += "\n"
729 farinafa 1.346 txt += "## number of arguments (first argument always jobnumber, the second is the resubmission number)\n"
730 slacapra 1.1 txt += "\n"
731 spiga 1.165 txt += "if [ $nargs -lt "+str(self.argsList)+" ]\n"
732 slacapra 1.1 txt += "then\n"
733 fanzago 1.161 txt += " echo 'ERROR ==> Too few arguments' +$nargs+ \n"
734     txt += ' job_exit_code=50113\n'
735     txt += " func_exit\n"
736 slacapra 1.1 txt += "fi\n"
737     txt += "\n"
738    
739     # Prepare job-specific part
740     job = common.job_list[nj]
741 ewv 1.131 if (self.datasetPath):
742 fanzago 1.93 txt += '\n'
743     txt += 'DatasetPath='+self.datasetPath+'\n'
744    
745 spiga 1.238 txt += 'PrimaryDataset='+self.primaryDataset +'\n'
746 fanzago 1.318 txt += 'DataTier='+self.dataTier+'\n'
747 fanzago 1.96 txt += 'ApplicationFamily=cmsRun\n'
748 fanzago 1.93
749     else:
750     txt += 'DatasetPath=MCDataTier\n'
751     txt += 'PrimaryDataset=null\n'
752     txt += 'DataTier=null\n'
753     txt += 'ApplicationFamily=MCDataTier\n'
754 ewv 1.170 if self.pset != None:
755 spiga 1.42 pset = os.path.basename(job.configFilename())
756 ewv 1.357 pkl = os.path.basename(job.configFilename()) + '.pkl'
757 spiga 1.42 txt += '\n'
758 spiga 1.95 txt += 'cp $RUNTIME_AREA/'+pset+' .\n'
759 ewv 1.357 txt += 'cp $RUNTIME_AREA/'+pkl+' .\n'
760 spiga 1.296
761 ewv 1.295 txt += 'PreserveSeeds=' + ','.join(self.preserveSeeds) + '; export PreserveSeeds\n'
762     txt += 'IncrementSeeds=' + ','.join(self.incrementSeeds) + '; export IncrementSeeds\n'
763     txt += 'echo "PreserveSeeds: <$PreserveSeeds>"\n'
764     txt += 'echo "IncrementSeeds:<$IncrementSeeds>"\n'
765 slacapra 1.90
766 ewv 1.184 txt += 'mv -f ' + pset + ' ' + psetName + '\n'
767 fanzago 1.360 if self.var_filter:
768     #print "self.var_filter = ",self.var_filter
769     txt += "export var_filter="+"'"+self.var_filter+"'\n"
770     txt += 'echo $var_filter'
771 ewv 1.319 else:
772 spiga 1.314 txt += '\n'
773 spiga 1.331 if self.AdditionalArgs: txt += 'export AdditionalArgs=\"%s\"\n'%(self.AdditionalArgs)
774 spiga 1.315 if int(self.NumEvents) != 0: txt += 'export MaxEvents=%s\n'%str(self.NumEvents)
775 gutsche 1.3 return txt
776 slacapra 1.176
777 fanzago 1.166 def wsUntarSoftware(self, nj=0):
778 gutsche 1.3 """
779     Put in the script the commands to build an executable
780     or a library.
781     """
782    
783 fanzago 1.166 txt = '\n#Written by cms_cmssw::wsUntarSoftware\n'
784 gutsche 1.3
785     if os.path.isfile(self.tgzNameWithPath):
786 spiga 1.358 txt += 'echo ">>> tar xzf $RUNTIME_AREA/'+os.path.basename(self.tgzNameWithPath)+' :" \n'
787 fanzago 1.285 if self.debug_wrapper==1 :
788 spiga 1.358 txt += 'tar zxvf $RUNTIME_AREA/'+os.path.basename(self.tgzNameWithPath)+'\n'
789 spiga 1.199 txt += 'ls -Al \n'
790 spiga 1.358 else:
791     txt += 'tar zxf $RUNTIME_AREA/'+os.path.basename(self.tgzNameWithPath)+'\n'
792 gutsche 1.3 txt += 'untar_status=$? \n'
793     txt += 'if [ $untar_status -ne 0 ]; then \n'
794 fanzago 1.161 txt += ' echo "ERROR ==> Untarring .tgz file failed"\n'
795     txt += ' job_exit_code=$untar_status\n'
796     txt += ' func_exit\n'
797 gutsche 1.3 txt += 'else \n'
798     txt += ' echo "Successful untar" \n'
799     txt += 'fi \n'
800 gutsche 1.50 txt += '\n'
801 slacapra 1.211 txt += 'echo ">>> Include $RUNTIME_AREA in PYTHONPATH:"\n'
802 gutsche 1.50 txt += 'if [ -z "$PYTHONPATH" ]; then\n'
803 slacapra 1.211 txt += ' export PYTHONPATH=$RUNTIME_AREA/\n'
804 gutsche 1.50 txt += 'else\n'
805 slacapra 1.211 txt += ' export PYTHONPATH=$RUNTIME_AREA/:${PYTHONPATH}\n'
806 fanzago 1.93 txt += 'echo "PYTHONPATH=$PYTHONPATH"\n'
807 gutsche 1.50 txt += 'fi\n'
808     txt += '\n'
809    
810 gutsche 1.3 pass
811 ewv 1.131
812 slacapra 1.1 return txt
813 ewv 1.170
814 fanzago 1.166 def wsBuildExe(self, nj=0):
815     """
816     Put in the script the commands to build an executable
817     or a library.
818     """
819    
820     txt = '\n#Written by cms_cmssw::wsBuildExe\n'
821     txt += 'echo ">>> moving CMSSW software directories in `pwd`" \n'
822    
823 ewv 1.170 txt += 'rm -r lib/ module/ \n'
824     txt += 'mv $RUNTIME_AREA/lib/ . \n'
825     txt += 'mv $RUNTIME_AREA/module/ . \n'
826 spiga 1.186 if self.dataExist == True:
827     txt += 'rm -r src/ \n'
828     txt += 'mv $RUNTIME_AREA/src/ . \n'
829 ewv 1.182 if len(self.additional_inbox_files)>0:
830 spiga 1.179 for file in self.additional_inbox_files:
831 spiga 1.191 txt += 'mv $RUNTIME_AREA/'+os.path.basename(file)+' . \n'
832 ewv 1.170
833 slacapra 1.211 txt += 'echo ">>> Include $RUNTIME_AREA in PYTHONPATH:"\n'
834 fanzago 1.166 txt += 'if [ -z "$PYTHONPATH" ]; then\n'
835 slacapra 1.211 txt += ' export PYTHONPATH=$RUNTIME_AREA/\n'
836 fanzago 1.166 txt += 'else\n'
837 slacapra 1.211 txt += ' export PYTHONPATH=$RUNTIME_AREA/:${PYTHONPATH}\n'
838 fanzago 1.166 txt += 'echo "PYTHONPATH=$PYTHONPATH"\n'
839     txt += 'fi\n'
840     txt += '\n'
841    
842 slacapra 1.302 if self.pset != None:
843 ewv 1.334 psetName = 'pset.py'
844    
845 slacapra 1.302 txt += '\n'
846     if self.debug_wrapper == 1:
847     txt += 'echo "***** cat ' + psetName + ' *********"\n'
848     txt += 'cat ' + psetName + '\n'
849     txt += 'echo "****** end ' + psetName + ' ********"\n'
850     txt += '\n'
851     txt += 'echo "***********************" \n'
852     txt += 'which edmConfigHash \n'
853     txt += 'echo "***********************" \n'
854 ewv 1.334 txt += 'edmConfigHash ' + psetName + ' \n'
855     txt += 'PSETHASH=`edmConfigHash ' + psetName + '` \n'
856 slacapra 1.302 txt += 'echo "PSETHASH = $PSETHASH" \n'
857     #### FEDE temporary fix for noEdm files #####
858     txt += 'if [ -z "$PSETHASH" ]; then \n'
859     txt += ' export PSETHASH=null\n'
860     txt += 'fi \n'
861     #############################################
862     txt += '\n'
863 fanzago 1.166 return txt
864 slacapra 1.1
865 ewv 1.131
866 slacapra 1.1 def executableName(self):
867 ewv 1.192 if self.scriptExe:
868 spiga 1.42 return "sh "
869     else:
870     return self.executable
871 slacapra 1.1
872     def executableArgs(self):
873 ewv 1.276 if self.scriptExe:
874 ewv 1.319 return self.scriptExe + " $NJob $AdditionalArgs"
875 fanzago 1.115 else:
876 ewv 1.334 return " -j $RUNTIME_AREA/crab_fjr_$NJob.xml -p pset.py"
877 slacapra 1.1
878     def inputSandbox(self, nj):
879     """
880     Returns a list of filenames to be put in JDL input sandbox.
881     """
882     inp_box = []
883     if os.path.isfile(self.tgzNameWithPath):
884     inp_box.append(self.tgzNameWithPath)
885 spiga 1.320 if os.path.isfile(self.argsFile):
886     inp_box.append(self.argsFile)
887 spiga 1.243 inp_box.append(common.work_space.jobDir() + self.scriptName)
888 slacapra 1.1 return inp_box
889    
890     def outputSandbox(self, nj):
891     """
892     Returns a list of filenames to be put in JDL output sandbox.
893     """
894     out_box = []
895    
896     ## User Declared output files
897 slacapra 1.54 for out in (self.output_file+self.output_file_sandbox):
898 ewv 1.131 n_out = nj + 1
899 slacapra 1.207 out_box.append(numberFile(out,str(n_out)))
900 slacapra 1.1 return out_box
901    
902    
903     def wsRenameOutput(self, nj):
904     """
905     Returns part of a job script which renames the produced files.
906     """
907    
908 ewv 1.160 txt = '\n#Written by cms_cmssw::wsRenameOutput\n'
909 fanzago 1.148 txt += 'echo ">>> current directory (SOFTWARE_DIR): $SOFTWARE_DIR" \n'
910     txt += 'echo ">>> current directory content:"\n'
911 fanzago 1.285 if self.debug_wrapper==1:
912 spiga 1.199 txt += 'ls -Al\n'
913 fanzago 1.145 txt += '\n'
914 slacapra 1.54
915 fanzago 1.128 for fileWithSuffix in (self.output_file):
916 farinafa 1.346 output_file_num = numberFile(fileWithSuffix, '$OutUniqueID')
917 slacapra 1.1 txt += '\n'
918 gutsche 1.7 txt += '# check output file\n'
919 slacapra 1.106 txt += 'if [ -e ./'+fileWithSuffix+' ] ; then\n'
920 ewv 1.147 if (self.copy_data == 1): # For OSG nodes, file is in $WORKING_DIR, should not be moved to $RUNTIME_AREA
921     txt += ' mv '+fileWithSuffix+' '+output_file_num+'\n'
922 spiga 1.209 txt += ' ln -s `pwd`/'+output_file_num+' $RUNTIME_AREA/'+fileWithSuffix+'\n'
923 ewv 1.147 else:
924     txt += ' mv '+fileWithSuffix+' $RUNTIME_AREA/'+output_file_num+'\n'
925     txt += ' ln -s $RUNTIME_AREA/'+output_file_num+' $RUNTIME_AREA/'+fileWithSuffix+'\n'
926 slacapra 1.106 txt += 'else\n'
927 fanzago 1.161 txt += ' job_exit_code=60302\n'
928     txt += ' echo "WARNING: Output file '+fileWithSuffix+' not found"\n'
929 ewv 1.156 if common.scheduler.name().upper() == 'CONDOR_G':
930 gutsche 1.7 txt += ' if [ $middleware == OSG ]; then \n'
931     txt += ' echo "prepare dummy output file"\n'
932     txt += ' echo "Processing of job output failed" > $RUNTIME_AREA/'+output_file_num+'\n'
933     txt += ' fi \n'
934 slacapra 1.1 txt += 'fi\n'
935 slacapra 1.105 file_list = []
936     for fileWithSuffix in (self.output_file):
937 farinafa 1.346 file_list.append(numberFile('$SOFTWARE_DIR/'+fileWithSuffix, '$OutUniqueID'))
938 ewv 1.131
939 spiga 1.245 txt += 'file_list="'+string.join(file_list,',')+'"\n'
940 fanzago 1.149 txt += '\n'
941 fanzago 1.148 txt += 'echo ">>> current directory (SOFTWARE_DIR): $SOFTWARE_DIR" \n'
942     txt += 'echo ">>> current directory content:"\n'
943 fanzago 1.285 if self.debug_wrapper==1:
944 spiga 1.199 txt += 'ls -Al\n'
945 fanzago 1.148 txt += '\n'
946 gutsche 1.7 txt += 'cd $RUNTIME_AREA\n'
947 fanzago 1.133 txt += 'echo ">>> current directory (RUNTIME_AREA): $RUNTIME_AREA"\n'
948 slacapra 1.1 return txt
949    
950 slacapra 1.63 def getRequirements(self, nj=[]):
951 slacapra 1.1 """
952 ewv 1.131 return job requirements to add to jdl files
953 slacapra 1.1 """
954     req = ''
955 slacapra 1.47 if self.version:
956 slacapra 1.10 req='Member("VO-cms-' + \
957 slacapra 1.47 self.version + \
958 slacapra 1.10 '", other.GlueHostApplicationSoftwareRunTimeEnvironment)'
959 ewv 1.192 if self.executable_arch:
960 gutsche 1.107 req+=' && Member("VO-cms-' + \
961 slacapra 1.105 self.executable_arch + \
962     '", other.GlueHostApplicationSoftwareRunTimeEnvironment)'
963 gutsche 1.35
964     req = req + ' && (other.GlueHostNetworkAdapterOutboundIP)'
965 spiga 1.353 if ( common.scheduler.name() in ["glite"] ):
966 slacapra 1.316 ## 25-Jun-2009 SL: patch to use Cream enabled WMS
967     if ( self.cfg_params.get('GRID.use_cream',None) ):
968     req += ' && (other.GlueCEStateStatus == "Production" || other.GlueCEStateStatus == "Special")'
969     else:
970     req += ' && other.GlueCEStateStatus == "Production" '
971 gutsche 1.35
972 slacapra 1.1 return req
973 gutsche 1.3
974     def configFilename(self):
975     """ return the config filename """
976 ewv 1.334 return self.name()+'.py'
977 gutsche 1.3
978     def wsSetupCMSOSGEnvironment_(self):
979     """
980     Returns part of a job script which is prepares
981     the execution environment and which is common for all CMS jobs.
982     """
983 ewv 1.160 txt = '\n#Written by cms_cmssw::wsSetupCMSOSGEnvironment_\n'
984     txt += ' echo ">>> setup CMS OSG environment:"\n'
985 fanzago 1.133 txt += ' echo "set SCRAM ARCH to ' + self.executable_arch + '"\n'
986     txt += ' export SCRAM_ARCH='+self.executable_arch+'\n'
987 fanzago 1.136 txt += ' echo "SCRAM_ARCH = $SCRAM_ARCH"\n'
988 ewv 1.135 txt += ' if [ -f $OSG_APP/cmssoft/cms/cmsset_default.sh ] ;then\n'
989 mkirn 1.40 txt += ' # Use $OSG_APP/cmssoft/cms/cmsset_default.sh to setup cms software\n'
990 fanzago 1.133 txt += ' source $OSG_APP/cmssoft/cms/cmsset_default.sh '+self.version+'\n'
991     txt += ' else\n'
992 fanzago 1.161 txt += ' echo "ERROR ==> $OSG_APP/cmssoft/cms/cmsset_default.sh file not found"\n'
993     txt += ' job_exit_code=10020\n'
994     txt += ' func_exit\n'
995 fanzago 1.133 txt += ' fi\n'
996 gutsche 1.3 txt += '\n'
997 fanzago 1.161 txt += ' echo "==> setup cms environment ok"\n'
998 fanzago 1.136 txt += ' echo "SCRAM_ARCH = $SCRAM_ARCH"\n'
999 gutsche 1.3
1000     return txt
1001 ewv 1.131
1002 gutsche 1.3 def wsSetupCMSLCGEnvironment_(self):
1003     """
1004     Returns part of a job script which is prepares
1005     the execution environment and which is common for all CMS jobs.
1006     """
1007 ewv 1.160 txt = '\n#Written by cms_cmssw::wsSetupCMSLCGEnvironment_\n'
1008     txt += ' echo ">>> setup CMS LCG environment:"\n'
1009 fanzago 1.133 txt += ' echo "set SCRAM ARCH and BUILD_ARCH to ' + self.executable_arch + ' ###"\n'
1010     txt += ' export SCRAM_ARCH='+self.executable_arch+'\n'
1011     txt += ' export BUILD_ARCH='+self.executable_arch+'\n'
1012     txt += ' if [ ! $VO_CMS_SW_DIR ] ;then\n'
1013 fanzago 1.161 txt += ' echo "ERROR ==> CMS software dir not found on WN `hostname`"\n'
1014     txt += ' job_exit_code=10031\n'
1015     txt += ' func_exit\n'
1016 fanzago 1.133 txt += ' else\n'
1017     txt += ' echo "Sourcing environment... "\n'
1018     txt += ' if [ ! -s $VO_CMS_SW_DIR/cmsset_default.sh ] ;then\n'
1019 fanzago 1.161 txt += ' echo "ERROR ==> cmsset_default.sh file not found into dir $VO_CMS_SW_DIR"\n'
1020     txt += ' job_exit_code=10020\n'
1021     txt += ' func_exit\n'
1022 fanzago 1.133 txt += ' fi\n'
1023     txt += ' echo "sourcing $VO_CMS_SW_DIR/cmsset_default.sh"\n'
1024     txt += ' source $VO_CMS_SW_DIR/cmsset_default.sh\n'
1025     txt += ' result=$?\n'
1026     txt += ' if [ $result -ne 0 ]; then\n'
1027 fanzago 1.161 txt += ' echo "ERROR ==> problem sourcing $VO_CMS_SW_DIR/cmsset_default.sh"\n'
1028     txt += ' job_exit_code=10032\n'
1029     txt += ' func_exit\n'
1030 fanzago 1.133 txt += ' fi\n'
1031     txt += ' fi\n'
1032     txt += ' \n'
1033 fanzago 1.161 txt += ' echo "==> setup cms environment ok"\n'
1034 gutsche 1.3 return txt
1035 gutsche 1.5
1036 spiga 1.238 def wsModifyReport(self, nj):
1037 fanzago 1.93 """
1038 ewv 1.131 insert the part of the script that modifies the FrameworkJob Report
1039 fanzago 1.93 """
1040 ewv 1.250
1041 fanzago 1.281 txt = ''
1042 fanzago 1.292 if (self.copy_data == 1):
1043 fanzago 1.281 txt = '\n#Written by cms_cmssw::wsModifyReport\n'
1044 ewv 1.283
1045 fanzago 1.175 txt += 'echo ">>> Modify Job Report:" \n'
1046 fanzago 1.217 txt += 'chmod a+x $RUNTIME_AREA/ProdCommon/FwkJobRep/ModifyJobReport.py\n'
1047 fanzago 1.175 txt += 'echo "CMSSW_VERSION = $CMSSW_VERSION"\n\n'
1048 fanzago 1.344
1049 fanzago 1.360 args = 'fjr $RUNTIME_AREA/crab_fjr_$NJob.xml json $RUNTIME_AREA/resultCopyFile n_job $OutUniqueID PrimaryDataset $PrimaryDataset ApplicationFamily $ApplicationFamily ApplicationName $executable cmssw_version $CMSSW_VERSION psethash $PSETHASH'
1050 fanzago 1.281
1051 fanzago 1.318 if (self.publish_data == 1):
1052     txt += 'ProcessedDataset='+self.processedDataset+'\n'
1053 fanzago 1.292 txt += 'echo "ProcessedDataset = $ProcessedDataset"\n'
1054     args += ' UserProcessedDataset $USER-$ProcessedDataset-$PSETHASH'
1055 fanzago 1.281
1056 fanzago 1.247 txt += 'echo "$RUNTIME_AREA/ProdCommon/FwkJobRep/ModifyJobReport.py '+str(args)+'"\n'
1057     txt += '$RUNTIME_AREA/ProdCommon/FwkJobRep/ModifyJobReport.py '+str(args)+'\n'
1058 fanzago 1.175 txt += 'modifyReport_result=$?\n'
1059     txt += 'if [ $modifyReport_result -ne 0 ]; then\n'
1060     txt += ' modifyReport_result=70500\n'
1061     txt += ' job_exit_code=$modifyReport_result\n'
1062     txt += ' echo "ModifyReportResult=$modifyReport_result" | tee -a $RUNTIME_AREA/$repo\n'
1063     txt += ' echo "WARNING: Problem with ModifyJobReport"\n'
1064     txt += 'else\n'
1065     txt += ' mv NewFrameworkJobReport.xml $RUNTIME_AREA/crab_fjr_$NJob.xml\n'
1066 spiga 1.103 txt += 'fi\n'
1067 fanzago 1.93 return txt
1068 ewv 1.283
1069 ewv 1.192 def wsParseFJR(self):
1070 spiga 1.189 """
1071 ewv 1.192 Parse the FrameworkJobReport to obtain useful infos
1072 spiga 1.189 """
1073     txt = '\n#Written by cms_cmssw::wsParseFJR\n'
1074     txt += 'echo ">>> Parse FrameworkJobReport crab_fjr.xml"\n'
1075     txt += 'if [ -s $RUNTIME_AREA/crab_fjr_$NJob.xml ]; then\n'
1076     txt += ' if [ -s $RUNTIME_AREA/parseCrabFjr.py ]; then\n'
1077 spiga 1.197 txt += ' cmd_out=`python $RUNTIME_AREA/parseCrabFjr.py --input $RUNTIME_AREA/crab_fjr_$NJob.xml --dashboard $MonitorID,$MonitorJobID '+self.debugWrap+'`\n'
1078 fanzago 1.285 if self.debug_wrapper==1 :
1079 spiga 1.197 txt += ' echo "Result of parsing the FrameworkJobReport crab_fjr.xml: $cmd_out"\n'
1080     txt += ' executable_exit_status=`python $RUNTIME_AREA/parseCrabFjr.py --input $RUNTIME_AREA/crab_fjr_$NJob.xml --exitcode`\n'
1081 spiga 1.189 txt += ' if [ $executable_exit_status -eq 50115 ];then\n'
1082     txt += ' echo ">>> crab_fjr.xml contents: "\n'
1083 spiga 1.222 txt += ' cat $RUNTIME_AREA/crab_fjr_$NJob.xml\n'
1084 spiga 1.189 txt += ' echo "Wrong FrameworkJobReport --> does not contain useful info. ExitStatus: $executable_exit_status"\n'
1085 spiga 1.197 txt += ' elif [ $executable_exit_status -eq -999 ];then\n'
1086     txt += ' echo "ExitStatus from FrameworkJobReport not available. not available. Using exit code of executable from command line."\n'
1087 spiga 1.189 txt += ' else\n'
1088     txt += ' echo "Extracted ExitStatus from FrameworkJobReport parsing output: $executable_exit_status"\n'
1089     txt += ' fi\n'
1090     txt += ' else\n'
1091     txt += ' echo "CRAB python script to parse CRAB FrameworkJobReport crab_fjr.xml is not available, using exit code of executable from command line."\n'
1092     txt += ' fi\n'
1093     #### Patch to check input data reading for CMSSW16x Hopefully we-ll remove it asap
1094 spiga 1.232 txt += ' if [ $executable_exit_status -eq 0 ];then\n'
1095 fanzago 1.273 txt += ' echo ">>> Executable succeded $executable_exit_status"\n'
1096 spiga 1.232 txt += ' fi\n'
1097 spiga 1.189 txt += 'else\n'
1098     txt += ' echo "CRAB FrameworkJobReport crab_fjr.xml is not available, using exit code of executable from command line."\n'
1099     txt += 'fi\n'
1100     txt += '\n'
1101 fanzago 1.364 txt += 'if [ $executable_exit_status -ne 0 ];then\n'
1102 fanzago 1.273 txt += ' echo ">>> Executable failed $executable_exit_status"\n'
1103     txt += ' echo "ExeExitCode=$executable_exit_status" | tee -a $RUNTIME_AREA/$repo\n'
1104     txt += ' echo "EXECUTABLE_EXIT_STATUS = $executable_exit_status"\n'
1105     txt += ' job_exit_code=$executable_exit_status\n'
1106     txt += ' func_exit\n'
1107     txt += 'fi\n\n'
1108 spiga 1.189 txt += 'echo "ExeExitCode=$executable_exit_status" | tee -a $RUNTIME_AREA/$repo\n'
1109     txt += 'echo "EXECUTABLE_EXIT_STATUS = $executable_exit_status"\n'
1110     txt += 'job_exit_code=$executable_exit_status\n'
1111    
1112     return txt
1113    
1114 gutsche 1.5 def setParam_(self, param, value):
1115     self._params[param] = value
1116    
1117     def getParams(self):
1118     return self._params
1119 gutsche 1.8
1120 spiga 1.257 def outList(self,list=False):
1121 mcinquil 1.121 """
1122     check the dimension of the output files
1123     """
1124 spiga 1.169 txt = ''
1125     txt += 'echo ">>> list of expected files on output sandbox"\n'
1126 mcinquil 1.121 listOutFiles = []
1127 ewv 1.170 stdout = 'CMSSW_$NJob.stdout'
1128 spiga 1.169 stderr = 'CMSSW_$NJob.stderr'
1129 spiga 1.268 if len(self.output_file) <= 0:
1130     msg ="WARNING: no output files name have been defined!!\n"
1131     msg+="\tno output files will be reported back/staged\n"
1132 spiga 1.304 common.logger.info(msg)
1133 ewv 1.350
1134 fanzago 1.148 if (self.return_data == 1):
1135 farinafa 1.348 for file in (self.output_file):
1136     listOutFiles.append(numberFile(file, '$OutUniqueID'))
1137     for file in (self.output_file_sandbox):
1138     listOutFiles.append(numberFile(file, '$NJob'))
1139     listOutFiles.append(stdout)
1140     listOutFiles.append(stderr)
1141    
1142 fanzago 1.161 txt += 'echo "output files: '+string.join(listOutFiles,' ')+'"\n'
1143 spiga 1.157 txt += 'filesToCheck="'+string.join(listOutFiles,' ')+'"\n'
1144 spiga 1.169 txt += 'export filesToCheck\n'
1145 spiga 1.341 taskinfo={}
1146     taskinfo['outfileBasename'] = self.output_file
1147     common._db.updateTask_(taskinfo)
1148 ewv 1.276
1149 spiga 1.257 if list : return self.output_file
1150 ewv 1.170 return txt
1151 ewv 1.355
1152     def checkCMSSWVersion(self, url = "https://cmstags.cern.ch/cgi-bin/CmsTC/", fileName = "ReleasesXML"):
1153     """
1154     compare current CMSSW release and arch with allowed releases
1155     """
1156    
1157     downloader = Downloader(url)
1158     goodRelease = False
1159    
1160     try:
1161     result = downloader.config(fileName)
1162     except:
1163     common.logger.info("ERROR: Problem reading file of allowed CMSSW releases.")
1164    
1165     try:
1166     events = pulldom.parseString(result)
1167    
1168     arch = None
1169     release = None
1170     relType = None
1171     relState = None
1172     for (event, node) in events:
1173     if event == pulldom.START_ELEMENT:
1174     if node.tagName == 'architecture':
1175     arch = node.attributes.getNamedItem('name').nodeValue
1176     if node.tagName == 'project':
1177     relType = node.attributes.getNamedItem('type').nodeValue
1178     relState = node.attributes.getNamedItem('state').nodeValue
1179     if relType == 'Production' and relState == 'Announced':
1180     release = node.attributes.getNamedItem('label').nodeValue
1181     if self.executable_arch == arch and self.version == release:
1182     goodRelease = True
1183     return goodRelease
1184    
1185     if not goodRelease:
1186     msg = "WARNING: %s on %s is not a supported release. " % \
1187     (self.version, self.executable_arch)
1188     msg += "Submission may fail."
1189     common.logger.info(msg)
1190     except:
1191     common.logger.info("Problems parsing file of allowed CMSSW releases.")
1192    
1193     return goodRelease
1194