CMS 3D CMS Logo

Functions
commonAnTS Namespace Reference

Functions

def debugMsg (level, message)
 
def executeCmd (cmd)
 
def getDirSize (path)
 
def getDiskUsage (path)
 
def getNumRunsWithinTime (path, timeDelta)
 
def prettyPrintUnits (value, unit, decimals=0)
 
def sendmail (EmailAddress, run=123456789, body="", subject="File merge failed.")
 

Function Documentation

def commonAnTS.debugMsg (   level,
  message 
)

Definition at line 42 of file commonAnTS.py.

Referenced by executeCmd().

42 def debugMsg(level,message):
43  LEVELS=["INFO","WARNING","ERROR"]
44  d=datetime.datetime.today()
45  timeStamp=d.strftime("%Y/%m/%d\t%H:%M:%S")
46  msg="%s\t%s:\t%s\n" % (timeStamp,LEVELS[level],message)
47  sys.stdout.write(msg)
48  return True
49 
def debugMsg(level, message)
Definition: commonAnTS.py:42
def commonAnTS.executeCmd (   cmd)

Definition at line 72 of file commonAnTS.py.

References debugMsg().

72 def executeCmd(cmd):
73  stdOutFile=tempfile.TemporaryFile(bufsize=0)
74  stdErrFile=tempfile.TemporaryFile(bufsize=0)
75  cmdHdl=sp.Popen(cmd,shell=True,stdout=stdOutFile,stderr=stdErrFile)
76  t=0
77  cmdHdl.poll()
78  while cmdHdl.returncode == None and t<TIME_OUT:
79  t=t+1
80  cmdHdl.poll()
81  time.sleep(1)
82  if t >= TIME_OUT and not cmdHdl.returncode:
83  try:
84  os.kill(cmdHdl.pid,9)
85  debugMsg(2,"Execution timed out on Command: '%s'" % cmd)
86  except:
87  DEBUG and debugMsg(1,"Execution timed out on Command: '%s' but it ended while trying to kill it, adjust timer" % cmd)
88  cmdHdl.poll()
89  DEBUG and debugMsg(0,"End of Execution cicle of Command: '%s' " % cmd)
90  stdOutFile.seek(0)
91  stdErrFile.seek(0)
92  return (stdOutFile,stdErrFile,cmdHdl.returncode)
93 
94 
def executeCmd(cmd)
Definition: commonAnTS.py:72
def debugMsg(level, message)
Definition: commonAnTS.py:42
def commonAnTS.getDirSize (   path)

Definition at line 4 of file commonAnTS.py.

4 def getDirSize(path):
5  import stat
6  size=os.stat(path).st_blksize
7  for directory,subdirs,files in os.walk(path):
8  dStats=os.lstat(directory)
9  size+=(dStats[stat.ST_NLINK]-1)*dStats[stat.ST_SIZE]
10  for f in files:
11  fStats=os.lstat("%s/%s" % (directory,f))
12  fSize=fStats[stat.ST_SIZE]
13  size+=fSize
14  return size
15 
def getDirSize(path)
Definition: commonAnTS.py:4
def commonAnTS.getDiskUsage (   path)

Definition at line 16 of file commonAnTS.py.

References objects.autophobj.float.

16 def getDiskUsage(path):
17  fsStats=os.statvfs(path)
18  size=fsStats.f_bsize*fsStats.f_blocks
19  available=fsStats.f_bavail*fsStats.f_bsize
20  used=size-available
21  usedPer=float(used)/size
22  return (size,available,used,usedPer)
23 
def getDiskUsage(path)
Definition: commonAnTS.py:16
def commonAnTS.getNumRunsWithinTime (   path,
  timeDelta 
)

Definition at line 24 of file commonAnTS.py.

24 def getNumRunsWithinTime(path,timeDelta):
25  numRuns=0
26  currTime=time.time()
27  STAY_DIR={}
28  for directory,subdirs,files in os.walk(path):
29  for f in sorted(files, key=lambda x: os.stat("%s/%s" % (directory,x)).st_mtime,reverse=True):
30  fullFName="%s/%s" % (directory,f)
31  fMatch=re.match(r".*_R([0-9]{9}).*",f)
32  if fMatch:
33  run=fMatch.group(1)
34  fMtime=os.stat(fullFName).st_mtime
35  if currTime - timeDelta*3600 <= fMtime:
36  STAY_DIR.setdefault(run,fMtime)
37  else:
38  break
39  numRuns=len(STAY_DIR)
40  return numRuns
41 
def getNumRunsWithinTime(path, timeDelta)
Definition: commonAnTS.py:24
def commonAnTS.prettyPrintUnits (   value,
  unit,
  decimals = 0 
)
Provide human readable units

Definition at line 50 of file commonAnTS.py.

References objects.autophobj.float, GetRecoTauVFromDQM_MC_cff.next, and funct.pow().

50 def prettyPrintUnits(value,unit,decimals=0):
51  """
52 Provide human readable units
53  """
54  runit=""
55  if unit is "b":
56  units=["B","KB","MB","GB","TB"]
57  it=iter(units)
58  v=long(value/1024)
59  p=0
60  runit=next(it)
61  while v > 0:
62  v=long(v/1024)
63  try:
64  runit=next(it)
65  p+=1
66  except:
67  break
68  return "%%.%df %%s " % decimals % (float(value)/pow(1024,p),runit)
69  else:
70  return "%%.%df %%s " % decimals % (value,"%")
71 
def prettyPrintUnits(value, unit, decimals=0)
Definition: commonAnTS.py:50
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def commonAnTS.sendmail (   EmailAddress,
  run = 123456789,
  body = "",
  subject = "File merge failed." 
)

Definition at line 95 of file commonAnTS.py.

95 def sendmail(EmailAddress,run=123456789,body="",subject="File merge failed."):
96  import os, smtplib
97  from email.MIMEText import MIMEText
98  server=socket.gethostname() #os.getenv("HOSTNAME")
99  user=os.getenv("USER")
100  ServerMail="%s@%s" % (user,server)
101  s=smtplib.SMTP("localhost")
102  tolist=[EmailAddress] #[EmailAddress, "lat@cern.ch"]
103  if not body: body="File copy to dropbox failed by unknown reason for run:%09d on server: %s" % (run,server)
104  msg = MIMEText(body)
105  msg['Subject'] = subject
106  msg['From'] = ServerMail
107  msg['To'] = EmailAddress
108  s.sendmail(ServerMail,tolist,msg.as_string())
109  s.quit()
110 
def sendmail(EmailAddress, run=123456789, body="", subject="File merge failed.")
Definition: commonAnTS.py:95