ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cms_cmssw.py
Revision: 1.355.2.1
Committed: Fri Mar 19 14:22:03 2010 UTC (15 years, 1 month ago) by fanzago
Content type: text/x-python
Branch: CRAB_multiout
Changes since 1.355: +41 -76 lines
Log Message:
changes for json file

File Contents

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