CMS 3D CMS Logo

logger.py
Go to the documentation of this file.
1 from optparse import OptionParser
2 import sys,os, re, subprocess, datetime
3 
4 import eostools as castortools
5 
6 class logger:
7  '''COLIN: do something cleaner with tagPackage'''
8  def __init__(self, dirLocalOrTgzDirOnCastor):
9 
10  self.dirLocal = None
11  self.tgzDirOnCastor = None
12  dirLocalOrTgzDirOnCastor = dirLocalOrTgzDirOnCastor.rstrip('/')
13 
14  if self.isDirLocal( dirLocalOrTgzDirOnCastor ):
15  self.dirLocal = dirLocalOrTgzDirOnCastor
16  elif self.isTgzDirOnCastor( dirLocalOrTgzDirOnCastor ):
17  self.tgzDirOnCastor = dirLocalOrTgzDirOnCastor
18  else:
19  raise ValueError( dirLocalOrTgzDirOnCastor + ' is neither a tgz directory on castor (provide a LFN!) nor a local directory')
20 
21 
22  def isDirLocal(self, file ):
23  if os.path.isdir( file ):
24  return True
25  else:
26  return False
27 
28  def isTgzDirOnEOS(self, file ):
29  '''Checks if file is a .tgz file in an eos dir'''
30  if not castortools.isCastorDir( file ):
31  file = castortools.castorToLFN(file)
32 
33  if castortools.isLFN( file ):
34  tgzPattern = re.compile('.*\.tgz$')
35  m = tgzPattern.match( file )
36  if m:
37  return True
38  else:
39  return False
40  else:
41  return False
42 
43  isTgzDirOnCastor = isTgzDirOnEOS
44 
45  def dump(self):
46  print 'local dir :', self.dirLocal
47  print 'castor archive :',self.tgzDirOnCastor
48 
49  def addFile(self, file):
50  # if self.dirLocal == None:
51  # self.stageIn()
52  # os.system( 'cp %s %s' % (file, self.dirLocal) )
53  # self.stageOut( self.tgzDirOnCastor )
54  if self.dirLocal != None:
55  os.system( 'cp %s %s' % (file, self.dirLocal) )
56 
57  def logCMSSW(self):
58  showtagsLog = 'logger_showtags.txt'
59  diffLog = 'logger_diff.txt'
60  # os.system('showtags > ' + showtagsLog)
61  self.showtags(showtagsLog)
62  self.gitdiff(diffLog)
63  self.addFile(showtagsLog)
64  self.addFile(diffLog)
65 
66  def logJobs(self, n):
67  nJobs = 'logger_jobs.txt'
68  out = file(nJobs,'w')
69  out.write('NJobs: %i\n' % n)
70  out.close()
71  self.addFile(nJobs)
72 
73  def gitdiff(self, log):
74  oldPwd = os.getcwd()
75  os.chdir( os.getenv('CMSSW_BASE') + '/src/' )
76  diffCmd = 'git diff -p --stat --color=never > %s/%s 2> /dev/null' % (oldPwd, log)
77  print diffCmd
78  os.system( diffCmd )
79  os.chdir( oldPwd )
80 
81  def showtags(self, log):
82  oldPwd = os.getcwd()
83  os.chdir( os.getenv('CMSSW_BASE') + '/src/' )
84  cmd = 'echo "Test Release based on: $CMSSW_VERSION" > %s/%s 2> /dev/null' % (oldPwd, log)
85  os.system( cmd )
86  cmd = 'echo "Base Release in: $CMSSW_RELEASE_BASE" >> %s/%s 2> /dev/null' % (oldPwd, log)
87  os.system( cmd )
88  cmd = 'echo "Your Test release in: $CMSSW_BASE" >> %s/%s 2> /dev/null' % (oldPwd, log)
89  os.system( cmd )
90  cmd = 'git status --porcelain -b | head -n 1 >> %s/%s 2> /dev/null' % (oldPwd, log)
91  os.system( cmd )
92  cmd = 'git log -n 100 --format="%%T %%ai %%s %%d" >> %s/%s 2> /dev/null' % (oldPwd, log)
93  os.system( cmd )
94  os.chdir( oldPwd )
95 
96  def stageIn(self):
97  if self.tgzDirOnCastor != None:
98  # castortools.xrdcp( '.', [self.tgzDirOnCastor] )
99  cmsStage = 'cmsStage -f ' + self.tgzDirOnCastor + ' .'
100  print cmsStage
101  os.system( cmsStage )
102  tgzDir = os.path.basename( self.tgzDirOnCastor )
103  print tgzDir
104  os.system('tar -zxvf ' + tgzDir)
105  os.system('rm ' + tgzDir )
106  (root, ext) = os.path.splitext(tgzDir)
107  self.dirLocal = root
108  else:
109  print 'cannot stage in, the log had not been staged out'
110 
111  def stageOut(self, castorDir):
112 
113  castorDir = castortools.eosToLFN( castorDir )
114  if not castortools.isLFN( castorDir ):
115  print 'cannot stage out, you need to provide an LFN as a destination directory, beginning with /store .'
116  return False
117 
118  if self.dirLocal != None:
119  tgzDir = self.dirLocal + '.tgz'
120  tgzCmd = 'tar -zcvf ' + tgzDir + ' ' + self.dirLocal
121  print tgzCmd
122  os.system( tgzCmd)
123  cmsStage = 'cmsStage -f %s %s' % (tgzDir, castorDir )
124  print cmsStage
125  os.system( cmsStage )
126  os.system('rm ' + tgzDir )
127  self.tgzDirOnCastor = castorDir + '/' + tgzDir
128  else:
129  print 'cannot stage out, the log is not staged in'
130 
def gitdiff(self, log)
Definition: logger.py:73
def showtags(self, log)
Definition: logger.py:81
isTgzDirOnCastor
Definition: logger.py:43
def isDirLocal(self, file)
Definition: logger.py:22
def logJobs(self, n)
Definition: logger.py:66
def stageOut(self, castorDir)
Definition: logger.py:111
def addFile(self, file)
Definition: logger.py:49
def logCMSSW(self)
Definition: logger.py:57
def stageIn(self)
Definition: logger.py:96
def dump(self)
Definition: logger.py:45
def isTgzDirOnEOS(self, file)
Definition: logger.py:28
def __init__(self, dirLocalOrTgzDirOnCastor)
Definition: logger.py:8