CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Functions | Variables
commonAnTS Namespace Reference

Functions

def debugMsg
 
def executeCmd
 
def getDirSize
 
def getDiskUsage
 
def getNumRunsWithinTime
 
def prettyPrintUnits
 
def sendmail
 

Variables

 DEBUG = False
 
int TIME_OUT = 700
 

Function Documentation

def commonAnTS.debugMsg (   level,
  message 
)

Definition at line 42 of file commonAnTS.py.

Referenced by executeCmd().

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

Definition at line 72 of file commonAnTS.py.

References debugMsg().

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

Definition at line 4 of file commonAnTS.py.

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

Definition at line 16 of file commonAnTS.py.

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

Definition at line 24 of file commonAnTS.py.

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

Definition at line 50 of file commonAnTS.py.

References funct.pow().

50 
51 def prettyPrintUnits(value,unit,decimals=0):
52  """
53 Provide human readable units
54  """
55  runit=""
56  if unit is "b":
57  units=["B","KB","MB","GB","TB"]
58  it=iter(units)
59  v=long(value/1024)
60  p=0
61  runit=it.next()
62  while v > 0:
63  v=long(v/1024)
64  try:
65  runit=it.next()
66  p+=1
67  except:
68  break
69  return "%%.%df %%s " % decimals % (float(value)/pow(1024,p),runit)
70  else:
71  return "%%.%df %%s " % decimals % (value,"%")
def prettyPrintUnits
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 
96 def sendmail(EmailAddress,run=123456789,body="",subject="File merge failed."):
97  import os, smtplib
98  from email.MIMEText import MIMEText
99  server=socket.gethostname() #os.getenv("HOSTNAME")
100  user=os.getenv("USER")
101  ServerMail="%s@%s" % (user,server)
102  s=smtplib.SMTP("localhost")
103  tolist=[EmailAddress] #[EmailAddress, "lat@cern.ch"]
104  if not body: body="File copy to dropbox failed by unknown reason for run:%09d on server: %s" % (run,server)
105  msg = MIMEText(body)
106  msg['Subject'] = subject
107  msg['From'] = ServerMail
108  msg['To'] = EmailAddress
109  s.sendmail(ServerMail,tolist,msg.as_string())
110  s.quit()
def sendmail
Definition: commonAnTS.py:95

Variable Documentation

commonAnTS.DEBUG = False

Definition at line 3 of file commonAnTS.py.

int commonAnTS.TIME_OUT = 700

Definition at line 2 of file commonAnTS.py.