1 |
from WorkSpace import WorkSpace
|
2 |
from JobList import JobList
|
3 |
from Scheduler import Scheduler
|
4 |
from crab_logger import Logger
|
5 |
from crab_exceptions import *
|
6 |
import common
|
7 |
|
8 |
import string
|
9 |
|
10 |
class ScriptWriter:
|
11 |
def __init__(self, template):
|
12 |
# pattern -> action
|
13 |
self.actions = {
|
14 |
'title' : self.title_,
|
15 |
'setup_monitoring' : None,
|
16 |
'setup_scheduler_environment' : None,
|
17 |
'setup_jobtype_environment' : None,
|
18 |
'copy_input_data' : None,
|
19 |
'build_executable' : self.buildExe_,
|
20 |
'run_executable' : self.runExe_,
|
21 |
'stop_monitoring' : None,
|
22 |
'move_output' : None,
|
23 |
'register_results' : None,
|
24 |
'make_summary' : None,
|
25 |
'notify' : None
|
26 |
}
|
27 |
|
28 |
self.template = template
|
29 |
self.nj = -1 # current job number
|
30 |
return
|
31 |
|
32 |
def setAction(self, pattern, action):
|
33 |
self.actions[pattern] = action
|
34 |
return
|
35 |
|
36 |
def modifyTemplateScript(self, nj):
|
37 |
"""
|
38 |
Create a script from scratch.
|
39 |
"""
|
40 |
self.nj = nj
|
41 |
|
42 |
tpl = open(self.template, 'r')
|
43 |
script = open(common.job_list[nj].scriptFilename(), 'w')
|
44 |
|
45 |
for line in tpl:
|
46 |
if len(line) > 6 and line[:6] == '#CRAB ':
|
47 |
act_str = string.strip(line[6:])
|
48 |
try:
|
49 |
action = self.actions[act_str]
|
50 |
except KeyError:
|
51 |
continue
|
52 |
|
53 |
if action:
|
54 |
txt = action()
|
55 |
script.write(txt)
|
56 |
pass
|
57 |
else:
|
58 |
script.write(line)
|
59 |
pass
|
60 |
else:
|
61 |
script.write(line)
|
62 |
pass
|
63 |
pass
|
64 |
|
65 |
script.close()
|
66 |
tpl.close()
|
67 |
return
|
68 |
|
69 |
def title_(self):
|
70 |
txt = '# This script was generated by '+common.prog_name
|
71 |
txt += ' (version '+common.prog_version_str+').\n'
|
72 |
return txt
|
73 |
|
74 |
def setupEnvironment(self, script, nj):
|
75 |
""" Write SetupEnvironment part of a job script."""
|
76 |
|
77 |
script.write('#\n')
|
78 |
script.write('# SETUP ENVIRONMENT\n')
|
79 |
script.write('#\n')
|
80 |
#TODO
|
81 |
#common.scheduler.writeScript_SetupEnvironment(script, nj)
|
82 |
#common.job_list[nj].type().writeScript_SetupEnvironment(script, nj)
|
83 |
script.write('#\n')
|
84 |
script.write('# END OF SETUP ENVIRONMENT\n')
|
85 |
script.write('#\n')
|
86 |
return
|
87 |
|
88 |
def copyInputData(self, script, nj):
|
89 |
#TODO
|
90 |
#common.scheduler.writeScript_CopyInputData(script, nj)
|
91 |
return
|
92 |
|
93 |
def addFiles(self, script, nj):
|
94 |
"""
|
95 |
Add into the script the content of some job-specific files.
|
96 |
"""
|
97 |
#TODO: ???
|
98 |
#common.job_list.type().writeScript_AddFiles(script, nj)
|
99 |
return
|
100 |
|
101 |
def buildExe_(self):
|
102 |
"""
|
103 |
Returns part of a job script which builds the binary executable.
|
104 |
"""
|
105 |
jbt = common.job_list.type()
|
106 |
|
107 |
txt = jbt.writeScript_BuildExe(self.nj)
|
108 |
|
109 |
job = common.job_list[self.nj]
|
110 |
exe = job.type().executableName()
|
111 |
|
112 |
txt += 'executable='+exe+'\n'
|
113 |
return txt
|
114 |
|
115 |
def runExe_(self):
|
116 |
"""
|
117 |
Returns part of a job script which executes the application.
|
118 |
"""
|
119 |
#jbt = common.job_list.type()
|
120 |
|
121 |
#jbt.writeScript_RunExe(script, self.nj)
|
122 |
return '$executable\n'
|
123 |
|
124 |
def move_output(self, script,nj):
|
125 |
return
|
126 |
|
127 |
def registerResults(self, script,nj):
|
128 |
return
|
129 |
|
130 |
def tail(self, script, nj):
|
131 |
""" Write a tailer part of a job script."""
|
132 |
|
133 |
script.write('#\n')
|
134 |
script.write('# TAIL\n')
|
135 |
script.write('#\n')
|
136 |
script.write('pwd\n')
|
137 |
script.write('echo "ls -Al"\n')
|
138 |
script.write('ls -Al\n')
|
139 |
|
140 |
#if common.use_jam:
|
141 |
# script.write('list=`ls -Al` \n')
|
142 |
# script.write('perl $RUNTIME_AREA/'+ common.run_jam +' --name='+common.output_jam+' --event=List_end --det="$list" \n')
|
143 |
# pass
|
144 |
|
145 |
script.write('#\n')
|
146 |
|
147 |
#TODO
|
148 |
#if common.flag_mksmry:
|
149 |
# script.write('chmod u+x postprocess\n')
|
150 |
# script.write('cat `ls -1 *.stdout` | ./postprocess | sort | uniq > sumry\n')
|
151 |
# pass
|
152 |
|
153 |
#TODO
|
154 |
# summary file may need jobtype specific info, e.g.
|
155 |
# for CMS Oscar it needs Pool catalogues,
|
156 |
# so we delegate operations to the related jobtype object.
|
157 |
#common.job_list.type().writeScript_Tail(script, nj)
|
158 |
|
159 |
#if common.flag_notify:
|
160 |
# script.write('if [[ $executable_exit_status -eq 0 && $replica_exit_status -eq 0 ]]; then\n')
|
161 |
# if common.flag_mksmry:
|
162 |
# script.write(' cat sumry | mail -s "job_finished" '+
|
163 |
# common.email +'\n')
|
164 |
# else:
|
165 |
# script.write(' mail -s "job_finished" '+
|
166 |
# common.email +' <<EOF\n')
|
167 |
# n1 = nj + 1
|
168 |
# script.write('Job # '+`n1`+' finished\n')
|
169 |
# script.write('EOF\n')
|
170 |
# pass
|
171 |
# script.write('fi\n')
|
172 |
# pass
|
173 |
|
174 |
script.write('echo ">>>>>>>> End of job at date `date`" \n')
|
175 |
|
176 |
#if common.use_jam:
|
177 |
# script.write('perl $RUNTIME_AREA/'+ common.run_jam +' --name='+common.output_jam+' --event=exit --det="$exit_status" \n')
|
178 |
# pass
|
179 |
|
180 |
script.write('exit $exit_status\n')
|
181 |
return
|
182 |
|