1 import os, datetime, time, sys, shutil, glob, re, subprocess
as sp, tempfile, socket
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]
11 fStats=os.lstat(
"%s/%s" % (directory,f))
12 fSize=fStats[stat.ST_SIZE]
17 fsStats=os.statvfs(path)
18 size=fsStats.f_bsize*fsStats.f_blocks
19 available=fsStats.f_bavail*fsStats.f_bsize
21 usedPer=
float(used)/size
22 return (size,available,used,usedPer)
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)
34 fMtime=os.stat(fullFName).st_mtime
35 if currTime - timeDelta*3600 <= fMtime:
36 STAY_DIR.setdefault(run,fMtime)
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)
52 Provide human readable units 56 units=[
"B",
"KB",
"MB",
"GB",
"TB"]
68 return "%%.%df %%s " % decimals % (
float(value)/
pow(1024,p),runit)
70 return "%%.%df %%s " % decimals % (value,
"%")
73 stdOutFile=tempfile.TemporaryFile(bufsize=0)
74 stdErrFile=tempfile.TemporaryFile(bufsize=0)
75 cmdHdl=sp.Popen(cmd,shell=
True,stdout=stdOutFile,stderr=stdErrFile)
78 while cmdHdl.returncode ==
None and t<TIME_OUT:
82 if t >= TIME_OUT
and not cmdHdl.returncode:
85 debugMsg(2,
"Execution timed out on Command: '%s'" % cmd)
87 DEBUG
and debugMsg(1,
"Execution timed out on Command: '%s' but it ended while trying to kill it, adjust timer" % cmd)
89 DEBUG
and debugMsg(0,
"End of Execution cicle of Command: '%s' " % cmd)
92 return (stdOutFile,stdErrFile,cmdHdl.returncode)
95 def sendmail(EmailAddress,run=123456789,body="",subject="File merge failed."):
97 from email.MIMEText
import MIMEText
98 server=socket.gethostname()
99 user=os.getenv(
"USER")
100 ServerMail=
"%s@%s" % (user,server)
101 s=smtplib.SMTP(
"localhost")
102 tolist=[EmailAddress]
103 if not body: body=
"File copy to dropbox failed by unknown reason for run:%09d on server: %s" % (run,server)
105 msg[
'Subject'] = subject
106 msg[
'From'] = ServerMail
107 msg[
'To'] = EmailAddress
108 s.sendmail(ServerMail,tolist,msg.as_string())
def getNumRunsWithinTime(path, timeDelta)
def sendmail(EmailAddress, run=123456789, body="", subject="File merge failed.")
def prettyPrintUnits(value, unit, decimals=0)
def debugMsg(level, message)
Power< A, B >::type pow(const A &a, const B &b)