CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/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                      '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',
00099                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnLine_HLT_HIon.py')],
00100                      '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',
00101                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnData_HLT_GRun.py')],
00102                      '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',
00103                                'cmsRun '+self.file2Path('HLTrigger/Configuration/test/OnData_HLT_HIon.py')],
00104                      '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', 
00105                                'cmsRun ' + self.file2Path('HLTrigger/Configuration/test/OnLine_HLT_PIon.py')],
00106                      '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',
00107                                'cmsRun ' + self.file2Path('HLTrigger/Configuration/test/OnData_HLT_PIon.py')],
00108                      }
00109 
00110         self.commands={}
00111         for dirName, command in lines.items():
00112             self.commands[dirName] = command
00113 
00114         for dirName, commandList in hltTests.items():
00115             self.commands[dirName] = commandList
00116         return
00117         
00118     def dumpTest(self):
00119         print ",".join(self.commands.keys())
00120         return
00121 
00122     def file2Path(self,rFile):
00123 
00124         fullPath = self.relPath + rFile
00125         if os.path.exists(self.devPath + rFile): fullPath = self.devPath + rFile
00126         return fullPath
00127 
00128     def runTests(self, testList = None):
00129 
00130         actDir = os.getcwd()
00131 
00132         if not os.path.exists('addOnTests'):
00133             os.makedirs('addOnTests')
00134         os.chdir('addOnTests')
00135 
00136         nfail=0
00137         npass=0
00138         report=''
00139         
00140         print 'Running in %s thread(s)' % self.maxThreads
00141         
00142         for dirName, command in self.commands.items():
00143 
00144             if testList and not dirName in testList:
00145                 del self.commands[dirName]
00146                 continue
00147 
00148             # make sure we don't run more than the allowed number of threads:
00149             while self.activeThreads() >= self.maxThreads:
00150                 time.sleep(10)
00151                 continue
00152             
00153             print 'Preparing to run %s' % str(command)
00154             current = testit(dirName, command)
00155             self.threadList.append(current)
00156             current.start()
00157             time.sleep(random.randint(1,5)) # try to avoid race cond by sleeping random amount of time [1,5] sec 
00158             
00159         # wait until all threads are finished
00160         while self.activeThreads() > 0:
00161             time.sleep(5)
00162             
00163         # all threads are done now, check status ...
00164         for pingle in self.threadList:
00165             pingle.join()
00166             for f in pingle.nfail: nfail  += f
00167             for p in pingle.npass: npass  += p
00168             report += pingle.report
00169             print pingle.report
00170             sys.stdout.flush()
00171             
00172         reportSumm = '\n %s tests passed, %s failed \n' %(npass,nfail)
00173         print reportSumm
00174         
00175         runall_report_name='runall-report.log'
00176         runall_report=open(runall_report_name,'w')
00177         runall_report.write(report+reportSumm)
00178         runall_report.close()
00179 
00180         # get the logs to the logs dir:
00181         print '==> in :', os.getcwd()
00182         print '    going to copy log files to logs dir ...'
00183         if not os.path.exists('logs'):
00184             os.makedirs('logs')
00185         for dirName in self.commands:
00186             cmd = "for L in `ls "+dirName+"/*.log`; do cp $L logs/cmsDriver-`dirname $L`_`basename $L` ; done"
00187             print "going to ",cmd
00188             os.system(cmd)
00189 
00190         import pickle
00191         pickle.dump(self.commands, open('logs/addOnTests.pkl', 'w') )
00192 
00193         os.chdir(actDir)
00194         
00195         return
00196 
00197     def upload(self, tgtDir):
00198 
00199         print "in ", os.getcwd()
00200 
00201         if not os.path.exists(tgtDir):
00202             os.makedirs(tgtDir)
00203         
00204         cmd = 'tar cf - addOnTests.log addOnTests/logs | (cd '+tgtDir+' ; tar xf - ) '
00205         try:
00206             print 'executing: ',cmd
00207             ret = os.system(cmd)
00208             if ret != 0:
00209                 print "ERROR uploading logs:", ret, cmd
00210         except Exception, e:
00211             print "EXCEPTION while uploading addOnTest-logs : ", str(e)
00212             
00213         return
00214 
00215                 
00216 def main(argv) :
00217 
00218     import getopt
00219     
00220     try:
00221         opts, args = getopt.getopt(argv, "dj:t:", ["nproc=", 'uploadDir=', 'tests=','noRun','dump'])
00222     except getopt.GetoptError, e:
00223         print "unknown option", str(e)
00224         sys.exit(2)
00225         
00226     np        = 4
00227     uploadDir = None
00228     runTests  = True
00229     testList  = None
00230     dump      = False
00231     for opt, arg in opts :
00232         if opt in ('-j', "--nproc" ):
00233             np=int(arg)
00234         if opt in ("--uploadDir", ):
00235             uploadDir = arg
00236         if opt in ('--noRun', ):
00237             runTests = False
00238         if opt in ('-d','--dump', ):
00239             dump = True
00240         if opt in ('-t','--tests', ):
00241             testList = arg.split(",")
00242 
00243     tester = StandardTester(np)
00244     if dump:
00245         tester.dumpTest()
00246     else:
00247         if runTests:
00248             tester.runTests(testList)
00249         if uploadDir:
00250             tester.upload(uploadDir)
00251     return
00252     
00253 if __name__ == '__main__' :
00254     main(sys.argv[1:])