ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cms_cmssw.py
Revision: 1.362
Committed: Tue Jul 6 16:33:23 2010 UTC (14 years, 9 months ago) by ewv
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_7_4_pre4, CRAB_2_7_4_pre3, CRAB_2_7_4_pre2
Changes since 1.361: +3 -6 lines
Log Message:
Inadvertent checkin of cms_cmssw.py

File Contents

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