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 |
return txt
|
99 |
|
100 |
|
101 |
### FEDE ###
|
102 |
|
103 |
def untarSoftware_(self):
|
104 |
"""
|
105 |
Returns part of a job script which untar CMSSW software.
|
106 |
"""
|
107 |
jbt = common.job_list.type()
|
108 |
|
109 |
txt = jbt.wsUntarSoftware(self.nj)
|
110 |
|
111 |
#txt += 'executable='+exe+'\n'
|
112 |
return txt
|
113 |
|
114 |
###########################################
|
115 |
|
116 |
def setupSchedulerEnvironment_(self):
|
117 |
"""
|
118 |
Returns part of a job script which does scheduler-specific work.
|
119 |
"""
|
120 |
txt = common.scheduler.wsSetupEnvironment()
|
121 |
return txt
|
122 |
|
123 |
def initialEnvironment_(self):
|
124 |
"""
|
125 |
Returns part of a job script which does scheduler-specific work.
|
126 |
"""
|
127 |
txt = common.scheduler.wsInitialEnvironment()
|
128 |
return txt
|
129 |
|
130 |
def setupJobTypeEnvironment_(self):
|
131 |
"""
|
132 |
Returns part of a job script which does jobtype-specific work.
|
133 |
"""
|
134 |
jbt = common.job_list.type()
|
135 |
txt = jbt.wsSetupEnvironment(self.nj)
|
136 |
return txt
|
137 |
|
138 |
def buildExe_(self):
|
139 |
"""
|
140 |
Returns part of a job script which builds the binary executable.
|
141 |
"""
|
142 |
jbt = common.job_list.type()
|
143 |
|
144 |
txt = jbt.wsBuildExe(self.nj)
|
145 |
|
146 |
job = common.job_list[self.nj]
|
147 |
exe = job.type().executableName()
|
148 |
|
149 |
txt += 'executable='+exe+'\n'
|
150 |
return txt
|
151 |
|
152 |
def runExe_(self):
|
153 |
"""
|
154 |
Returns part of a job script which executes the application.
|
155 |
"""
|
156 |
job = common.job_list[self.nj]
|
157 |
args = job.type().executableArgs()
|
158 |
|
159 |
txt = ''
|
160 |
txt += 'CPU_INFOS=-1 \n'
|
161 |
# NO carriage return for this line #Fabio
|
162 |
txt += '/usr/bin/time -f \"%U %S %P\" -o cpu_timing.txt '
|
163 |
txt += '$executable '+args+'\n'
|
164 |
return txt
|
165 |
|
166 |
def renameOutput_(self):
|
167 |
"""
|
168 |
Returns part of a job script which renames output files.
|
169 |
"""
|
170 |
jbt = common.job_list.type()
|
171 |
txt = '\n'
|
172 |
txt += jbt.wsRenameOutput(self.nj)
|
173 |
return txt
|
174 |
|
175 |
def copyInput_(self):
|
176 |
"""
|
177 |
Returns part of a job script which copies input files from SE.
|
178 |
"""
|
179 |
txt = common.scheduler.wsCopyInput()
|
180 |
return txt
|
181 |
|
182 |
def copyOutput_(self):
|
183 |
"""
|
184 |
Returns part of a job script which copies output files to SE.
|
185 |
"""
|
186 |
txt = common.scheduler.wsCopyOutput()
|
187 |
return txt
|
188 |
|
189 |
def parseReport_(self):
|
190 |
"""
|
191 |
Returns part of a job script which parse the FrameworkJobReport.
|
192 |
"""
|
193 |
jbt = common.job_list.type()
|
194 |
txt = jbt.wsParseFJR()
|
195 |
return txt
|
196 |
|
197 |
def modifyReport_(self):
|
198 |
"""
|
199 |
Returns part of a job script which modifies the FrameworkJobReport.
|
200 |
"""
|
201 |
jbt = common.job_list.type()
|
202 |
txt = jbt.wsModifyReport(self.nj)
|
203 |
return txt
|
204 |
|
205 |
def cleanEnv_(self):
|
206 |
"""
|
207 |
In OSG environment this function removes the WORKING_DIR
|
208 |
"""
|
209 |
jbt = common.job_list.type()
|
210 |
txt = jbt.cleanEnv()
|
211 |
return txt
|
212 |
|
213 |
def func_exit_(self):
|
214 |
"""
|
215 |
Returns part of a job script which does scheduler-specific
|
216 |
output checks and management.
|
217 |
"""
|
218 |
txt = common.scheduler.wsExitFunc()
|
219 |
return txt
|
220 |
|
221 |
def rewriteCMSSWcfg_(self):
|
222 |
"""
|
223 |
Returns part of the script that runs writeCfg.py on the WN
|
224 |
"""
|
225 |
# FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
|
226 |
txt=''
|
227 |
if str(self.pset).upper() != 'NONE':
|
228 |
txt += "# Rewrite cfg for this job\n"
|
229 |
|
230 |
if (self.CMSSW_major >= 2 and self.CMSSW_minor >= 1) or self.CMSSW_major > 2: # py in, py out for 2_1_x
|
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 |
elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
|
234 |
txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
|
235 |
txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
|
236 |
else: # cfg in, cfg out for 1_x_y
|
237 |
txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
|
238 |
txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
|
239 |
|
240 |
return txt
|