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,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 |
'build_executable' : self.buildExe_,
|
20 |
'run_executable' : self.runExe_,
|
21 |
'rename_output' : self.renameOutput_,
|
22 |
'copy_output' : self.copyOutput_,
|
23 |
#'register_output' : self.registerOutput_,
|
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 |
return
|
39 |
|
40 |
def setAction(self, pattern, action):
|
41 |
self.actions[pattern] = action
|
42 |
return
|
43 |
|
44 |
def modifyTemplateScript(self):
|
45 |
"""
|
46 |
Create a script from scratch.
|
47 |
"""
|
48 |
|
49 |
tpl = open(self.template, 'r')
|
50 |
script = open(common.taskDB.dict('ScriptName'),'w')
|
51 |
|
52 |
for line in tpl:
|
53 |
if len(line) > 6 and line[:6] == '#CRAB ':
|
54 |
act_str = string.strip(line[6:])
|
55 |
try:
|
56 |
action = self.actions[act_str]
|
57 |
except KeyError:
|
58 |
continue
|
59 |
|
60 |
if action:
|
61 |
txt = action()
|
62 |
script.write(txt)
|
63 |
pass
|
64 |
else:
|
65 |
script.write(line)
|
66 |
pass
|
67 |
else:
|
68 |
script.write(line)
|
69 |
pass
|
70 |
pass
|
71 |
|
72 |
script.close()
|
73 |
tpl.close()
|
74 |
return
|
75 |
|
76 |
def title_(self):
|
77 |
txt = '# This script was generated by '+common.prog_name
|
78 |
txt += ' (version '+common.prog_version_str+').\n'
|
79 |
return txt
|
80 |
|
81 |
def setupSchedulerEnvironment_(self):
|
82 |
"""
|
83 |
Returns part of a job script which does scheduler-specific work.
|
84 |
"""
|
85 |
txt = common.scheduler.wsSetupEnvironment()
|
86 |
return txt
|
87 |
|
88 |
def setupJobTypeEnvironment_(self):
|
89 |
"""
|
90 |
Returns part of a job script which does jobtype-specific work.
|
91 |
"""
|
92 |
jbt = common.job_list.type()
|
93 |
txt = jbt.wsSetupEnvironment(self.nj)
|
94 |
return txt
|
95 |
|
96 |
def buildExe_(self):
|
97 |
"""
|
98 |
Returns part of a job script which builds the binary executable.
|
99 |
"""
|
100 |
jbt = common.job_list.type()
|
101 |
|
102 |
txt = jbt.wsBuildExe(self.nj)
|
103 |
|
104 |
job = common.job_list[self.nj]
|
105 |
exe = job.type().executableName()
|
106 |
|
107 |
txt += 'executable='+exe+'\n'
|
108 |
return txt
|
109 |
|
110 |
def runExe_(self):
|
111 |
"""
|
112 |
Returns part of a job script which executes the application.
|
113 |
"""
|
114 |
job = common.job_list[self.nj]
|
115 |
args = job.type().executableArgs()
|
116 |
return '$executable '+args+'\n'
|
117 |
|
118 |
def renameOutput_(self):
|
119 |
"""
|
120 |
Returns part of a job script which renames output files.
|
121 |
"""
|
122 |
jbt = common.job_list.type()
|
123 |
txt = '\n'
|
124 |
txt += jbt.wsRenameOutput(self.nj)
|
125 |
return txt
|
126 |
|
127 |
def copyInput_(self):
|
128 |
"""
|
129 |
Returns part of a job script which copies input files from SE.
|
130 |
"""
|
131 |
txt = common.scheduler.wsCopyInput()
|
132 |
return txt
|
133 |
|
134 |
def copyOutput_(self):
|
135 |
"""
|
136 |
Returns part of a job script which copies output files to SE.
|
137 |
"""
|
138 |
txt = common.scheduler.wsCopyOutput()
|
139 |
return txt
|
140 |
|
141 |
#def registerOutput_(self):
|
142 |
# """
|
143 |
# Returns part of a job script which registers output files to RLS catalog.
|
144 |
# """
|
145 |
# txt = ''
|
146 |
# return txt
|
147 |
|
148 |
def modifyReport_(self):
|
149 |
"""
|
150 |
Returns part of a job script which modifies the FrameworkJobReport.
|
151 |
"""
|
152 |
jbt = common.job_list.type()
|
153 |
txt = jbt.modifyReport(self.nj)
|
154 |
return txt
|
155 |
|
156 |
def cleanEnv_(self):
|
157 |
"""
|
158 |
In OSG environment this function removes the WORKING_DIR
|
159 |
"""
|
160 |
jbt = common.job_list.type()
|
161 |
txt = jbt.cleanEnv()
|
162 |
return txt
|
163 |
|
164 |
def checkOut_(self):
|
165 |
"""
|
166 |
With glite check if the output is too big
|
167 |
"""
|
168 |
txt = "\n"
|
169 |
if self.output_troncate_flag == 1:
|
170 |
limit = 55000000 ##52 MB
|
171 |
jbt = common.job_list.type()
|
172 |
txt = jbt.checkOut(limit)
|
173 |
return txt
|