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