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, template, output_troncate_flag): ## added by Matty
|
12 |
# pattern -> action
|
13 |
### FEDE added modify_report FOR DBS OUTPUT PUBLICATION
|
14 |
self.actions = {
|
15 |
'title' : self.title_,
|
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 |
'modify_report' : self.modifyReport_,
|
25 |
'clean_env' : self.cleanEnv_,
|
26 |
'check_output_limit' : self.checkOut_
|
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 |
self.output_troncate_flag = output_troncate_flag
|
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 |
|
48 |
return
|
49 |
|
50 |
def setAction(self, pattern, action):
|
51 |
self.actions[pattern] = action
|
52 |
return
|
53 |
|
54 |
def modifyTemplateScript(self):
|
55 |
"""
|
56 |
Create a script from scratch.
|
57 |
"""
|
58 |
|
59 |
tpl = open(self.template, 'r')
|
60 |
script = open(common.taskDB.dict('ScriptName'),'w')
|
61 |
|
62 |
for line in tpl:
|
63 |
if len(line) > 6 and line[:6] == '#CRAB ':
|
64 |
act_str = string.strip(line[6:])
|
65 |
try:
|
66 |
action = self.actions[act_str]
|
67 |
except KeyError:
|
68 |
continue
|
69 |
|
70 |
if action:
|
71 |
txt = action()
|
72 |
script.write(txt)
|
73 |
pass
|
74 |
else:
|
75 |
script.write(line)
|
76 |
pass
|
77 |
else:
|
78 |
script.write(line)
|
79 |
pass
|
80 |
pass
|
81 |
|
82 |
script.close()
|
83 |
tpl.close()
|
84 |
return
|
85 |
|
86 |
def title_(self):
|
87 |
txt = '# This script was generated by '+common.prog_name
|
88 |
txt += ' (version '+common.prog_version_str+').\n'
|
89 |
return txt
|
90 |
|
91 |
def setupSchedulerEnvironment_(self):
|
92 |
"""
|
93 |
Returns part of a job script which does scheduler-specific work.
|
94 |
"""
|
95 |
txt = common.scheduler.wsSetupEnvironment()
|
96 |
return txt
|
97 |
|
98 |
def setupJobTypeEnvironment_(self):
|
99 |
"""
|
100 |
Returns part of a job script which does jobtype-specific work.
|
101 |
"""
|
102 |
jbt = common.job_list.type()
|
103 |
txt = jbt.wsSetupEnvironment(self.nj)
|
104 |
return txt
|
105 |
|
106 |
def buildExe_(self):
|
107 |
"""
|
108 |
Returns part of a job script which builds the binary executable.
|
109 |
"""
|
110 |
jbt = common.job_list.type()
|
111 |
|
112 |
txt = jbt.wsBuildExe(self.nj)
|
113 |
|
114 |
job = common.job_list[self.nj]
|
115 |
exe = job.type().executableName()
|
116 |
|
117 |
txt += 'executable='+exe+'\n'
|
118 |
return txt
|
119 |
|
120 |
def runExe_(self):
|
121 |
"""
|
122 |
Returns part of a job script which executes the application.
|
123 |
"""
|
124 |
job = common.job_list[self.nj]
|
125 |
args = job.type().executableArgs()
|
126 |
return '$executable '+args+'\n'
|
127 |
|
128 |
def renameOutput_(self):
|
129 |
"""
|
130 |
Returns part of a job script which renames output files.
|
131 |
"""
|
132 |
jbt = common.job_list.type()
|
133 |
txt = '\n'
|
134 |
txt += jbt.wsRenameOutput(self.nj)
|
135 |
return txt
|
136 |
|
137 |
def copyInput_(self):
|
138 |
"""
|
139 |
Returns part of a job script which copies input files from SE.
|
140 |
"""
|
141 |
txt = common.scheduler.wsCopyInput()
|
142 |
return txt
|
143 |
|
144 |
def copyOutput_(self):
|
145 |
"""
|
146 |
Returns part of a job script which copies output files to SE.
|
147 |
"""
|
148 |
txt = common.scheduler.wsCopyOutput()
|
149 |
return txt
|
150 |
|
151 |
def modifyReport_(self):
|
152 |
"""
|
153 |
Returns part of a job script which modifies the FrameworkJobReport.
|
154 |
"""
|
155 |
jbt = common.job_list.type()
|
156 |
txt = jbt.modifyReport(self.nj)
|
157 |
return txt
|
158 |
|
159 |
def cleanEnv_(self):
|
160 |
"""
|
161 |
In OSG environment this function removes the WORKING_DIR
|
162 |
"""
|
163 |
jbt = common.job_list.type()
|
164 |
txt = jbt.cleanEnv()
|
165 |
return txt
|
166 |
|
167 |
def checkOut_(self):
|
168 |
"""
|
169 |
With glite check if the output is too big
|
170 |
"""
|
171 |
txt = "\n"
|
172 |
if self.output_troncate_flag == 1:
|
173 |
limit = 55000000 ##52 MB
|
174 |
jbt = common.job_list.type()
|
175 |
txt = jbt.checkOut(limit)
|
176 |
return txt
|
177 |
def rewriteCMSSWcfg_(self):
|
178 |
"""
|
179 |
Returns part of the script that runs writeCfg.py on the WN
|
180 |
"""
|
181 |
# FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
|
182 |
txt = "# Rewrite cfg for this job\n"
|
183 |
|
184 |
if self.CMSSW_major > 1: # Use py files for CMSSW 2_0_x and up
|
185 |
txt += "echo $RUNTIME_AREA/writeCfg.py --debug pset.cfg pset.pycfg\n"
|
186 |
txt += "python $RUNTIME_AREA/writeCfg.py --debug pset.cfg pset.pycfg\n"
|
187 |
else:
|
188 |
txt += "echo $RUNTIME_AREA/writeCfg.py --debug pset.cfg pset.cfg\n"
|
189 |
txt += "python $RUNTIME_AREA/writeCfg.py --debug pset.cfg pset.cfg\n"
|
190 |
|
191 |
return txt
|