CMS 3D CMS Logo

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