-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuberPEST.py
More file actions
125 lines (106 loc) · 3.3 KB
/
uberPEST.py
File metadata and controls
125 lines (106 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#uberPEST - PEST running and notification
import gmail_sender
import os
import time
import getpass
######
# get the file name for the .rec file from the .par file
######
def read_par_file(infilename):
import string
infile=open(infilename,"r")
tmp=string.split(infile.next())
pestcall=tmp[0]
tmp=string.split(infile.next())
casename = tmp[0]
tmp=infile.next()
#read up to the '#' character to pull out the flags
indies = [i for i in xrange(len(tmp)) if tmp.startswith('#',i)]
flags = tmp[0:indies[0]]
if (string.lower(flags.strip())=='none'):
flags=False
tmp=string.split(infile.next())
email_recipient = tmp[0]
tmp=string.split(infile.next())
gmail_user = tmp[0]
tmp=string.split(infile.next())
if(string.lower(tmp[0])=='none'):
batchfile=False
else:
batchfile = tmp[0]
attachment = []
while 1:
try:
tmp=string.split(infile.next())
if(string.lower(tmp[0])=='none'):
attachment=False
infile.close()
return pestcall,casename,email_recipient,gmail_user,attachment,batchfile,flags
else:
attachment.append(tmp[0])
except:
infile.close()
return pestcall,casename,email_recipient,gmail_user,attachment,batchfile,flags
infile.close()
return pestcall,casename,email_recipient,gmail_user,attachment,batchfile,flags
#######################
# M A I N #
#######################
# read in the parfile values
(pestcall,casename,email_recipient,gmail_user,attachment,batchfile,flags) = read_par_file("uberPEST.par")
# get the email account information
# NOTE: any active gmail account can be used - account name read from the PAR file:
gmail_pwd = getpass.getpass('Please enter GMAIL password for account:\n ' + gmail_user + '\n>>')
# get the starting time
t1 = time.localtime()
# concatenate and run the system call
scall = pestcall + ' ' + casename
if (flags):
scall += ' ' + flags
os.system(scall)
# run the (optional) batch file here
if (batchfile):
os.system(batchfile)
# get the end time and elapsed time
t2 = time.localtime()
etime = int(round((time.mktime(t2)-time.mktime(t1))))
# I wish I had read documentation closer rather than spending so much effort
# calculating and formatting time! The package to use is
# datetime. live and learn...
eday = etime/60/60/24
edaystr = str(eday) + " Day"
if (eday>1):
edaystr += "s "
else:
edaystr += " "
ehour = etime/60/60%60%24
ehourstr = str(ehour) + " Hour"
if (ehour>1):
ehourstr += "s "
else:
ehourstr += " "
emin = etime/60%60
eminstr = str(emin) + " Min"
if (emin>1):
eminstr += "s "
else:
eminstr += " "
esec = etime%60
esecstr = str(esec) + " Sec"
if (esec>1):
esecstr += "s "
else:
esecstr += " "
etimeSTRING = edaystr + ehourstr + eminstr + esecstr
# compose the email here
# header
header = scall + " is done"
# body text
body = scall + " finished.\n\n" + "run start time: " + time.asctime(t1) \
+ "\n run end time: " + time.asctime(t2) \
+ "\n\nel. time: " + etimeSTRING
# write the output to the email and send it
if (attachment):
gmail_sender.mail(email_recipient,header,body,gmail_user,gmail_pwd, attachment)
else:
gmail_sender.mail(email_recipient,header,body,gmail_user,gmail_pwd)