1 |
from WorkSpace import WorkSpace
|
2 |
from JobList import JobList
|
3 |
from Scheduler import Scheduler
|
4 |
from crab_exceptions import *
|
5 |
import common
|
6 |
import Scram
|
7 |
import string,os
|
8 |
|
9 |
class ScriptWriter:
|
10 |
def __init__(self, cfg_params, template):
|
11 |
# pattern -> action
|
12 |
self.actions = {
|
13 |
'title' : self.title_,
|
14 |
'untar_software' : self.untarSoftware_,
|
15 |
'initial_environment' : self.initialEnvironment_,
|
16 |
'setup_scheduler_environment' : self.setupSchedulerEnvironment_,
|
17 |
'setup_jobtype_environment' : self.setupJobTypeEnvironment_,
|
18 |
'copy_input' : self.copyInput_,
|
19 |
'rewrite_cmssw_cfg' : self.rewriteCMSSWcfg_,
|
20 |
'build_executable' : self.buildExe_,
|
21 |
'run_executable' : self.runExe_,
|
22 |
'rename_output' : self.renameOutput_,
|
23 |
'copy_output' : self.copyOutput_,
|
24 |
'parse_report' : self.parseReport_,
|
25 |
'modify_report' : self.modifyReport_,
|
26 |
'func_exit' : self.func_exit_
|
27 |
}
|
28 |
|
29 |
if os.path.isfile("./"+template):
|
30 |
self.template = "./"+template
|
31 |
elif os.getenv('CRABDIR') and os.path.isfile(os.getenv('CRABDIR')+'/python/'+template):
|
32 |
self.template = os.getenv('CRABDIR')+'/python/'+template
|
33 |
else:
|
34 |
raise CrabException("No crab_template.sh found!")
|
35 |
self.nj = -1 # current job number
|
36 |
|
37 |
try:
|
38 |
self.scram = Scram.Scram(None)
|
39 |
self.CMSSWversion = self.scram.getSWVersion()
|
40 |
parts = self.CMSSWversion.split('_')
|
41 |
self.CMSSW_major = int(parts[1])
|
42 |
self.CMSSW_minor = int(parts[2])
|
43 |
self.CMSSW_patch = int(parts[3])
|
44 |
except:
|
45 |
raise CrabException("Could not determine CMSSW version")
|
46 |
self.debug_wrapper=''
|
47 |
debug = int(cfg_params.get('USER.debug_wrapper',0))
|
48 |
if debug==1 : self.debug_wrapper='--debug'
|
49 |
|
50 |
self.scriptName = cfg_params.get('CRAB.jobtype').upper()+'.sh'
|
51 |
|
52 |
self.pset = cfg_params.get('CMSSW.pset','None')
|
53 |
|
54 |
return
|
55 |
|
56 |
def setAction(self, pattern, action):
|
57 |
self.actions[pattern] = action
|
58 |
return
|
59 |
|
60 |
def modifyTemplateScript(self):
|
61 |
"""
|
62 |
Create a script from scratch.
|
63 |
"""
|
64 |
|
65 |
tpl = open(self.template, 'r')
|
66 |
|
67 |
wrapper_fullPath = common.work_space.jobDir()+self.scriptName
|
68 |
script = open(wrapper_fullPath,'w')
|
69 |
|
70 |
for line in tpl:
|
71 |
if len(line) > 6 and line[:6] == '#CRAB ':
|
72 |
act_str = string.strip(line[6:])
|
73 |
try:
|
74 |
action = self.actions[act_str]
|
75 |
except KeyError:
|
76 |
continue
|
77 |
|
78 |
if action:
|
79 |
txt = action()
|
80 |
script.write(txt)
|
81 |
pass
|
82 |
else:
|
83 |
script.write(line)
|
84 |
pass
|
85 |
else:
|
86 |
script.write(line)
|
87 |
pass
|
88 |
pass
|
89 |
|
90 |
script.close()
|
91 |
tpl.close()
|
92 |
os.chmod(wrapper_fullPath, 0744)
|
93 |
return
|
94 |
|
95 |
def title_(self):
|
96 |
#txt = '# This script was generated by '+common.prog_name
|
97 |
#txt += ' (version '+common.prog_version_str+').\n'
|
98 |
txt = 'echo "This script was generated by '+common.prog_name
|
99 |
txt += ' (version '+common.prog_version_str+')."'
|
100 |
return txt
|
101 |
|
102 |
|
103 |
### FEDE ###
|
104 |
|
105 |
def untarSoftware_(self):
|
106 |
"""
|
107 |
Returns part of a job script which untar CMSSW software.
|
108 |
"""
|
109 |
jbt = common.job_list.type()
|
110 |
|
111 |
txt = jbt.wsUntarSoftware(self.nj)
|
112 |
|
113 |
#txt += 'executable='+exe+'\n'
|
114 |
return txt
|
115 |
|
116 |
###########################################
|
117 |
|
118 |
def setupSchedulerEnvironment_(self):
|
119 |
"""
|
120 |
Returns part of a job script which does scheduler-specific work.
|
121 |
"""
|
122 |
txt = common.scheduler.wsSetupEnvironment()
|
123 |
return txt
|
124 |
|
125 |
def initialEnvironment_(self):
|
126 |
"""
|
127 |
Returns part of a job script which does scheduler-specific work.
|
128 |
"""
|
129 |
txt = common.scheduler.wsInitialEnvironment()
|
130 |
return txt
|
131 |
|
132 |
def setupJobTypeEnvironment_(self):
|
133 |
"""
|
134 |
Returns part of a job script which does jobtype-specific work.
|
135 |
"""
|
136 |
jbt = common.job_list.type()
|
137 |
txt = jbt.wsSetupEnvironment(self.nj)
|
138 |
return txt
|
139 |
|
140 |
def buildExe_(self):
|
141 |
"""
|
142 |
Returns part of a job script which builds the binary executable.
|
143 |
"""
|
144 |
jbt = common.job_list.type()
|
145 |
|
146 |
txt = jbt.wsBuildExe(self.nj)
|
147 |
|
148 |
job = common.job_list[self.nj]
|
149 |
exe = job.type().executableName()
|
150 |
|
151 |
txt += 'executable='+exe+'\n'
|
152 |
return txt
|
153 |
|
154 |
def runExe_(self):
|
155 |
"""
|
156 |
Returns part of a job script which executes the application.
|
157 |
"""
|
158 |
job = common.job_list[self.nj]
|
159 |
args = job.type().executableArgs()
|
160 |
|
161 |
txt = ''
|
162 |
txt += 'CPU_INFOS=0 \n'
|
163 |
# NO carriage return for this line #Fabio
|
164 |
txt += '/usr/bin/time -f \"%U %S %P\" -o cpu_timing.txt '
|
165 |
txt += '$executable '+args
|
166 |
txt += ' > executable.out 2>&1 \n'
|
167 |
return txt
|
168 |
|
169 |
def renameOutput_(self):
|
170 |
"""
|
171 |
Returns part of a job script which renames output files.
|
172 |
"""
|
173 |
jbt = common.job_list.type()
|
174 |
txt = '\n'
|
175 |
txt += jbt.wsRenameOutput(self.nj)
|
176 |
return txt
|
177 |
|
178 |
def copyInput_(self):
|
179 |
"""
|
180 |
Returns part of a job script which copies input files from SE.
|
181 |
"""
|
182 |
txt = common.scheduler.wsCopyInput()
|
183 |
return txt
|
184 |
|
185 |
def copyOutput_(self):
|
186 |
"""
|
187 |
Returns part of a job script which copies output files to SE.
|
188 |
"""
|
189 |
txt = common.scheduler.wsCopyOutput()
|
190 |
return txt
|
191 |
|
192 |
def parseReport_(self):
|
193 |
"""
|
194 |
Returns part of a job script which parse the FrameworkJobReport.
|
195 |
"""
|
196 |
jbt = common.job_list.type()
|
197 |
txt = jbt.wsParseFJR()
|
198 |
return txt
|
199 |
|
200 |
def modifyReport_(self):
|
201 |
"""
|
202 |
Returns part of a job script which modifies the FrameworkJobReport.
|
203 |
"""
|
204 |
jbt = common.job_list.type()
|
205 |
txt = jbt.wsModifyReport(self.nj)
|
206 |
return txt
|
207 |
|
208 |
def cleanEnv_(self):
|
209 |
"""
|
210 |
In OSG environment this function removes the WORKING_DIR
|
211 |
"""
|
212 |
jbt = common.job_list.type()
|
213 |
txt = jbt.cleanEnv()
|
214 |
return txt
|
215 |
|
216 |
def func_exit_(self):
|
217 |
"""
|
218 |
Returns part of a job script which does scheduler-specific
|
219 |
output checks and management.
|
220 |
"""
|
221 |
txt = common.scheduler.wsExitFunc()
|
222 |
return txt
|
223 |
|
224 |
def rewriteCMSSWcfg_(self):
|
225 |
"""
|
226 |
Returns part of the script that runs writeCfg.py on the WN
|
227 |
"""
|
228 |
txt=''
|
229 |
if str(self.pset).upper() != 'NONE':
|
230 |
txt += "# Rewrite cfg for this job\n"
|
231 |
txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
|
232 |
txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
|
233 |
txt += """
|
234 |
result=$?
|
235 |
if [ $result -ne 0 ]; then
|
236 |
echo "ERROR ==> problem re-writing config file"
|
237 |
job_exit_code=10040
|
238 |
func_exit
|
239 |
fi\n
|
240 |
"""
|
241 |
txt += " \n"
|
242 |
txt += "cat $RUNTIME_AREA/inputsReport.txt \n"
|
243 |
# txt += "cat inputsReport.txt >> $RUNTIME_AREA/$repo \n"
|
244 |
# txt += "dumpStatus $RUNTIME_AREA/$repo\n"
|
245 |
|
246 |
return txt
|