ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.36
Committed: Sun Sep 21 10:33:22 2008 UTC (16 years, 7 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_0_pre1
Changes since 1.35: +1 -1 lines
Log Message:
modifyReport --> wsModifyReport

File Contents

# User Rev Content
1 nsmirnov 1.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 ewv 1.21 import Scram
8 slacapra 1.6 import string,os
9 nsmirnov 1.1
10     class ScriptWriter:
11 ewv 1.32 def __init__(self, cfg_params, template):
12 nsmirnov 1.1 # pattern -> action
13     self.actions = {
14 nsmirnov 1.3 'title' : self.title_,
15 fanzago 1.23 'untar_software' : self.untarSoftware_,
16 ewv 1.32 'initial_environment' : self.initialEnvironment_,
17 nsmirnov 1.3 'setup_scheduler_environment' : self.setupSchedulerEnvironment_,
18     'setup_jobtype_environment' : self.setupJobTypeEnvironment_,
19 fanzago 1.8 'copy_input' : self.copyInput_,
20 ewv 1.21 'rewrite_cmssw_cfg' : self.rewriteCMSSWcfg_,
21 nsmirnov 1.3 'build_executable' : self.buildExe_,
22     'run_executable' : self.runExe_,
23     'rename_output' : self.renameOutput_,
24 fanzago 1.5 'copy_output' : self.copyOutput_,
25 spiga 1.30 'parse_report' : self.parseReport_,
26 fanzago 1.12 'modify_report' : self.modifyReport_,
27 spiga 1.27 'func_exit' : self.func_exit_
28 nsmirnov 1.1 }
29 ewv 1.21
30 slacapra 1.6 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 nsmirnov 1.2 self.nj = -1 # current job number
37 mcinquil 1.14
38 ewv 1.21 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 spiga 1.33 self.debug_wrapper=''
48     debug = cfg_params.get('USER.debug_wrapper',False)
49     if debug: self.debug_wrapper='--debug'
50    
51 nsmirnov 1.1 return
52    
53     def setAction(self, pattern, action):
54     self.actions[pattern] = action
55     return
56 ewv 1.21
57 slacapra 1.10 def modifyTemplateScript(self):
58 nsmirnov 1.1 """
59     Create a script from scratch.
60     """
61 ewv 1.21
62 nsmirnov 1.1 tpl = open(self.template, 'r')
63 spiga 1.22 script = open(common._db.queryTask('scriptName'),'w')
64 nsmirnov 1.1
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 nsmirnov 1.2 def title_(self):
90 nsmirnov 1.1 txt = '# This script was generated by '+common.prog_name
91     txt += ' (version '+common.prog_version_str+').\n'
92     return txt
93 ewv 1.26
94 fanzago 1.23
95     ### FEDE ###
96 ewv 1.26
97 fanzago 1.23 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 ewv 1.26
108 fanzago 1.23 ###########################################
109 ewv 1.21
110 nsmirnov 1.3 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 nsmirnov 1.1
117 ewv 1.32 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 nsmirnov 1.3 def setupJobTypeEnvironment_(self):
125 nsmirnov 1.1 """
126 fanzago 1.7 Returns part of a job script which does jobtype-specific work.
127 nsmirnov 1.1 """
128 nsmirnov 1.3 jbt = common.job_list.type()
129     txt = jbt.wsSetupEnvironment(self.nj)
130     return txt
131 ewv 1.21
132 nsmirnov 1.2 def buildExe_(self):
133 nsmirnov 1.1 """
134 nsmirnov 1.2 Returns part of a job script which builds the binary executable.
135 nsmirnov 1.1 """
136 nsmirnov 1.2 jbt = common.job_list.type()
137 nsmirnov 1.1
138 nsmirnov 1.3 txt = jbt.wsBuildExe(self.nj)
139 nsmirnov 1.1
140 nsmirnov 1.2 job = common.job_list[self.nj]
141 nsmirnov 1.1 exe = job.type().executableName()
142    
143 nsmirnov 1.2 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 gutsche 1.9 job = common.job_list[self.nj]
151     args = job.type().executableArgs()
152 farinafa 1.34
153     txt = ''
154     txt += 'CRAB_EXE_CPU_TIME=-1 \n'
155     # NO carriage return for this line #Fabio
156 farinafa 1.35 txt += '/usr/bin/time -f \"%U %S %P\" -o cpu_timing.txt '
157 farinafa 1.34 txt += '$executable '+args+'\n'
158     return txt
159 nsmirnov 1.1
160 nsmirnov 1.3 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 fanzago 1.5
169 fanzago 1.8 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 fanzago 1.5 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 spiga 1.30 def parseReport_(self):
184     """
185     Returns part of a job script which parse the FrameworkJobReport.
186     """
187     jbt = common.job_list.type()
188 spiga 1.31 txt = jbt.wsParseFJR()
189 spiga 1.30 return txt
190    
191 fanzago 1.11 def modifyReport_(self):
192     """
193     Returns part of a job script which modifies the FrameworkJobReport.
194     """
195     jbt = common.job_list.type()
196 spiga 1.36 txt = jbt.wsModifyReport(self.nj)
197 fanzago 1.11 return txt
198 fanzago 1.12
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 mcinquil 1.14
207 spiga 1.27 def func_exit_(self):
208 mcinquil 1.14 """
209 ewv 1.32 Returns part of a job script which does scheduler-specific
210 spiga 1.27 output checks and management.
211 mcinquil 1.14 """
212 spiga 1.27 txt = common.scheduler.wsExitFunc()
213 mcinquil 1.14 return txt
214 spiga 1.22
215 ewv 1.21 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 ewv 1.26 if (self.CMSSW_major >= 2 and self.CMSSW_minor >= 1) or self.CMSSW_major > 2: # py in, py out for 2_1_x
223 spiga 1.33 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 ewv 1.26 elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
226 spiga 1.33 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 ewv 1.26 else: # cfg in, cfg out for 1_x_y
229 spiga 1.33 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 ewv 1.21
232     return txt