ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/submit.py
Revision: 1.2
Committed: Sat Jun 5 02:36:28 2010 UTC (14 years, 11 months ago) by paus
Content type: text/x-python
Branch: MAIN
CVS Tags: Mit_014a
Changes since 1.1: +756 -0 lines
Log Message:
Wow I forgot all about my cvs.

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     import task
17    
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     cmd = 'grep ^storage_path ' + mitCfg + '/' + version + '/crab.cfg'
56     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     cmd = 'grep ^user_remote_dir ' + mitCfg + '/' + version + '/crab.cfg'
65     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     mitCfg = "filler"
205     version = "013"
206     #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     cmsswPy = cmssw + '.py'
256     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     crabFile = mitCfg + '/' + version + '/' + 'crab.cfg'
265     if not os.path.exists(crabFile):
266     cmd = "Crab file not found: %s" % crabFile
267     raise RuntimeError, cmd
268     cmsswFile = mitCfg + '/' + version + '/' + cmsswPy
269     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     # Resolve the other mitCfg parameters from the configuration file
275     cmd = 'cat ' + mitCfg + '/' + version + '/' + database
276    
277     join = 0
278     fullLine = ""
279     bSlash = "\\";
280     for line in os.popen(cmd).readlines(): # run command
281     line = line[:-1]
282     # get ride of empty or commented lines
283     if line == '' or line[0] == '#':
284     continue
285    
286     # join lines
287     if join == 1:
288     fullLine += line
289     else:
290     fullLine = line
291    
292     # determine if finished or more is coming
293     if fullLine[-1] == bSlash:
294     join = 1
295     fullLine = fullLine[:-1]
296     else:
297     join = 0
298     # test whether there is a directory
299     names = fullLine.split() # splitting every blank
300     if names[0] == cmsDataset or names[1] == mitDataset:
301     cmsDataset = names[0] # CMS name of the dataset
302     mitDataset = names[1] # equivalent MIT name of the dataset
303     nevents = int(names[2]) # number of events to be used in the production
304     if names[4] != "-":
305     localPath = names[4]
306     print "\n Sample info from database %s for CMSSW config %s\n %s"\
307     %(database,cmsswPy,fullLine)
308     if len(names) == 6:
309     dbs = names[5]
310     dbs = 'https://cmsdbsprod.cern.ch:8443/cms_dbs_' + dbs + '/servlet/DBSServlet'
311     print ' dbs: ' + dbs + '\n'
312     else:
313     print ''
314    
315     if mitDataset == None or cmsDataset == None:
316     print "ERROR - dataset not defined."
317     sys.exit(1)
318    
319     # Prepare file based processing input
320     runFile = mitCfg + '/' + version + '/' + 'run.sh'
321     if not os.path.exists(runFile):
322     cmd = "Run file not found: %s" % runFile
323     raise RuntimeError, cmd
324     cmd = 'cp ' + runFile + ' ./'
325     os.system(cmd)
326    
327     lfnFile = mitCfg + '/' + version + '/' + mitDataset + '.lfns'
328     if os.path.exists(lfnFile):
329     cmd = "Lfn file found: %s. This means someone already worked on this dataset." % lfnFile
330     #### raise RuntimeError, cmd
331     else:
332     cmd = 'input.py --option=lfn --dataset=' + cmsDataset + ' > ' + lfnFile
333     print ' Input: ' + cmd
334     os.system(cmd)
335    
336     # Create the corresponding crab task
337     crabTask = task.Task(crabId,cmsDataset,mitCfg,version)
338     crabTask.storagePath = findStoragePath(mitCfg,version,mitDataset)
339     crabTask.loadAllLfns(lfnFile)
340     crabTask.loadCompletedLfns()
341     crabTask.createMissingLfns(lfnFile,lfnFile + '_' + crabTask.tag)
342     crabTask.createSubTasks(lfnFile + '_' + crabTask.tag)
343     cmd = 'cp ' + lfnFile + '_' + crabTask.tag + '_*' + ' ./'
344     os.system(cmd)
345    
346     nevents = len(crabTask.lfns)
347    
348     # Say what we do now
349     print ' Preparing dataset: ' + cmsDataset + ' [MIT: ' + mitDataset + ' with ' + str(nevents) + \
350     ' total jobs(=lfns), nEvtsTotal: %d]' % crabTask.nTotalEvts
351    
352     # --------------------------------------------------------------------------------------------------
353     # Prepare the config files
354     # --------------------------------------------------------------------------------------------------
355     # Cleaning up
356     cmd = "rm -f crab.cfg crab.cfg-Template cmssw.cfg cmssw.py"
357     os.system(cmd)
358    
359     # Parse template input and write the crab configuration file
360     fileInput = open(crabFile,'r')
361     fileOutput = open("crab.cfg-Template",'w')
362     line = fileInput.readline()
363     while (line != ''):
364     if line[0] != '#':
365     line = searchReplace(line,mitCfg,version,mitDataset, \
366     cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
367     fileOutput.write(line)
368     line = fileInput.readline()
369     fileInput .close()
370     fileOutput.close()
371    
372     # Parse template input and write the crab configuration file
373     fileInput = open(cmsswFile,'r')
374     fileOutput = open(cmsswPy,'w')
375     line = fileInput.readline()
376     while (line != ''):
377     if line[0] != '#':
378     line = searchReplace(line,mitCfg,version,mitDataset, \
379     cmsDataset,cmsswPy,dbs,sched,blacklist,skpEvts)
380     fileOutput.write(line)
381     line = fileInput.readline()
382     fileInput .close()
383     fileOutput.close()
384    
385     # --------------------------------------------------------------------------------------------------
386     # Job creation and submission
387     # --------------------------------------------------------------------------------------------------
388     print '\n Using CMSSW version: ' + os.environ['CMSSW_VERSION']
389     print ' Using CRAB version: ' + os.environ['CRAB_VERS'] + '\n'
390    
391     pattern1 = 'working directory'
392     pattern2 = 'job\(s\) can run on'
393    
394     # Go through the crabTask and submit each subtask to the grid
395     for subTask in crabTask.subTasks:
396    
397     tag = crabTask.tag + '_' + subTask.tag()
398     print ' Working on subtask: ' + tag
399    
400     nJobsTotal = 0
401     crabIdCheck = ''
402    
403     # adjust crab config
404     fileInput = open("crab.cfg-Template",'r')
405     fileOutput = open("crab.cfg",'w')
406     line = fileInput.readline()
407     while (line != ''):
408     if line[0] != '#':
409     line = adjustCfg(line,subTask.nSubTaskLfn,tag)
410     fileOutput.write(line)
411     line = fileInput.readline()
412     fileInput .close()
413     fileOutput.close()
414    
415     # ----------------------------------------------------------------------------------------------
416     # Deal with storage element area
417     # ----------------------------------------------------------------------------------------------
418     # find the forseen storage place
419     cmd = 'grep ^storage_element crab.cfg-Template'
420     for file in os.popen(cmd).readlines(): # run command
421     line = file[:-1] # strip '\n'
422     # decode the storage element name
423     names = line.split("=") # splitting every '='
424     storageEle = names.pop()
425     storageEle = re.sub("\s", "",storageEle)
426     cmd = 'grep ^storage_path crab.cfg-Template'
427     for file in os.popen(cmd).readlines(): # run command
428     line = file[:-1] # strip '\n'
429     # decode the storage directory name
430     names = line.split("=") # splitting every '='
431     names = names[1:]
432     storagePath = "=".join(names)
433     storagePath = re.sub("\s", "",storagePath)
434     cmd = 'grep ^user_remote_dir crab.cfg-Template'
435     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     storagePath += names[-1]
440     storageUrl = 'srm://' + storageEle + ':8443' + storagePath
441    
442     storagePath = adjustCfg(storagePath,subTask.nSubTaskLfn,tag)
443     storageUrl = adjustCfg(storageUrl, subTask.nSubTaskLfn,tag)
444    
445     print ' StorageUrl: ' + storageUrl
446    
447     # Create storage area
448     if test == 0: # for testing we do not create the directories
449     if (storageEle == 'srm.cern.ch'):
450     createDirCern(storagePath)
451     else:
452     createDirGeneral(storageEle,storagePath)
453    
454     # cmd = "crab -create -debug 3 -USER.ui_working_dir=" + tag + " | tee forDaniele "
455     cmd = "crab -create -USER.ui_working_dir=" + tag
456     print ' -> ' + cmd
457     if test != 0:
458     cmd = 'echo ' + cmd
459     print ' ' + cmd
460     for line in os.popen(cmd).readlines(): # run command
461     line = line[:-1] # strip '\n'
462     print ' CRAB: ' + line
463     if re.search(pattern1,line):
464     f = line.split("/")
465     crabIdCheck = f[-2]
466     if re.search(pattern2,line):
467     f = line.split(" ")
468     nJobsTotal = f[1]
469     if nJobsTotal == '':
470     nJobsTotal = f[2]
471     # and report
472     print ' --> %s jobs created (%s).\n'%(nJobsTotal,tag)
473    
474     # adjust arguments
475     cmd = 'input.py --db=' + lfnFile + '_' + tag + ' --option=xml --dataset=' + cmsDataset + \
476     ' > ' + tag + '/share/arguments.xml'
477     print ' update arguments: ' + cmd
478     if test == 0:
479     os.system(cmd)
480    
481     # loop through the file and determine the submission parameters
482     block = ''
483     blocks = []
484     idx = 0
485     minIdxs = []
486     maxIdxs = []
487    
488     fileInput = open(lfnFile + '_' + tag,'r')
489     line = fileInput.readline()
490     while (line != ''):
491     line = line[:-1]
492     if line[0] != '#':
493     idx += 1
494     f = line.split()
495     block = f[0]
496     lfn = f[1]
497     nEvents = f[2]
498     if len(blocks) == 0 or block != blocks[-1]:
499     # new block found
500     blocks .append(block)
501     minIdxs.append(idx)
502     maxIdxs.append(idx)
503     else:
504     maxIdxs[-1] = idx
505     # keep the reading going
506     line = fileInput.readline()
507     fileInput .close()
508    
509     # merge blocks together if they use the same sites
510     lastSites = ''
511     idx = 0
512     mergedIdx = 0
513     mergedBlocks = []
514     mergedSites = []
515     mergedMinIdxs = []
516     mergedMaxIdxs = []
517     print '\n Show the unmerged blocks:'
518     for block in blocks:
519     cmd = "sites.py --block=" + block
520     for line in os.popen(cmd).readlines(): # run command
521     line = line[:-1]
522     sites = line
523     print ' Block ' + block + ' process: %d to %d'%(minIdxs[idx],maxIdxs[idx]) + \
524     ' at\n > ' + sites
525     # block with different sites found
526     if sites != lastSites:
527     lastSites = sites
528     mergedSites .append(sites)
529     mergedBlocks .append(blocks[idx])
530     mergedMinIdxs.append(minIdxs[idx])
531     mergedMaxIdxs.append(maxIdxs[idx])
532     mergedIdx =+ 1
533     else:
534     mergedMaxIdxs[mergedIdx-1] = maxIdxs[idx]
535     # last action in the loop: increment the unmerged blocks
536     idx += 1
537    
538     # Show already what we will do
539     idx = 0
540     print '\n Show the merged blocks:'
541     for block in mergedBlocks:
542     print ' Merged Block ' + block + ' process: %d to %d'\
543     %(mergedMinIdxs[idx],mergedMaxIdxs[idx]) + \
544     ' at\n > ' + mergedSites[idx]
545     # last action in the loop: increment the merged blocks
546     idx += 1
547    
548     # perfrom the submission block by block (using the merged blocks of course)
549     nSubmission = len(mergedBlocks)
550     idx = 0
551     print '\n Submit 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    
557     nSubmit = '%d-%d'%(mergedMinIdxs[idx],mergedMaxIdxs[idx])
558     if mergedMinIdxs[idx] == mergedMaxIdxs[idx]:
559     nSubmit = '%d,%d'%(mergedMinIdxs[idx],100000000)
560     cmd = 'crab -submit %s -continue %s -GRID.se_white_list=%s'%(nSubmit,tag,mergedSites[idx])
561     print ' ' + cmd
562     os.system(cmd)
563     # last action in the loop: increment the merged blocks
564     idx += 1
565    
566     print ' Number of blocks submitted: %d' % nSubmission
567    
568     print ' Done... keep watching it...'
569     sys.exit(0)
570    
571    
572     ## if nSubmit != -1:
573     ## cmd = 'crab -continue %s -submit %s'%(crabId,nSubmit)
574     ## print 'SUBMIT.PY: ' + cmd
575     ## status = os.system(cmd)
576     ## if status == 0:
577     ## print ' --> job submitted\n'
578     ## else:
579     ## leftOver = int(nJobsTotal)
580     ## nSubmitted = 0
581     ## nSubBatch = 80
582     ## while (nSubmitted < int(nJobsTotal)):
583     ## if leftOver < nSubBatch:
584     ## nSubBatch = leftOver
585     ##
586     ## cmd = 'crab -continue %s -submit %d'%(crabId,nSubBatch)
587     ## print 'SUBMIT.PY: ' + cmd
588     ## status = os.system(cmd)
589     ## if status == 0:
590     ## print ' --> %d job submitted\n'%(nSubBatch)
591     ## leftOver = leftOver - nSubBatch
592     ## nSubmitted = nSubmitted + nSubBatch
593     ##
594     ## #print 'SUBMIT.PY: ' + cmd
595     ## #status = os.system(cmd)
596     ## #if status == 0:
597     ## # print ' --> job submitted\n'
598     ##
599    
600     # --------------------------------------------------------------------------------------------------
601     # Run a test job to test the configuration and measure the expected output size
602     # --------------------------------------------------------------------------------------------------
603     if noTestJob == 0:
604     #-----------------------------------------------------------------------------------------------
605     # use a specific file for test
606     #-----------------------------------------------------------------------------------------------
607     # first check whether we already have a local file
608     print '\n Try to find an existing local file using "find"'
609     f = cmsDataset.split("/")
610     name = f[1]
611     vers = f[2]
612     tier = f[3]
613     file = ""
614     lfn = ""
615     cmd = 'find ./store/ -name ' + name
616     print 'Searching: ' + cmd
617     for line in os.popen(cmd).readlines(): # run command
618     file = line[:-1] # strip '\n'
619    
620     # looks like there could be a file we found a directory, confirm
621     if file != "":
622     print '\n We have a directory ' + file + ' .. confirming'
623     file = file + '/' + tier
624     cmd = 'find ' + file + ' -name \*.root'
625     for line in os.popen(cmd).readlines(): # run command
626     if line.find(vers):
627     file = line[:-1] # strip '\n'
628     lfn = file[1:]
629    
630     if os.path.exists(file):
631     print ' moving on with locally found file: \n' + ' ' + file
632     else:
633     print ' no local file found'
634     file = ""
635     lfn = ""
636    
637     # now try to see whether we can find a file to download
638     if file == "":
639     cmd = './bin/findLfn.py --input=' + cmsDataset + ' | grep /store/'
640     print '\n Find an LFN to download: ' + cmd
641     for line in os.popen(cmd).readlines(): # run command
642     if line.find("/store") != -1:
643     lfn = line[:-1] # strip '\n'
644     break
645     if lfn == "":
646     print "\n WARNING: No file found, continue assuming it is a simulation job.\n\n"
647     else:
648     print ' --> LFN: ' + lfn
649     file = '.' + lfn
650    
651     if os.path.exists(file):
652     print ' --> File already exists: ' + file
653     else:
654     cmd = './bin/downloadLfn.py ' + lfn
655     print ' --> downloading: ' + cmd
656     status = os.system(cmd)
657     if status != 0:
658     print ' ERROR - failed to copy LFN. EXIT now!'
659     sys.exit(1)
660    
661     # Parse template input and adjust the input file to the newly copied lfn
662     fileInput = open(cmsswPy,'r')
663     fileOutput = open("test-"+cmsswPy,'w')
664     line = fileInput.readline()
665     while (line != ''):
666     if line.find("file:") != -1:
667     line = '"file:' + lfn[1:] + '"\n'
668     fileOutput.write(line)
669     line = fileInput.readline()
670     fileInput .close()
671     fileOutput.close()
672    
673     # Setting the number of events (hard coded in the file so far)
674     nTryEvts = 1000.
675    
676     print '\n --> Please wait, running a test job now! Should be short (trying %.0f'%nTryEvts + \
677     ' evts). Check log: cmssw.log'
678     cmd = 'rm -f cmssw.log; /usr/bin/time --format "%e %U %S" cmsRun test-' + cmsswPy + \
679     ' >& cmssw.log'
680     print ' CMD: ' + cmd
681     status = os.system(cmd)
682    
683     cmd = 'tail -1 cmssw.log'
684     for file in os.popen(cmd).readlines(): # run command
685     line = file[:-1] # strip '\n'
686     f = line.split() # splitting every blank
687     rtime = float(f[0]) # wall clock time
688     utime = float(f[1]) # user time
689     stime = float(f[2]) # system time
690    
691     nEvtsTest = 1000
692     cmd = 'grep \'Begin processing\' cmssw.log | tail -1'
693     for file in os.popen(cmd).readlines(): # run command
694     line = file[:-1] # strip '\n'
695     # test whether there is a directory
696     f = line.split() # splitting every blank
697     nEvtsTest = f[3] # this is the number of records processed
698     nEvtsTest = int(nEvtsTest[:-2]) # strip 'th'
699     cmd = 'ls -s ' + mitDataset + '*.root'
700     size = 0
701     for file in os.popen(cmd).readlines(): # run command
702     line = file[:-1] # strip '\n'
703     f = line.split() # splitting every blank
704     size += int(f[0])/1000. # size in MB
705    
706     cmd = 'tail -1 cmssw.log'
707     for file in os.popen(cmd).readlines(): # run command
708     line = file[:-1] # strip '\n'
709     # get total, user and system times
710     names = line.split() # splitting every blank
711    
712     if nEvtsTest != nTryEvts:
713     print ' WARNING - Instead of %f did %d'%(nTryEvts,nEvtsTest)
714    
715     print ' '
716     print ' Number of test events produced: %d'%nEvtsTest
717     print ' File size for all events: %.2f MB'%size
718     print ' Processing time for all events: %.2f secs (u: %.2f s: %.2f)'%(rtime,utime,stime)
719     print ' '
720     print ' --> 1 event == %.2f secs'%(rtime/nEvtsTest)
721     print ' --> 1.00 GB == %d events'%(nEvtsTest/size*1024.)
722     print ' --> %.2f GB == %d events'%(nevents/(nEvtsTest/size*1024.),nevents)
723     print ' '
724    
725     if testJob == 1:
726     print '\n Test job finished, stopping now.\n'
727     sys.exit(0)
728    
729    
730     ## # are we just completing an existing production? and is there something to complete?
731     ## if complete == 1:
732     ## f = storagePath.split('=')
733     ## rfDir = f[-1]
734     ## #cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,rfDir)
735     ## cmd = 'castorInventory.py --nJobs=%s %s | grep Missing'%(nJobsTotal,storageUrl)
736     ## #print ' CMD: ' + cmd
737     ## for line in os.popen(cmd).readlines(): # run command
738     ## line = line[:-1] # strip '\n'
739     ## f = line.split(':')
740     ## nSubmit = f[1].strip()
741     ## f = nSubmit.split(',')
742     ## if len(f) == 0 or nSubmit == '':
743     ## print ' No more jobs left it seems, nSubmit=' + nSubmit
744     ## cmd = 'rm -rf ' + crabId
745     ## print ' Cleanup: ' + cmd + '\n\n'
746     ## status = os.system(cmd)
747     ## sys.exit(0)
748     ## elif len(f) == 1:
749     ## nInvalid = str(int(nJobsTotal) + 1000)
750     ## print ' One more jobs left, careful, adjusted, nSubmit=' + nSubmit
751     ## nSubmit = nSubmit + ',' + nInvalid
752     ##
753     ## ### nSubmit = ",".join(f[:-1])
754     ## print ' Missing jobs are: ' + nSubmit
755     ##
756     ## sys.exit(0)