ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.40
Committed: Thu Apr 16 19:15:05 2009 UTC (16 years ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.39: +1 -1 lines
Log Message:
changes for cpu  timing information reporting to the DashBoard. Now distinguish between: User/System/CPU percentage

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