CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Utilities/ReleaseScripts/scripts/addOnTests.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import os
00004 import time
00005 import sys
00006 import re
00007 import random
00008 from threading import Thread
00009 
00010 scriptPath = os.path.dirname( os.path.abspath(sys.argv[0]) )
00011 if scriptPath not in sys.path:
00012     sys.path.append(scriptPath)
00013 
00014         
00015 class testit(Thread):
00016     def __init__(self,dirName, commandList):
00017         Thread.__init__(self)
00018         self.dirName = dirName
00019         self.commandList = commandList
00020         self.status=-1
00021         self.report=''
00022         self.nfail=[]
00023         self.npass=[]
00024 
00025         return
00026     
00027     def run(self):
00028 
00029         startime='date %s' %time.asctime()
00030         exitCodes = []
00031 
00032         for command in self.commandList:
00033 
00034             if not os.path.exists(self.dirName):
00035                 os.makedirs(self.dirName)
00036 
00037             commandbase = command.replace(' ','_').replace('/','_')
00038             logfile='%s.log' % commandbase[:150].replace("'",'').replace('"','').replace('../','')
00039             
00040             executable = 'cd '+self.dirName+'; '+command+' > '+logfile+' 2>&1'
00041 
00042             ret = os.system(executable)
00043             exitCodes.append( ret )
00044 
00045         endtime='date %s' %time.asctime()
00046         tottime='%s-%s'%(endtime,startime)
00047     
00048         for i in range(len(self.commandList)):
00049             command = self.commandList[i]
00050             exitcode = exitCodes[i]
00051             if exitcode != 0:
00052                 log='%s : FAILED - time: %s s - exit: %s\n' %(command,tottime,exitcode)
00053                 self.report+='%s\n'%log
00054                 self.nfail.append(1)
00055                 self.npass.append(0)
00056             else:
00057                 log='%s : PASSED - time: %s s - exit: %s\n' %(command,tottime,exitcode)
00058                 self.report+='%s\n'%log
00059                 self.nfail.append(0)
00060                 self.npass.append(1)
00061 
00062         return
00063 
00064 class StandardTester(object):
00065 
00066     def __init__(self, nThrMax=4):
00067 
00068         self.threadList = []
00069         self.maxThreads = nThrMax
00070         self.prepare()
00071 
00072         return
00073 
00074     def activeThreads(self):
00075 
00076         nActive = 0
00077         for t in self.threadList:
00078             if t.isAlive() : nActive += 1
00079 
00080         return nActive
00081 
00082     def prepare(self):
00083     
00084         self.devPath = os.environ['LOCALRT'] + '/src/'
00085         self.relPath = self.devPath
00086         if os.environ.has_key('CMSSW_RELEASE_BASE') and (os.environ['CMSSW_RELEASE_BASE'] != ""): self.relPath = os.environ['CMSSW_RELEASE_BASE'] + '/src/'
00087 
00088         lines = { 'read312RV' : ['cmsRun '+self.file2Path('Utilities/ReleaseScripts/scripts/read312RV_cfg.py')], 
00089                   'fastsim1'  : ['cmsRun '+self.file2Path('FastSimulation/Configuration/test/IntegrationTestFake_cfg.py')],
00090                   'fastsim2'  : ['cmsRun '+self.file2Path('FastSimulation/Configuration/test/IntegrationTest_cfg.py')],
00091                   #'fastsim3'  : ['cmsRun '+self.file2Path('FastSimulation/Configuration/test/ExampleWithHLT_1E31_cfg.py')],
00092                   'fastsim4'  : ['cmsRun '+self.file2Path('FastSimulation/Configuration/test/IntegrationTestWithHLT_cfg.py')],
00093                   'pat1'      : ['cmsRun '+self.file2Path('PhysicsTools/PatAlgos/test/IntegrationTest_cfg.py')],
00094                 }
00095 
00096         hltTests = { 'hlt1' : ['cmsDriver.py TTbar_Tauola.cfi -s GEN,SIM,DIGI,L1,DIGI2RAW --mc --scenario=pp -n 10 --conditions auto:startup --relval 9000,50 --datatier "GEN-SIM-RAW" --eventcontent RAW --fileout file:RelVal_Raw_GRun_STARTUP.root',
00097                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnLine_HLT_GRun.py'), 
00098                                'cmsDriver.py RelVal -s HLT:GRun,RAW2DIGI,L1Reco,RECO --mc --scenario=pp -n 10 --conditions auto:startup_GRun --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_GRun_STARTUP.root --fileout file:RelVal_Raw_GRun_STARTUP_HLT_RECO.root'], 
00099                      'hlt2' : ['cmsDriver.py TTbar_Tauola.cfi -s GEN,SIM,DIGI,L1,DIGI2RAW --mc --scenario=HeavyIons -n 10 --conditions auto:starthi --relval 9000,50 --datatier "GEN-SIM-RAW" --eventcontent RAW --fileout file:RelVal_Raw_HIon_STARTUP.root',
00100                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnLine_HLT_HIon.py'),
00101                                'cmsDriver.py RelVal -s HLT:HIon,RAW2DIGI,L1Reco,RECO --mc --scenario=HeavyIons -n 10 --conditions auto:starthi_HIon --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_HIon_STARTUP.root --fileout file:RelVal_Raw_HIon_STARTUP_HLT_RECO.root'],
00102                      'hlt3' : ['cmsDriver.py RelVal -s L1REPACK --data --scenario=pp -n 10 --conditions auto:startup --relval 9000,50 --datatier "RAW" --eventcontent RAW --fileout file:RelVal_Raw_GRun_DATA.root --filein /store/data/Run2012A/MuEG/RAW/v1/000/191/718/14932935-E289-E111-830C-5404A6388697.root',
00103                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnData_HLT_GRun.py'),
00104                                'cmsDriver.py RelVal -s HLT:GRun,RAW2DIGI,L1Reco,RECO --data --scenario=pp -n 10 --conditions auto:hltonline_GRun --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_GRun_DATA.root --fileout file:RelVal_Raw_GRun_DATA_HLT_RECO.root'],
00105                      'hlt4' : ['cmsDriver.py RelVal -s L1REPACK --data --scenario=HeavyIons -n 10 --conditions auto:starthi --relval 9000,50 --datatier "RAW" --eventcontent RAW --fileout file:RelVal_Raw_HIon_DATA.root --filein /store/hidata/HIRun2011/HIHighPt/RAW/v1/000/182/838/F20AAF66-F71C-E111-9704-BCAEC532971D.root',
00106                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnData_HLT_HIon.py'),
00107                                'cmsDriver.py RelVal -s HLT:HIon,RAW2DIGI,L1Reco,RECO --data --scenario=HeavyIons -n 10 --conditions auto:hltonline_HIon --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_HIon_DATA.root --fileout file:RelVal_Raw_HIon_DATA_HLT_RECO.root'],
00108                      'hlt5' : ['cmsDriver.py TTbar_Tauola.cfi -s GEN,SIM,DIGI,L1,DIGI2RAW --mc --scenario=pp -n 10 --conditions auto:startup --relval 9000,50 --datatier "GEN-SIM-RAW" --eventcontent RAW --fileout file:RelVal_Raw_PIon_STARTUP.root', 
00109                                'cmsRun ' + self.file2Path('HLTrigger/Configuration/test/OnLine_HLT_PIon.py'),
00110                                'cmsDriver.py RelVal -s HLT:PIon,RAW2DIGI,L1Reco,RECO --mc --scenario=pp -n 10 --conditions auto:startup_PIon --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_PIon_STARTUP.root --fileout file:RelVal_Raw_PIon_STARTUP_HLT_RECO.root'],
00111                      'hlt6' : ['cmsDriver.py RelVal -s L1REPACK --data --scenario=pp -n 10 --conditions auto:startup --relval 9000,50 --datatier "RAW" --eventcontent RAW --fileout file:RelVal_Raw_PIon_DATA.root --filein /store/data/Run2012A/MuEG/RAW/v1/000/191/718/14932935-E289-E111-830C-5404A6388697.root',
00112                                'cmsRun ' + self.file2Path('HLTrigger/Configuration/test/OnData_HLT_PIon.py'),
00113                                'cmsDriver.py RelVal -s HLT:PIon,RAW2DIGI,L1Reco,RECO --data --scenario=pp -n 10 --conditions auto:hltonline_PIon --relval 9000,50 --datatier "RAW-HLT-RECO" --eventcontent FEVTDEBUGHLT --customise=HLTrigger/Configuration/CustomConfigs.L1THLT --processName=HLTRECO --filein file:RelVal_Raw_PIon_DATA.root --fileout file:RelVal_Raw_PIon_DATA_HLT_RECO.root'],
00114                      }
00115 
00116         self.commands={}
00117         for dirName, command in lines.items():
00118             self.commands[dirName] = command
00119 
00120         for dirName, commandList in hltTests.items():
00121             self.commands[dirName] = commandList
00122         return
00123         
00124     def dumpTest(self):
00125         print ",".join(self.commands.keys())
00126         return
00127 
00128     def file2Path(self,rFile):
00129 
00130         fullPath = self.relPath + rFile
00131         if os.path.exists(self.devPath + rFile): fullPath = self.devPath + rFile
00132         return fullPath
00133 
00134     def runTests(self, testList = None):
00135 
00136         actDir = os.getcwd()
00137 
00138         if not os.path.exists('addOnTests'):
00139             os.makedirs('addOnTests')
00140         os.chdir('addOnTests')
00141 
00142         nfail=0
00143         npass=0
00144         report=''
00145         
00146         print 'Running in %s thread(s)' % self.maxThreads
00147         
00148         for dirName, command in self.commands.items():
00149 
00150             if testList and not dirName in testList:
00151                 del self.commands[dirName]
00152                 continue
00153 
00154             # make sure we don't run more than the allowed number of threads:
00155             while self.activeThreads() >= self.maxThreads:
00156                 time.sleep(10)
00157                 continue
00158             
00159             print 'Preparing to run %s' % str(command)
00160             current = testit(dirName, command)
00161             self.threadList.append(current)
00162             current.start()
00163             time.sleep(random.randint(1,5)) # try to avoid race cond by sleeping random amount of time [1,5] sec 
00164             
00165         # wait until all threads are finished
00166         while self.activeThreads() > 0:
00167             time.sleep(5)
00168             
00169         # all threads are done now, check status ...
00170         for pingle in self.threadList:
00171             pingle.join()
00172             for f in pingle.nfail: nfail  += f
00173             for p in pingle.npass: npass  += p
00174             report += pingle.report
00175             print pingle.report
00176             sys.stdout.flush()
00177             
00178         reportSumm = '\n %s tests passed, %s failed \n' %(npass,nfail)
00179         print reportSumm
00180         
00181         runall_report_name='runall-report.log'
00182         runall_report=open(runall_report_name,'w')
00183         runall_report.write(report+reportSumm)
00184         runall_report.close()
00185 
00186         # get the logs to the logs dir:
00187         print '==> in :', os.getcwd()
00188         print '    going to copy log files to logs dir ...'
00189         if not os.path.exists('logs'):
00190             os.makedirs('logs')
00191         for dirName in self.commands:
00192             cmd = "for L in `ls "+dirName+"/*.log`; do cp $L logs/cmsDriver-`dirname $L`_`basename $L` ; done"
00193             print "going to ",cmd
00194             os.system(cmd)
00195 
00196         import pickle
00197         pickle.dump(self.commands, open('logs/addOnTests.pkl', 'w') )
00198 
00199         os.chdir(actDir)
00200         
00201         return
00202 
00203     def upload(self, tgtDir):
00204 
00205         print "in ", os.getcwd()
00206 
00207         if not os.path.exists(tgtDir):
00208             os.makedirs(tgtDir)
00209         
00210         cmd = 'tar cf - addOnTests.log addOnTests/logs | (cd '+tgtDir+' ; tar xf - ) '
00211         try:
00212             print 'executing: ',cmd
00213             ret = os.system(cmd)
00214             if ret != 0:
00215                 print "ERROR uploading logs:", ret, cmd
00216         except Exception, e:
00217             print "EXCEPTION while uploading addOnTest-logs : ", str(e)
00218             
00219         return
00220 
00221                 
00222 def main(argv) :
00223 
00224     import getopt
00225     
00226     try:
00227         opts, args = getopt.getopt(argv, "dj:t:", ["nproc=", 'uploadDir=', 'tests=','noRun','dump'])
00228     except getopt.GetoptError, e:
00229         print "unknown option", str(e)
00230         sys.exit(2)
00231         
00232     np        = 4
00233     uploadDir = None
00234     runTests  = True
00235     testList  = None
00236     dump      = False
00237     for opt, arg in opts :
00238         if opt in ('-j', "--nproc" ):
00239             np=int(arg)
00240         if opt in ("--uploadDir", ):
00241             uploadDir = arg
00242         if opt in ('--noRun', ):
00243             runTests = False
00244         if opt in ('-d','--dump', ):
00245             dump = True
00246         if opt in ('-t','--tests', ):
00247             testList = arg.split(",")
00248 
00249     tester = StandardTester(np)
00250     if dump:
00251         tester.dumpTest()
00252     else:
00253         if runTests:
00254             tester.runTests(testList)
00255         if uploadDir:
00256             tester.upload(uploadDir)
00257     return
00258     
00259 if __name__ == '__main__' :
00260     main(sys.argv[1:])