Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:32

0001 from __future__ import print_function
0002 from __future__ import absolute_import
0003 from optparse import OptionParser
0004 import sys,os, re, subprocess, datetime
0005 
0006 from . import eostools as castortools
0007 
0008 class logger:
0009     '''COLIN: do something cleaner with tagPackage'''
0010     def __init__(self, dirLocalOrTgzDirOnCastor):
0011         
0012         self.dirLocal = None
0013         self.tgzDirOnCastor = None
0014         dirLocalOrTgzDirOnCastor = dirLocalOrTgzDirOnCastor.rstrip('/')
0015 
0016         if self.isDirLocal( dirLocalOrTgzDirOnCastor ):
0017             self.dirLocal = dirLocalOrTgzDirOnCastor
0018         elif self.isTgzDirOnCastor( dirLocalOrTgzDirOnCastor ):
0019             self.tgzDirOnCastor = dirLocalOrTgzDirOnCastor
0020         else:
0021             raise ValueError( dirLocalOrTgzDirOnCastor + ' is neither a tgz directory on castor (provide a LFN!) nor a local directory')
0022             
0023         
0024     def isDirLocal(self, file ):
0025         if os.path.isdir( file ):
0026             return True
0027         else:
0028             return False
0029 
0030     def isTgzDirOnEOS(self, file ):
0031         '''Checks if file is a .tgz file in an eos dir'''
0032         if not castortools.isCastorDir( file ):
0033             file = castortools.castorToLFN(file)
0034             
0035         if castortools.isLFN( file ):
0036             tgzPattern = re.compile('.*\.tgz$')
0037             m = tgzPattern.match( file )
0038             if m:
0039                 return True
0040             else:
0041                 return False
0042         else:
0043             return False
0044 
0045     isTgzDirOnCastor = isTgzDirOnEOS
0046 
0047     def dump(self):
0048         print('local dir      :', self.dirLocal)
0049         print('castor archive :',self.tgzDirOnCastor)
0050 
0051     def addFile(self, file):
0052         #        if self.dirLocal == None:
0053         #            self.stageIn()
0054         #            os.system( 'cp %s %s' % (file, self.dirLocal) )
0055         #            self.stageOut( self.tgzDirOnCastor )
0056         if self.dirLocal != None:
0057             os.system( 'cp %s %s' % (file, self.dirLocal) )
0058 
0059     def logCMSSW(self): 
0060         showtagsLog = 'logger_showtags.txt'
0061         diffLog = 'logger_diff.txt'
0062         # os.system('showtags > ' + showtagsLog)
0063         self.showtags(showtagsLog)
0064         self.gitdiff(diffLog)
0065         self.addFile(showtagsLog)
0066         self.addFile(diffLog) 
0067 
0068     def logJobs(self, n):
0069         nJobs = 'logger_jobs.txt'
0070         out = file(nJobs,'w')
0071         out.write('NJobs: %i\n' % n)
0072         out.close()
0073         self.addFile(nJobs)
0074 
0075     def gitdiff(self, log):
0076         oldPwd = os.getcwd()
0077         os.chdir( os.getenv('CMSSW_BASE') + '/src/' )
0078         diffCmd = 'git diff -p --stat --color=never > %s/%s 2> /dev/null' % (oldPwd, log)
0079         print(diffCmd)
0080         os.system( diffCmd )
0081         os.chdir( oldPwd )
0082 
0083     def showtags(self, log):
0084         oldPwd = os.getcwd()
0085         os.chdir( os.getenv('CMSSW_BASE') + '/src/' )
0086         cmd = 'echo "Test Release based on: $CMSSW_VERSION" >  %s/%s 2> /dev/null' % (oldPwd, log)
0087         os.system( cmd )
0088         cmd = 'echo "Base Release in: $CMSSW_RELEASE_BASE"  >> %s/%s 2> /dev/null' % (oldPwd, log)
0089         os.system( cmd )
0090         cmd = 'echo "Your Test release in: $CMSSW_BASE"     >> %s/%s 2> /dev/null' % (oldPwd, log)
0091         os.system( cmd )
0092         cmd = 'git status --porcelain -b | head -n 1 >> %s/%s 2> /dev/null' % (oldPwd, log)
0093         os.system( cmd )
0094         cmd = 'git log -n 100 --format="%%T %%ai %%s %%d" >> %s/%s 2> /dev/null' % (oldPwd, log)
0095         os.system( cmd )
0096         os.chdir( oldPwd )        
0097 
0098     def stageIn(self):
0099         if self.tgzDirOnCastor != None:
0100             # castortools.xrdcp( '.', [self.tgzDirOnCastor] )
0101             cmsStage = 'cmsStage -f ' + self.tgzDirOnCastor + ' .'
0102             print(cmsStage)
0103             os.system( cmsStage ) 
0104             tgzDir = os.path.basename( self.tgzDirOnCastor )
0105             print(tgzDir) 
0106             os.system('tar -zxvf ' + tgzDir)
0107             os.system('rm ' + tgzDir )
0108             (root, ext) = os.path.splitext(tgzDir)
0109             self.dirLocal = root
0110         else:
0111             print('cannot stage in, the log had not been staged out')
0112 
0113     def stageOut(self, castorDir):
0114 
0115         castorDir = castortools.eosToLFN( castorDir )
0116         if not castortools.isLFN( castorDir ):
0117             print('cannot stage out, you need to provide an LFN as a destination directory, beginning with /store .')
0118             return False
0119         
0120         if self.dirLocal != None:
0121             tgzDir = self.dirLocal + '.tgz'
0122             tgzCmd = 'tar -zcvf ' + tgzDir + ' ' + self.dirLocal
0123             print(tgzCmd)
0124             os.system( tgzCmd)
0125             cmsStage = 'cmsStage -f %s %s' % (tgzDir, castorDir )
0126             print(cmsStage)
0127             os.system( cmsStage )
0128             os.system('rm ' + tgzDir )
0129             self.tgzDirOnCastor =  castorDir + '/' + tgzDir
0130         else:
0131             print('cannot stage out, the log is not staged in')
0132