ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.43
Committed: Tue May 26 10:23:01 2009 UTC (15 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_6_0_pre10, CRAB_2_6_0_pre9, CRAB_2_6_0_pre8, CRAB_2_6_0_pre7, CRAB_2_6_0_pre6, CRAB_2_6_0_pre5, CRAB_2_6_0_pre4, CRAB_2_6_0_pre3
Changes since 1.42: +0 -1 lines
Log Message:
adapting code to logging usage. (crab_logger removed)

File Contents

# Content
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 return
53
54 def setAction(self, pattern, action):
55 self.actions[pattern] = action
56 return
57
58 def modifyTemplateScript(self):
59 """
60 Create a script from scratch.
61 """
62
63 tpl = open(self.template, 'r')
64
65 wrapper_fullPath = common.work_space.jobDir()+self.scriptName
66 script = open(wrapper_fullPath,'w')
67
68 for line in tpl:
69 if len(line) > 6 and line[:6] == '#CRAB ':
70 act_str = string.strip(line[6:])
71 try:
72 action = self.actions[act_str]
73 except KeyError:
74 continue
75
76 if action:
77 txt = action()
78 script.write(txt)
79 pass
80 else:
81 script.write(line)
82 pass
83 else:
84 script.write(line)
85 pass
86 pass
87
88 script.close()
89 tpl.close()
90 os.chmod(wrapper_fullPath, 0744)
91 return
92
93 def title_(self):
94 txt = '# This script was generated by '+common.prog_name
95 txt += ' (version '+common.prog_version_str+').\n'
96 return txt
97
98
99 ### FEDE ###
100
101 def untarSoftware_(self):
102 """
103 Returns part of a job script which untar CMSSW software.
104 """
105 jbt = common.job_list.type()
106
107 txt = jbt.wsUntarSoftware(self.nj)
108
109 #txt += 'executable='+exe+'\n'
110 return txt
111
112 ###########################################
113
114 def setupSchedulerEnvironment_(self):
115 """
116 Returns part of a job script which does scheduler-specific work.
117 """
118 txt = common.scheduler.wsSetupEnvironment()
119 return txt
120
121 def initialEnvironment_(self):
122 """
123 Returns part of a job script which does scheduler-specific work.
124 """
125 txt = common.scheduler.wsInitialEnvironment()
126 return txt
127
128 def setupJobTypeEnvironment_(self):
129 """
130 Returns part of a job script which does jobtype-specific work.
131 """
132 jbt = common.job_list.type()
133 txt = jbt.wsSetupEnvironment(self.nj)
134 return txt
135
136 def buildExe_(self):
137 """
138 Returns part of a job script which builds the binary executable.
139 """
140 jbt = common.job_list.type()
141
142 txt = jbt.wsBuildExe(self.nj)
143
144 job = common.job_list[self.nj]
145 exe = job.type().executableName()
146
147 txt += 'executable='+exe+'\n'
148 return txt
149
150 def runExe_(self):
151 """
152 Returns part of a job script which executes the application.
153 """
154 job = common.job_list[self.nj]
155 args = job.type().executableArgs()
156
157 txt = ''
158 txt += 'CPU_INFOS=-1 \n'
159 # NO carriage return for this line #Fabio
160 txt += '/usr/bin/time -f \"%U %S %P\" -o cpu_timing.txt '
161 txt += '$executable '+args+'\n'
162 return txt
163
164 def renameOutput_(self):
165 """
166 Returns part of a job script which renames output files.
167 """
168 jbt = common.job_list.type()
169 txt = '\n'
170 txt += jbt.wsRenameOutput(self.nj)
171 return txt
172
173 def copyInput_(self):
174 """
175 Returns part of a job script which copies input files from SE.
176 """
177 txt = common.scheduler.wsCopyInput()
178 return txt
179
180 def copyOutput_(self):
181 """
182 Returns part of a job script which copies output files to SE.
183 """
184 txt = common.scheduler.wsCopyOutput()
185 return txt
186
187 def parseReport_(self):
188 """
189 Returns part of a job script which parse the FrameworkJobReport.
190 """
191 jbt = common.job_list.type()
192 txt = jbt.wsParseFJR()
193 return txt
194
195 def modifyReport_(self):
196 """
197 Returns part of a job script which modifies the FrameworkJobReport.
198 """
199 jbt = common.job_list.type()
200 txt = jbt.wsModifyReport(self.nj)
201 return txt
202
203 def cleanEnv_(self):
204 """
205 In OSG environment this function removes the WORKING_DIR
206 """
207 jbt = common.job_list.type()
208 txt = jbt.cleanEnv()
209 return txt
210
211 def func_exit_(self):
212 """
213 Returns part of a job script which does scheduler-specific
214 output checks and management.
215 """
216 txt = common.scheduler.wsExitFunc()
217 return txt
218
219 def rewriteCMSSWcfg_(self):
220 """
221 Returns part of the script that runs writeCfg.py on the WN
222 """
223 # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
224 txt = "# Rewrite cfg for this job\n"
225
226 if (self.CMSSW_major >= 2 and self.CMSSW_minor >= 1) or self.CMSSW_major > 2: # py in, py out for 2_1_x
227 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
228 txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
229 elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
230 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
231 txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
232 else: # cfg in, cfg out for 1_x_y
233 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
234 txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
235
236 return txt