CMS 3D CMS Logo

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