-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdaemonB_exec.py
188 lines (166 loc) · 6.5 KB
/
daemonB_exec.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
# python script that identifies tarfiles, in a specific directory, and executes a series of commands if the tarfile is newer than the last run of the script
# The main funcitonof the script is to initiate Monte Carlo simulations using BEAMnrc
#
# Copyright [2016] [Rickard Cronholm] Licensed under the
# Educational Community License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
#http://www.osedu.org/licenses/ECL-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS"
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
import os
import sys
import datetime
import shutil
import fnmatch
import time
import samcUtil
import tarfile
import subprocess
# get current time
now = datetime.datetime.now()
newTime = now.strftime("%y%m%d%H%M%S")
# change dir to where the script is
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# check if lock file exists, otherwise create it
lockFile = 'Db.lock'
if os.path.isfile(lockFile):
print 'Busy, quitting'
sys.exit(0)
else:
flock = open(lockFile, 'w')
flock.write('Busy\n')
flock.close()
# get variables from confFile
confFile = 'samc.conf'
with open(confFile) as f:
confCont = f.readlines()
whatToGet = ['daemonB.timeFile', 'common.EGS_HOME', 'common.dosxyzDir',
'common.VCUinp', 'daemonB.nBatch', 'daemonB.beamPegs']
# generate struct
cv = samcUtil.struct()
# read variables
cv = samcUtil.getConfVars(cv, whatToGet, confCont)
# convert types
cv = samcUtil.typeCast(cv, ['nBatch'], [int])
'''
# get variables from confFile
confFile = 'samc.conf'
timeFile = samcUtil.getVariable(confFile,'daemonB.timeFile')
EGS_HOME = samcUtil.getVariable(confFile,'common.EGS_HOME')
VCUinp = samcUtil.getVariable(confFile,'common.VCUinp')
dosxyznrc = samcUtil.getVariable(confFile,'common.dosxyzDir')
nBatch = int(samcUtil.getVariable(confFile,'daemonB.nBatch'))
beamPegs = samcUtil.getVariable(confFile,'daemonB.beamPegs')
logFile = samcUtil.getVariable(confFile,'daemonB.logFile')
'''
# get time of last run
f = open(cv.timeFile, 'r')
oldTime = f.readline().rstrip()
print 'This was last run at {0}'.format(oldTime)
f.close()
# get list of tarfiles
tarfiles = []
for file in [tarfileList for tarfileList in os.listdir(dname) if tarfileList.endswith(".tar") and not tarfileList.startswith("post")]:
# get time of modification of file
fileTime = os.path.getmtime(file)
fileTime = time.localtime(fileTime)
fileTime = time.strftime("%y%m%d%H%M%S", fileTime)
# if more recent than last run, append to list
if fileTime > oldTime:
tarfiles.append(file)
print 'Found {0} tarfile(s)'.format(len(tarfiles))
# quit if no tarfiles found
if len(tarfiles) == 0:
print 'Nothing to do, exiting'
samcUtil.writeTimeToFile(cv.timeFile, newTime)
os.remove(lockFile) # remove lockFile
sys.exit()
# execute a series of commands for each tarfile found
for i in range(0, len(tarfiles)):
# myTarfile = tarfiles[i]
# os.system('tar -C %(dname)s -xf %(myTarfile)s' % locals())
# remove tarfile
#os.remove(tarfiles[i]) # uncomment this line when finnished testing
# untar
tar = tarfile.open(tarfiles[i])
tar.extractall()
tar.close()
os.remove(tarfiles[i]) # remove tarfile
# get the timeStamp from the name of the tarfile
timeStamp, fileExt = os.path.splitext(tarfiles[i])
print 'timeStamp: ', timeStamp
sys.stdout.flush()
# get BEAMdir
f = open(os.path.join(timeStamp, timeStamp + '.beamDir'), 'r')
beamDir = f.readline().rstrip()
f.close()
# get MLCdir
mlcDir = False
try:
f = open(os.path.join(timeStamp, timeStamp + '.mlcDir'), 'r')
mlcDir = f.readline().rstrip()
f.close()
except IOError:
pass
# copy files to destination folders
dirList = os.listdir(timeStamp + os.path.sep)
for file1 in dirList: # file the files
if fnmatch.fnmatch(file1, '*BEAM*'): # match the wildcard
# copy the files from origin to destination
shutil.copy2(os.path.join(timeStamp, file1), cv.EGS_HOME + beamDir)
if fnmatch.fnmatch(file1, '*vcu*'): # match the wildcard
# copy the files from origin to destination
shutil.copy2(os.path.join(timeStamp, file1), cv.VCUinp)
if fnmatch.fnmatch(file1, '*DOSXYZ*'): # match the wildcard
# copy the files from origin to destination
shutil.copy2(os.path.join(timeStamp, file1), cv.dosxyzDir)
if fnmatch.fnmatch(file1, '*phant'): # match the wildcard
# copy the files from origin to destination
shutil.copy2(os.path.join(timeStamp, file1), cv.dosxyzDir)
if mlcDir and fnmatch.fnmatch(file1, '*MLC*'): # match the wildcard
# copy the files from origin to destination
shutil.copy2(os.path.join(timeStamp, file1), cv.EGS_HOME + mlcDir)
# get number of beams
f = open(os.path.join(timeStamp, timeStamp + '.numBeams'), 'r')
numBeams = int(f.readline().rstrip())
for j in range(0, numBeams):
thisBeamName = timeStamp + '_' + str(j) + '_BEAMnrc'
if j == 0:
if not os.path.exists(os.path.sep.join([cv.EGS_HOME + beamDir, '.'.join([thisBeamName, 'egsinp'])])):
thisBeamName = timeStamp + '_BEAMnrc'
HEN_HOUSE = os.environ.get('HEN_HOUSE')
exb = 'scripts/run_user_code_batch'
exbCall = ''.join([HEN_HOUSE, exb])
'''
f = open('/home/ricr/MCQA/queue', 'a')
f.write('{0} {1} {2} {3} p={4}\n'.format(exbCall, cv.beamDir,
thisBeamName, cv.beamPegs, cv.nBatch))
f.close()
'''
#os.system('%(exbCall)s %(cv.beamDir)s %(thisBeamName)s %(cv.beamPegs)s p=%(cv.nBatch)s' % locals())
subprocess.call([exbCall, beamDir, thisBeamName, cv.beamPegs,
'='.join(['p', str(cv.nBatch)])])
'''
# write to log file
if i==0: # overwrite existing file
flog = open(logFile, 'w')
else: # append to file
flog = open(logFile, 'a')
# get currentTime
currTime = datetime.datetime.now()
flog.write('{0}\t{1}\n'.format(timeStamp,currTime.strftime("%y%m%d%H%M%S")))
flog.close()
'''
# update time of last run
samcUtil.writeTimeToFile(cv.timeFile, newTime)
os.remove(lockFile) # remove lockFile