Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-28 23:48:24

0001 import os, datetime, time,  sys, shutil, glob, re, subprocess as sp, tempfile, socket
0002 TIME_OUT=700
0003 DEBUG=False
0004 def getDirSize(path): 
0005   import stat
0006   size=os.stat(path).st_blksize
0007   for directory,subdirs,files in os.walk(path):
0008     dStats=os.lstat(directory)
0009     size+=(dStats[stat.ST_NLINK]-1)*dStats[stat.ST_SIZE]
0010     for f in files:  
0011       fStats=os.lstat("%s/%s" % (directory,f))
0012       fSize=fStats[stat.ST_SIZE]
0013       size+=fSize
0014   return size
0015 
0016 def getDiskUsage(path):
0017   fsStats=os.statvfs(path)
0018   size=fsStats.f_bsize*fsStats.f_blocks
0019   available=fsStats.f_bavail*fsStats.f_bsize
0020   used=size-available
0021   usedPer=float(used)/size
0022   return (size,available,used,usedPer)
0023 
0024 def getNumRunsWithinTime(path,timeDelta):
0025   numRuns=0
0026   currTime=time.time()
0027   STAY_DIR={}
0028   for directory,subdirs,files in os.walk(path):
0029     for f in sorted(files, key=lambda x: os.stat("%s/%s" % (directory,x)).st_mtime,reverse=True):
0030       fullFName="%s/%s" % (directory,f)
0031       fMatch=re.match(r".*_R([0-9]{9}).*",f)
0032       if fMatch:
0033         run=fMatch.group(1)
0034         fMtime=os.stat(fullFName).st_mtime
0035         if currTime - timeDelta*3600 <= fMtime:
0036           STAY_DIR.setdefault(run,fMtime)
0037         else:
0038           break
0039   numRuns=len(STAY_DIR)
0040   return numRuns
0041   
0042 def debugMsg(level,message):
0043   LEVELS=["INFO","WARNING","ERROR"]
0044   d=datetime.datetime.today()
0045   timeStamp=d.strftime("%Y/%m/%d\t%H:%M:%S")
0046   msg="%s\t%s:\t%s\n" % (timeStamp,LEVELS[level],message)
0047   sys.stdout.write(msg)
0048   return True
0049   
0050 def prettyPrintUnits(value,unit,decimals=0):
0051   """
0052 Provide human readable units
0053   """
0054   runit=""
0055   if unit is "b":
0056     units=["B","KB","MB","GB","TB"]
0057     it=iter(units)
0058     v=int(value/1024)
0059     p=0
0060     runit=next(it)
0061     while v > 0:
0062       v=int(v/1024)
0063       try:
0064         runit=next(it)
0065         p+=1
0066       except:
0067         break
0068     return "%%.%df %%s " % decimals % (float(value)/pow(1024,p),runit)
0069   else:
0070     return "%%.%df %%s " % decimals % (value,"%") 
0071   
0072 def executeCmd(cmd):
0073   stdOutFile=tempfile.TemporaryFile(bufsize=0)
0074   stdErrFile=tempfile.TemporaryFile(bufsize=0)
0075   cmdHdl=sp.Popen(cmd,shell=True,stdout=stdOutFile,stderr=stdErrFile)
0076   t=0
0077   cmdHdl.poll()
0078   while cmdHdl.returncode == None and t<TIME_OUT:
0079     t=t+1
0080     cmdHdl.poll()
0081     time.sleep(1)
0082   if t >= TIME_OUT and not cmdHdl.returncode:
0083     try:
0084       os.kill(cmdHdl.pid,9)
0085       debugMsg(2,"Execution timed out on Command: '%s'" % cmd)
0086     except:
0087       DEBUG and debugMsg(1,"Execution timed out on Command: '%s' but it ended while trying to kill it, adjust timer" % cmd)
0088   cmdHdl.poll()
0089   DEBUG and debugMsg(0,"End of Execution cicle of Command: '%s' " % cmd)
0090   stdOutFile.seek(0)
0091   stdErrFile.seek(0)
0092   return (stdOutFile,stdErrFile,cmdHdl.returncode)
0093   
0094   
0095 def sendmail(EmailAddress,run=123456789,body="",subject="File merge failed."):
0096   import os, smtplib
0097   from email.MIMEText import MIMEText
0098   server=socket.gethostname() #os.getenv("HOSTNAME")
0099   user=os.getenv("USER")
0100   ServerMail="%s@%s" % (user,server)
0101   s=smtplib.SMTP("localhost")
0102   tolist=[EmailAddress] #[EmailAddress, "lat@cern.ch"]
0103   if not body: body="File copy to dropbox failed by unknown reason for run:%09d on server: %s" % (run,server)
0104   msg = MIMEText(body)
0105   msg['Subject'] = subject
0106   msg['From'] = ServerMail
0107   msg['To'] = EmailAddress
0108   s.sendmail(ServerMail,tolist,msg.as_string())
0109   s.quit()