ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.23
Committed: Tue Mar 18 15:43:43 2008 UTC (17 years, 1 month ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_2_0_pre4, CRAB_2_2_0_pre2
Changes since 1.22: +17 -0 lines
Log Message:
changes to write the FJR from beginning of job wrapper

File Contents

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