ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/submit.py
Revision: 1.3
Committed: Mon Jun 21 17:41:26 2010 UTC (14 years, 10 months ago) by paus
Content type: text/x-python
Branch: MAIN
CVS Tags: Mit_014b
Changes since 1.2: +44 -22 lines
Log Message:
Translation table for CE node name to generic site name.

File Contents

# User Rev Content
1 paus 1.2 #!/usr/bin/env python
2     #---------------------------------------------------------------------------------------------------
3     # Script to test, create and submit one complete production task
4     #
5     # Complete refers here to the proper preparation of the ultimate storage location (a storge element
6     # with a given storage path etc.), the submission of a test job to evaluate timing and data output
7     # size and finally the creation of the job configurations and the submission of the jobs to the grid
8     # sites. The grid is accessed via crab tools.
9     #
10     # While the script is pretty complete it has become a bit long and messy. Some cleanup will
11     # hopefully soon be performed.
12     #
13     # Author: C.Paus (July 1, 2008)
14     #---------------------------------------------------------------------------------------------------
15     import os,sys,getopt,re,string
16 paus 1.3 import task,translator
17 paus 1.2
18     #===================================================================================================
19     def searchReplace(line,mitCfg,version,mitDataset, \
20     cmsDataset='X',cmsswPy='X',dbs='X',sched='X',blacklist='X',skpEvts='X'):
21     # compile search and replacement sequences
22     pCmsDset = re.compile('XX-CMSDATASET-XX')
23     pMitDset = re.compile('XX-MITDATASET-XX')
24     pCmsswPy = re.compile('XX-CMSSWPY-XX')
25     pMitCfg = re.compile('XX-MITCFG-XX')
26     pMitVers = re.compile('XX-MITVERSION-XX')
27     pDbs = re.compile('XX-DBS-XX')
28     pSched = re.compile('XX-SCHED-XX')
29     pBlacklist = re.compile('XX-BLACKLIST-XX')
30     pSkpEvts = re.compile('XX-SKIPEVTS-XX')
31     # perform all search and replaces
32     line = pCmsDset .sub(cmsDataset, line);
33     line = pMitDset .sub(mitDataset, line);
34     line = pCmsswPy .sub(cmsswPy, line);
35     line = pMitCfg .sub(mitCfg, line);
36     line = pMitVers .sub(version, line);
37     line = pDbs .sub(dbs, line);
38     line = pSched .sub(sched, line);
39     line = pBlacklist.sub(blacklist, line);
40     return line
41    
42     #===================================================================================================
43     def adjustCfg(line,nevents,crabId):
44     # compile search and replacement sequences
45     pNevents = re.compile('XX-NEVENTS-XX')
46     pCrabId = re.compile('XX-CRABID-XX')
47     # perform all search and replaces
48     line = pNevents .sub(str(nevents),line);
49     line = pCrabId .sub(crabId, line);
50     return line
51    
52     #===================================================================================================
53     def findStoragePath(mitCfg,version,mitDataset):
54     # find the forseen storage place
55 paus 1.3 cmd = 'grep ^storage_path ' + os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/crab.cfg'
56 paus 1.2 for file in os.popen(cmd).readlines():
57     line = file[:-1]
58     line = searchReplace(line,mitCfg,version,mitDataset);
59     # decode the storage directory name
60     names = line.split("=")
61     names = names[1:]
62     storagePath = "=".join(names)
63     storagePath = re.sub("\s", "",storagePath)
64 paus 1.3 cmd = 'grep ^user_remote_dir ' + os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/crab.cfg'
65 paus 1.2 for file in os.popen(cmd).readlines():
66     line = file[:-1]
67     line = searchReplace(line,mitCfg,version,mitDataset);
68     # decode the storage directory name
69     names = line.split(" ")
70     storagePath += names[-1]
71     return storagePath
72    
73     #===================================================================================================
74     def createDirCern(storagePath):
75     # check whether path exists
76     cmd = 'rfdir ' + storagePath + ' >& /dev/null'
77     status = os.system(cmd)
78    
79     # create it if missing
80     if status == 0:
81     print ' Castor directory exists: ' + storagePath + '\n --> Moving on.'
82     else:
83     print ' Castor directory needs to be created.'
84     cmd = 'rfmkdir -p ' + storagePath
85     status = os.system(cmd)
86     if status == 0:
87     print ' --> Created: ' + storagePath
88    
89     # always set the permissions
90     cmd = 'rfchmod 777 ' + storagePath
91     status = os.system(cmd)
92     if status == 0:
93     print ' --> Set permissions: 777\n'
94     else:
95     print ' --> Setting permissions failed. EXIT now.\n'
96     sys.exit(1)
97    
98     #===================================================================================================
99     def createDirGeneral(storageEle,storagePath):
100     # create all relevant subdirectories
101     f = storagePath.split('/') # splitting every '/'
102     storagePath2 = "/".join(f[:-1])
103     storagePath1 = "/".join(f[:-2])
104     storagePath0 = "/".join(f[:-3])
105    
106     # set the storage URL
107     storageUrl = 'srm://' + storageEle + ':8443' + storagePath
108    
109     # check whether path exists
110     cmd = 'srmls ' + storageUrl + ' >& /dev/null'
111     status = os.system(cmd)
112    
113     # create it only if missing
114     if status == 0:
115     print ' '
116     print ' Directory already found.... moving on.'
117    
118     else:
119     # create all relevant directories
120     cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath0 + ' >& /dev/null'
121     print ' srmmkdir: ' + cmd
122     status = os.system(cmd)
123     print ' srmmkdir: status %d'%(status)
124     cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath1 + ' >& /dev/null'
125     print ' srmmkdir: ' + cmd
126     status = os.system(cmd)
127     print ' srmmkdir: status %d'%(status)
128     cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath2 + ' >& /dev/null'
129     print ' srmmkdir: ' + cmd
130     status = os.system(cmd)
131     print ' srmmkdir: status %d'%(status)
132    
133     # create the main storage directory
134     cmd = 'srmmkdir srm://' + storageEle + ':8443' + storagePath + ' >& /dev/null'
135     print ' srmmkdir: ' + cmd
136     status = os.system(cmd)
137     print ' srmmkdir: status %d'%(status)
138     if status == 0:
139     print ' '
140     print ' Directory was created.'
141     else:
142     print ' '
143     print ' '
144     print ' '
145     print ' ERROR - failed to create the Storage Area.'
146     print ' '
147     print ' '
148    
149     print ' Check permissions with: srmls -l -count=1 ' + storageUrl + '\n'
150     cmd = 'srmls -l -count=1 ' + storageUrl + ' | grep GroupPermission | grep RWX'
151     status = -1
152     for line in os.popen(cmd).readlines(): # run command
153     line = line[:-1]
154     print " Permissions? " + line
155     status = 0
156     #status = os.system(cmd)
157     print ' '
158    
159     if status != 0:
160     print ' --> Directory creation or permissions failed. EXIT now.\n'
161     sys.exit(1)
162    
163     #===================================================================================================
164     # Main starts here
165     #===================================================================================================
166     # Define string to explain usage of the script
167     usage = "Usage: submit.py --cmsDataset=<name> | --mitDataset=<name>\n"
168     usage += " --cmssw=<name>\n"
169     usage += " --mitCfg=<name>\n"
170     usage += " --version=<version>\n"
171     usage += " --dbs=<name>\n"
172     usage += " --sched=<name>\n"
173     usage += " --blacklist=<name>\n"
174     usage += " --nSubmit=<submittedJobs>\n"
175     usage += " --skipEvents=<'nRunX:nEventY','nRunXX:nEventYY',...>\n"
176     usage += " --complete\n"
177     usage += " --testJob\n"
178     usage += " --noTestJob\n"
179     usage += " --help\n"
180    
181     # Define the valid options which can be specified and check out the command line
182     valid = ['cmsDataset=','mitDataset=','cmssw=','mitCfg=','version=','dbs=','sched=','blacklist=',
183     'nSubmit=','skipEvents=','complete','testJob','noTestJob','test','help']
184     try:
185     opts, args = getopt.getopt(sys.argv[1:], "", valid)
186     except getopt.GetoptError, ex:
187     print usage
188     print str(ex)
189     sys.exit(1)
190    
191     # --------------------------------------------------------------------------------------------------
192     # Get all parameters for the production
193     # --------------------------------------------------------------------------------------------------
194     # Set defaults for each option
195     cmsDataset = None
196     mitDataset = None
197     cmd = "date +crab_0_%y%m%d_%H%M%S"
198     for line in os.popen(cmd).readlines(): # run command
199     line = line[:-1]
200     crabId = line
201     print "\n This job will be CrabId: " + crabId + "\n"
202    
203     cmssw = "cmssw"
204 paus 1.3 mitCfg = "filefi"
205     version = "014"
206 paus 1.2 #dbs = "https://cmsdbsprod.cern.ch:8443/cms_dbs_prod_global/servlet/DBSServlet"
207     dbs = "http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
208     sched = "glite"
209     blacklist = ""
210     nSubmit = -1
211     skpEvts = ''
212     complete = 0
213     noTestJob = 0
214     testJob = 0
215     test = 0
216    
217     # Read new values from the command line
218     for opt, arg in opts:
219     if opt == "--help":
220     print usage
221     sys.exit(0)
222     if opt == "--cmsDataset":
223     cmsDataset = arg
224     if opt == "--mitDataset":
225     mitDataset = arg
226     if opt == "--cmssw":
227     cmssw = arg
228     if opt == "--mitCfg":
229     mitCfg = arg
230     if opt == "--version":
231     version = arg
232     if opt == "--dbs":
233     dbs = arg
234     if opt == "--sched":
235     sched = arg
236     if opt == "--blacklist":
237     blacklist = arg
238     if opt == "--nSubmit":
239     nSubmit = arg
240     if opt == "--skipEvents":
241     skpEvts = arg
242     if opt == "--complete":
243     complete = 1
244     if opt == "--noTestJob":
245     noTestJob = 1
246     testJob = 0
247     if opt == "--testJob":
248     testJob = 1
249     noTestJob = 0
250     if opt == "--test":
251     test = 1
252    
253     # Make sure we have the right 'database' and the right config file
254     database = 'Productions'
255 paus 1.3 cmsswPy = cmssw + '_' + crabId + '.py'
256 paus 1.2 if cmssw != 'cmssw':
257     database += '.' + cmssw
258    
259     # Deal with obvious problems
260     if cmsDataset == None and mitDataset == None:
261     cmd = "--cmsDataset | --mitDataset options not provided. One of them is required."
262     raise RuntimeError, cmd
263    
264 paus 1.3 crabFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'crab.cfg'
265 paus 1.2 if not os.path.exists(crabFile):
266     cmd = "Crab file not found: %s" % crabFile
267     raise RuntimeError, cmd
268 paus 1.3 cmsswFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + cmssw + '.py'
269 paus 1.2 if not os.path.exists(cmsswFile):
270     cmd = "Cmssw file not found: %s" % cmsswFile
271     cmd = " XXXX ERROR no valid configuration found XXXX"
272     raise RuntimeError, cmd
273    
274 paus 1.3
275     # Prepare the ce/se translator
276     translator = translator.Translator(os.environ['MIT_PROD_DIR']+'/'+mitCfg+'/'+version+'/ceTable',
277     os.environ['MIT_PROD_DIR']+'/'+mitCfg+'/'+version+'/seTable')
278    
279 paus 1.2 # Resolve the other mitCfg parameters from the configuration file
280 paus 1.3 cmd = 'cat ' + os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + database
281 paus 1.2
282     join = 0
283     fullLine = ""
284     bSlash = "\\";
285     for line in os.popen(cmd).readlines(): # run command
286     line = line[:-1]
287     # get ride of empty or commented lines
288     if line == '' or line[0] == '#':
289     continue
290    
291     # join lines
292     if join == 1:
293     fullLine += line
294     else:
295     fullLine = line
296    
297     # determine if finished or more is coming
298     if fullLine[-1] == bSlash:
299     join = 1
300     fullLine = fullLine[:-1]
301     else:
302     join = 0
303     # test whether there is a directory
304     names = fullLine.split() # splitting every blank
305     if names[0] == cmsDataset or names[1] == mitDataset:
306     cmsDataset = names[0] # CMS name of the dataset
307     mitDataset = names[1] # equivalent MIT name of the dataset
308     nevents = int(names[2]) # number of events to be used in the production
309     if names[4] != "-":
310     localPath = names[4]
311     print "\n Sample info from database %s for CMSSW config %s\n %s"\
312     %(database,cmsswPy,fullLine)
313     if len(names) == 6:
314     dbs = names[5]
315     dbs = 'https://cmsdbsprod.cern.ch:8443/cms_dbs_' + dbs + '/servlet/DBSServlet'
316     print ' dbs: ' + dbs + '\n'
317     else:
318     print ''
319    
320     if mitDataset == None or cmsDataset == None:
321     print "ERROR - dataset not defined."
322     sys.exit(1)
323    
324     # Prepare file based processing input
325 paus 1.3 runFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'run.sh'
326 paus 1.2 if not os.path.exists(runFile):
327     cmd = "Run file not found: %s" % runFile
328     raise RuntimeError, cmd
329     cmd = 'cp ' + runFile + ' ./'
330     os.system(cmd)
331 paus 1.3 writeCfgFile = os.environ['MIT_PROD_DIR'] + '/' + mitCfg + '/' + version + '/' + 'writeCfg.py'
332     cmd = 'cp ' + writeCfgFile + ' ./'
333     os.system(cmd)
334 paus 1.2
335     lfnFile = mitCfg + '/' + version + '/' + mitDataset + '.lfns'
336     if os.path.exists(lfnFile):
337     cmd = "Lfn file found: %s. This means someone already worked on this dataset." % lfnFile
338     #### raise RuntimeError, cmd
339     else:
340     cmd = 'input.py --option=lfn --dataset=' + cmsDataset + ' > ' + lfnFile
341     print ' Input: ' + cmd
342     os.system(cmd)
343    
344     # Create the corresponding crab task
345     crabTask = task.Task(crabId,cmsDataset,mitCfg,version)
346     crabTask.storagePath = findStoragePath(mitCfg,version,mitDataset)
347     crabTask.loadAllLfns(lfnFile)
348     crabTask.loadCompletedLfns()
349     crabTask.createMissingLfns(lfnFile,lfnFile + '_' + crabTask.tag)
350     crabTask.createSubTasks(lfnFile + '_' + crabTask.tag)
351     cmd = 'cp ' + lfnFile + '_' + crabTask.tag + '_*' + ' ./'
352     os.system(cmd)
353    
354     nevents = len(crabTask.lfns)
355    
356     # Say what we do now
357     print ' Preparing dataset: ' + cmsDataset + ' [MIT: ' + mitDataset + ' with ' + str(nevents) + \
358     ' total jobs(=lfns), nEvtsTotal: %d]' % crabTask.nTotalEvts
359    
360     # --------------------------------------------------------------------------------------------------
361     # Prepare the config files
362     # --------------------------------------------------------------------------------------------------
363     # Cleaning up
364 paus 1.3 cmd = "rm -f crab_" + crabTask.tag + ".cfg crab_" + crabTask.tag + ".cfg-Template " + cmsswPy
365 paus 1.2 os.system(cmd)
366    
367     # Parse template input and write the crab configuration file
368     fileInput = open(crabFile,'r')
369 paus 1.3 fileOutput = open("crab_" + crabTask.tag + ".cfg-Template",'w')
370 paus 1.2 line = fileInput.readline()
371     while (line != ''):
372     if line[0] != '#':
373     line = searchReplace(line,mitCfg,version,mitDataset, \
374     cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
375     fileOutput.write(line)
376     line = fileInput.readline()
377     fileInput .close()
378     fileOutput.close()
379    
380     # Parse template input and write the crab configuration file
381     fileInput = open(cmsswFile,'r')
382     fileOutput = open(cmsswPy,'w')
383     line = fileInput.readline()
384     while (line != ''):
385     if line[0] != '#':
386     line = searchReplace(line,mitCfg,version,mitDataset, \
387     cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
388     fileOutput.write(line)
389     line = fileInput.readline()
390     fileInput .close()
391     fileOutput.close()
392    
393     # --------------------------------------------------------------------------------------------------
394     # Job creation and submission
395     # --------------------------------------------------------------------------------------------------
396     print '\n Using CMSSW version: ' + os.environ['CMSSW_VERSION']
397     print ' Using CRAB version: ' + os.environ['CRAB_VERS'] + '\n'
398    
399     pattern1 = 'working directory'
400     pattern2 = 'job\(s\) can run on'
401    
402     # Go through the crabTask and submit each subtask to the grid
403     for subTask in crabTask.subTasks:
404    
405     tag = crabTask.tag + '_' + subTask.tag()
406     print ' Working on subtask: ' + tag
407    
408     nJobsTotal = 0
409     crabIdCheck = ''
410    
411     # adjust crab config
412 paus 1.3 fileInput = open("crab_" + crabTask.tag + ".cfg-Template",'r')
413     fileOutput = open("crab_" + crabTask.tag + ".cfg",'w')
414 paus 1.2 line = fileInput.readline()
415     while (line != ''):
416     if line[0] != '#':
417     line = adjustCfg(line,subTask.nSubTaskLfn,tag)
418     fileOutput.write(line)
419     line = fileInput.readline()
420     fileInput .close()
421     fileOutput.close()
422    
423     # ----------------------------------------------------------------------------------------------
424     # Deal with storage element area
425     # ----------------------------------------------------------------------------------------------
426     # find the forseen storage place
427 paus 1.3 cmd = 'grep ^storage_element crab_' + crabTask.tag + '.cfg-Template'
428 paus 1.2 for file in os.popen(cmd).readlines(): # run command
429     line = file[:-1] # strip '\n'
430     # decode the storage element name
431     names = line.split("=") # splitting every '='
432     storageEle = names.pop()
433     storageEle = re.sub("\s", "",storageEle)
434 paus 1.3 cmd = 'grep ^storage_path crab_' + crabTask.tag + '.cfg-Template'
435 paus 1.2 for file in os.popen(cmd).readlines(): # run command
436     line = file[:-1] # strip '\n'
437     # decode the storage directory name
438     names = line.split("=") # splitting every '='
439     names = names[1:]
440     storagePath = "=".join(names)
441     storagePath = re.sub("\s", "",storagePath)
442 paus 1.3 cmd = 'grep ^user_remote_dir crab_' + crabTask.tag + '.cfg-Template'
443 paus 1.2 for file in os.popen(cmd).readlines(): # run command
444     line = file[:-1] # strip '\n'
445     # decode the storage directory name
446     names = line.split(" ") # splitting every '='
447     storagePath += names[-1]
448     storageUrl = 'srm://' + storageEle + ':8443' + storagePath
449    
450     storagePath = adjustCfg(storagePath,subTask.nSubTaskLfn,tag)
451     storageUrl = adjustCfg(storageUrl, subTask.nSubTaskLfn,tag)
452    
453     print ' StorageUrl: ' + storageUrl
454    
455     # Create storage area
456     if test == 0: # for testing we do not create the directories
457     if (storageEle == 'srm.cern.ch'):
458     createDirCern(storagePath)
459     else:
460     createDirGeneral(storageEle,storagePath)
461    
462     # cmd = "crab -create -debug 3 -USER.ui_working_dir=" + tag + " | tee forDaniele "
463 paus 1.3 cmd = "crab -create -cfg crab_" + crabTask.tag + ".cfg -USER.ui_working_dir=" + tag
464 paus 1.2 print ' -> ' + cmd
465     if test != 0:
466     cmd = 'echo ' + cmd
467     print ' ' + cmd
468     for line in os.popen(cmd).readlines(): # run command
469     line = line[:-1] # strip '\n'
470     print ' CRAB: ' + line
471     if re.search(pattern1,line):
472     f = line.split("/")
473     crabIdCheck = f[-2]
474     if re.search(pattern2,line):
475     f = line.split(" ")
476     nJobsTotal = f[1]
477     if nJobsTotal == '':
478     nJobsTotal = f[2]
479 paus 1.3 # report
480 paus 1.2 print ' --> %s jobs created (%s).\n'%(nJobsTotal,tag)
481 paus 1.3 # and cleanup the temporary file for the subtask
482     cmd = "rm -f " + mitDataset + ".lfns_" + tag
483     os.system(cmd)
484    
485 paus 1.2 # adjust arguments
486     cmd = 'input.py --db=' + lfnFile + '_' + tag + ' --option=xml --dataset=' + cmsDataset + \
487     ' > ' + tag + '/share/arguments.xml'
488     print ' update arguments: ' + cmd
489     if test == 0:
490     os.system(cmd)
491    
492     # loop through the file and determine the submission parameters
493     block = ''
494     blocks = []
495     idx = 0
496     minIdxs = []
497     maxIdxs = []
498    
499     fileInput = open(lfnFile + '_' + tag,'r')
500     line = fileInput.readline()
501     while (line != ''):
502     line = line[:-1]
503     if line[0] != '#':
504     idx += 1
505     f = line.split()
506     block = f[0]
507     lfn = f[1]
508     nEvents = f[2]
509     if len(blocks) == 0 or block != blocks[-1]:
510     # new block found
511     blocks .append(block)
512     minIdxs.append(idx)
513     maxIdxs.append(idx)
514     else:
515     maxIdxs[-1] = idx
516     # keep the reading going
517     line = fileInput.readline()
518     fileInput .close()
519    
520     # merge blocks together if they use the same sites
521     lastSites = ''
522     idx = 0
523     mergedIdx = 0
524     mergedBlocks = []
525     mergedSites = []
526     mergedMinIdxs = []
527     mergedMaxIdxs = []
528     print '\n Show the unmerged blocks:'
529     for block in blocks:
530     cmd = "sites.py --block=" + block
531     for line in os.popen(cmd).readlines(): # run command
532     line = line[:-1]
533     sites = line
534     print ' Block ' + block + ' process: %d to %d'%(minIdxs[idx],maxIdxs[idx]) + \
535     ' at\n > ' + sites
536     # block with different sites found
537     if sites != lastSites:
538     lastSites = sites
539     mergedSites .append(sites)
540     mergedBlocks .append(blocks[idx])
541     mergedMinIdxs.append(minIdxs[idx])
542     mergedMaxIdxs.append(maxIdxs[idx])
543     mergedIdx =+ 1
544     else:
545     mergedMaxIdxs[mergedIdx-1] = maxIdxs[idx]
546     # last action in the loop: increment the unmerged blocks
547     idx += 1
548    
549     # Show already what we will do
550     idx = 0
551     print '\n Show the merged blocks:'
552     for block in mergedBlocks:
553     print ' Merged Block ' + block + ' process: %d to %d'\
554     %(mergedMinIdxs[idx],mergedMaxIdxs[idx]) + \
555     ' at\n > ' + mergedSites[idx]
556     # last action in the loop: increment the merged blocks
557     idx += 1
558    
559     # perfrom the submission block by block (using the merged blocks of course)
560     nSubmission = len(mergedBlocks)
561     idx = 0
562     print '\n Submit the merged blocks:'
563     for block in mergedBlocks:
564     print ' Merged Block ' + block + ' process: %d to %d'\
565     %(mergedMinIdxs[idx],mergedMaxIdxs[idx]) + \
566     ' at\n > ' + mergedSites[idx]
567    
568     nSubmit = '%d-%d'%(mergedMinIdxs[idx],mergedMaxIdxs[idx])
569     if mergedMinIdxs[idx] == mergedMaxIdxs[idx]:
570     nSubmit = '%d,%d'%(mergedMinIdxs[idx],100000000)
571 paus 1.3 translatedSites = translator.translateSes(mergedSites[idx])
572     cmd = 'crab -submit %s -continue %s -GRID.ce_white_list=%s'%(nSubmit,tag,translatedSites)
573 paus 1.2 print ' ' + cmd
574 paus 1.3 status = os.system(cmd)
575     while status != 0:
576     print ' Submission failed (%s) --> retry'%(cmd)
577     status = os.system(cmd)
578    
579 paus 1.2 # last action in the loop: increment the merged blocks
580     idx += 1
581    
582     print ' Number of blocks submitted: %d' % nSubmission
583    
584 paus 1.3
585     # and cleanup the temporary file for the task
586     cmd = "rm -f crab_" + crabTask.tag + ".cfg crab_" + crabTask.tag + ".cfg-Template " \
587     + cmsswPy + ' ' + cmsswPy + 'c'
588     os.system(cmd)
589    
590 paus 1.2 print ' Done... keep watching it...'
591     sys.exit(0)
592    
593    
594     ## if nSubmit != -1:
595     ## cmd = 'crab -continue %s -submit %s'%(crabId,nSubmit)
596     ## print 'SUBMIT.PY: ' + cmd
597     ## status = os.system(cmd)
598     ## if status == 0:
599     ## print ' --> job submitted\n'
600     ## else:
601     ## leftOver = int(nJobsTotal)
602     ## nSubmitted = 0
603     ## nSubBatch = 80
604     ## while (nSubmitted < int(nJobsTotal)):
605     ## if leftOver < nSubBatch:
606     ## nSubBatch = leftOver
607     ##
608     ## cmd = 'crab -continue %s -submit %d'%(crabId,nSubBatch)
609     ## print 'SUBMIT.PY: ' + cmd
610     ## status = os.system(cmd)
611     ## if status == 0:
612     ## print ' --> %d job submitted\n'%(nSubBatch)
613     ## leftOver = leftOver - nSubBatch
614     ## nSubmitted = nSubmitted + nSubBatch
615     ##
616     ## #print 'SUBMIT.PY: ' + cmd
617     ## #status = os.system(cmd)
618     ## #if status == 0:
619     ## # print ' --> job submitted\n'
620     ##
621    
622     # --------------------------------------------------------------------------------------------------
623     # Run a test job to test the configuration and measure the expected output size
624     # --------------------------------------------------------------------------------------------------
625     if noTestJob == 0:
626     #-----------------------------------------------------------------------------------------------
627     # use a specific file for test
628     #-----------------------------------------------------------------------------------------------
629     # first check whether we already have a local file
630     print '\n Try to find an existing local file using "find"'
631     f = cmsDataset.split("/")
632     name = f[1]
633     vers = f[2]
634     tier = f[3]
635     file = ""
636     lfn = ""
637     cmd = 'find ./store/ -name ' + name
638     print 'Searching: ' + cmd
639     for line in os.popen(cmd).readlines(): # run command
640     file = line[:-1] # strip '\n'
641    
642     # looks like there could be a file we found a directory, confirm
643     if file != "":
644     print '\n We have a directory ' + file + ' .. confirming'
645     file = file + '/' + tier
646     cmd = 'find ' + file + ' -name \*.root'
647     for line in os.popen(cmd).readlines(): # run command
648     if line.find(vers):
649     file = line[:-1] # strip '\n'
650     lfn = file[1:]
651    
652     if os.path.exists(file):
653     print ' moving on with locally found file: \n' + ' ' + file
654     else:
655     print ' no local file found'
656     file = ""
657     lfn = ""
658    
659     # now try to see whether we can find a file to download
660     if file == "":
661     cmd = './bin/findLfn.py --input=' + cmsDataset + ' | grep /store/'
662     print '\n Find an LFN to download: ' + cmd
663     for line in os.popen(cmd).readlines(): # run command
664     if line.find("/store") != -1:
665     lfn = line[:-1] # strip '\n'
666     break
667     if lfn == "":
668     print "\n WARNING: No file found, continue assuming it is a simulation job.\n\n"
669     else:
670     print ' --> LFN: ' + lfn
671     file = '.' + lfn
672    
673     if os.path.exists(file):
674     print ' --> File already exists: ' + file
675     else:
676     cmd = './bin/downloadLfn.py ' + lfn
677     print ' --> downloading: ' + cmd
678     status = os.system(cmd)
679     if status != 0:
680     print ' ERROR - failed to copy LFN. EXIT now!'
681     sys.exit(1)
682    
683     # Parse template input and adjust the input file to the newly copied lfn
684     fileInput = open(cmsswPy,'r')
685     fileOutput = open("test-"+cmsswPy,'w')
686     line = fileInput.readline()
687     while (line != ''):
688     if line.find("file:") != -1:
689     line = '"file:' + lfn[1:] + '"\n'
690     fileOutput.write(line)
691     line = fileInput.readline()
692     fileInput .close()
693     fileOutput.close()
694    
695     # Setting the number of events (hard coded in the file so far)
696     nTryEvts = 1000.
697    
698     print '\n --> Please wait, running a test job now! Should be short (trying %.0f'%nTryEvts + \
699     ' evts). Check log: cmssw.log'
700     cmd = 'rm -f cmssw.log; /usr/bin/time --format "%e %U %S" cmsRun test-' + cmsswPy + \
701     ' >& cmssw.log'
702     print ' CMD: ' + cmd
703     status = os.system(cmd)
704    
705     cmd = 'tail -1 cmssw.log'
706     for file in os.popen(cmd).readlines(): # run command
707     line = file[:-1] # strip '\n'
708     f = line.split() # splitting every blank
709     rtime = float(f[0]) # wall clock time
710     utime = float(f[1]) # user time
711     stime = float(f[2]) # system time
712    
713     nEvtsTest = 1000
714     cmd = 'grep \'Begin processing\' cmssw.log | tail -1'
715     for file in os.popen(cmd).readlines(): # run command
716     line = file[:-1] # strip '\n'
717     # test whether there is a directory
718     f = line.split() # splitting every blank
719     nEvtsTest = f[3] # this is the number of records processed
720     nEvtsTest = int(nEvtsTest[:-2]) # strip 'th'
721     cmd = 'ls -s ' + mitDataset + '*.root'
722     size = 0
723     for file in os.popen(cmd).readlines(): # run command
724     line = file[:-1] # strip '\n'
725     f = line.split() # splitting every blank
726     size += int(f[0])/1000. # size in MB
727    
728     cmd = 'tail -1 cmssw.log'
729     for file in os.popen(cmd).readlines(): # run command
730     line = file[:-1] # strip '\n'
731     # get total, user and system times
732     names = line.split() # splitting every blank
733    
734     if nEvtsTest != nTryEvts:
735     print ' WARNING - Instead of %f did %d'%(nTryEvts,nEvtsTest)
736    
737     print ' '
738     print ' Number of test events produced: %d'%nEvtsTest
739     print ' File size for all events: %.2f MB'%size
740     print ' Processing time for all events: %.2f secs (u: %.2f s: %.2f)'%(rtime,utime,stime)
741     print ' '
742     print ' --> 1 event == %.2f secs'%(rtime/nEvtsTest)
743     print ' --> 1.00 GB == %d events'%(nEvtsTest/size*1024.)
744     print ' --> %.2f GB == %d events'%(nevents/(nEvtsTest/size*1024.),nevents)
745     print ' '
746    
747     if testJob == 1:
748     print '\n Test job finished, stopping now.\n'
749     sys.exit(0)
750    
751    
752     ## # are we just completing an existing production? and is there something to complete?
753     ## if complete == 1:
754     ## f = storagePath.split('=')
755     ## rfDir = f[-1]
756     ## #cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,rfDir)
757     ## cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,storageUrl)
758     ## #print ' CMD: ' + cmd
759     ## for line in os.popen(cmd).readlines(): # run command
760     ## line = line[:-1] # strip '\n'
761     ## f = line.split(':')
762     ## nSubmit = f[1].strip()
763     ## f = nSubmit.split(',')
764     ## if len(f) == 0 or nSubmit == '':
765     ## print ' No more jobs left it seems, nSubmit=' + nSubmit
766     ## cmd = 'rm -rf ' + crabId
767     ## print ' Cleanup: ' + cmd + '\n\n'
768     ## status = os.system(cmd)
769     ## sys.exit(0)
770     ## elif len(f) == 1:
771     ## nInvalid = str(int(nJobsTotal) + 1000)
772     ## print ' One more jobs left, careful, adjusted, nSubmit=' + nSubmit
773     ## nSubmit = nSubmit + ',' + nInvalid
774     ##
775     ## ### nSubmit = ",".join(f[:-1])
776     ## print ' Missing jobs are: ' + nSubmit
777     ##
778     ## sys.exit(0)