CMS 3D CMS Logo

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