CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
commonAnTS.py
Go to the documentation of this file.
1 import os, datetime, time, sys, shutil, glob, re, subprocess as sp, tempfile, socket
2 TIME_OUT=700
3 DEBUG=False
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 
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 
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 
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 
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=int(value/1024)
59  p=0
60  runit=next(it)
61  while v > 0:
62  v=int(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 
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 
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()
def sendmail
Definition: commonAnTS.py:95
def getNumRunsWithinTime
Definition: commonAnTS.py:24
def executeCmd
Definition: commonAnTS.py:72
def getDiskUsage
Definition: commonAnTS.py:16
def prettyPrintUnits
Definition: commonAnTS.py:50
def getDirSize
Definition: commonAnTS.py:4
def debugMsg
Definition: commonAnTS.py:42
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29