ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.33
Committed: Thu May 29 21:00:32 2008 UTC (16 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_2_2_pre5, CRAB_2_2_2_pre4, CRAB_2_2_2_pre3, CRAB_2_2_2_pre2, CRAB_2_2_2_pre1, CRAB_2_2_1, CRAB_2_2_1_pre6, CRAB_2_2_1_pre5, CRAB_2_2_1_pre4
Changes since 1.32: +10 -10 lines
Log Message:
moved from debuggin_pset to more general debugging_wrapper. Plus major changes on parse framework job report script. Now it take arguments and return different output according to the requests.

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     return '$executable '+args+'\n'
153 nsmirnov 1.1
154 nsmirnov 1.3 def renameOutput_(self):
155     """
156     Returns part of a job script which renames output files.
157     """
158     jbt = common.job_list.type()
159     txt = '\n'
160     txt += jbt.wsRenameOutput(self.nj)
161     return txt
162 fanzago 1.5
163 fanzago 1.8 def copyInput_(self):
164     """
165     Returns part of a job script which copies input files from SE.
166     """
167     txt = common.scheduler.wsCopyInput()
168     return txt
169    
170 fanzago 1.5 def copyOutput_(self):
171     """
172     Returns part of a job script which copies output files to SE.
173     """
174     txt = common.scheduler.wsCopyOutput()
175     return txt
176    
177 spiga 1.30 def parseReport_(self):
178     """
179     Returns part of a job script which parse the FrameworkJobReport.
180     """
181     jbt = common.job_list.type()
182 spiga 1.31 txt = jbt.wsParseFJR()
183 spiga 1.30 return txt
184    
185 fanzago 1.11 def modifyReport_(self):
186     """
187     Returns part of a job script which modifies the FrameworkJobReport.
188     """
189     jbt = common.job_list.type()
190     txt = jbt.modifyReport(self.nj)
191     return txt
192 fanzago 1.12
193     def cleanEnv_(self):
194     """
195     In OSG environment this function removes the WORKING_DIR
196     """
197     jbt = common.job_list.type()
198     txt = jbt.cleanEnv()
199     return txt
200 mcinquil 1.14
201 spiga 1.27 def func_exit_(self):
202 mcinquil 1.14 """
203 ewv 1.32 Returns part of a job script which does scheduler-specific
204 spiga 1.27 output checks and management.
205 mcinquil 1.14 """
206 spiga 1.27 txt = common.scheduler.wsExitFunc()
207 mcinquil 1.14 return txt
208 spiga 1.22
209 ewv 1.21 def rewriteCMSSWcfg_(self):
210     """
211     Returns part of the script that runs writeCfg.py on the WN
212     """
213     # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
214     txt = "# Rewrite cfg for this job\n"
215    
216 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
217 spiga 1.33 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
218     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.py pset.py\n"
219 ewv 1.26 elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
220 spiga 1.33 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
221     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.py\n"
222 ewv 1.26 else: # cfg in, cfg out for 1_x_y
223 spiga 1.33 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
224     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_wrapper)+" pset.cfg pset.cfg\n"
225 ewv 1.21
226     return txt