ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.31
Committed: Tue May 27 14:36:11 2008 UTC (16 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: PRODCOMMON_0_10_7_testCS2
Changes since 1.30: +1 -1 lines
Log Message:
wrong argument

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 spiga 1.29 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 nsmirnov 1.3 'setup_scheduler_environment' : self.setupSchedulerEnvironment_,
17     'setup_jobtype_environment' : self.setupJobTypeEnvironment_,
18 fanzago 1.8 'copy_input' : self.copyInput_,
19 ewv 1.21 'rewrite_cmssw_cfg' : self.rewriteCMSSWcfg_,
20 nsmirnov 1.3 'build_executable' : self.buildExe_,
21     'run_executable' : self.runExe_,
22     'rename_output' : self.renameOutput_,
23 fanzago 1.5 'copy_output' : self.copyOutput_,
24 spiga 1.30 'parse_report' : self.parseReport_,
25 fanzago 1.12 'modify_report' : self.modifyReport_,
26 spiga 1.27 'func_exit' : self.func_exit_
27 nsmirnov 1.1 }
28 ewv 1.21
29 slacapra 1.6 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 nsmirnov 1.2 self.nj = -1 # current job number
36 mcinquil 1.14
37 ewv 1.21 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 spiga 1.28 self.debug_pset=''
47     debug = cfg_params.get('USER.debug_pset',False)
48     if debug: self.debug_pset='--debug'
49    
50 nsmirnov 1.1 return
51    
52     def setAction(self, pattern, action):
53     self.actions[pattern] = action
54     return
55 ewv 1.21
56 slacapra 1.10 def modifyTemplateScript(self):
57 nsmirnov 1.1 """
58     Create a script from scratch.
59     """
60 ewv 1.21
61 nsmirnov 1.1 tpl = open(self.template, 'r')
62 spiga 1.22 script = open(common._db.queryTask('scriptName'),'w')
63 nsmirnov 1.1
64     for line in tpl:
65     if len(line) > 6 and line[:6] == '#CRAB ':
66     act_str = string.strip(line[6:])
67     try:
68     action = self.actions[act_str]
69     except KeyError:
70     continue
71    
72     if action:
73     txt = action()
74     script.write(txt)
75     pass
76     else:
77     script.write(line)
78     pass
79     else:
80     script.write(line)
81     pass
82     pass
83    
84     script.close()
85     tpl.close()
86     return
87    
88 nsmirnov 1.2 def title_(self):
89 nsmirnov 1.1 txt = '# This script was generated by '+common.prog_name
90     txt += ' (version '+common.prog_version_str+').\n'
91     return txt
92 ewv 1.26
93 fanzago 1.23
94     ### FEDE ###
95 ewv 1.26
96 fanzago 1.23 def untarSoftware_(self):
97     """
98     Returns part of a job script which untar CMSSW software.
99     """
100     jbt = common.job_list.type()
101    
102     txt = jbt.wsUntarSoftware(self.nj)
103    
104     #txt += 'executable='+exe+'\n'
105     return txt
106 ewv 1.26
107 fanzago 1.23 ###########################################
108 ewv 1.21
109 nsmirnov 1.3 def setupSchedulerEnvironment_(self):
110     """
111     Returns part of a job script which does scheduler-specific work.
112     """
113     txt = common.scheduler.wsSetupEnvironment()
114     return txt
115 nsmirnov 1.1
116 nsmirnov 1.3 def setupJobTypeEnvironment_(self):
117 nsmirnov 1.1 """
118 fanzago 1.7 Returns part of a job script which does jobtype-specific work.
119 nsmirnov 1.1 """
120 nsmirnov 1.3 jbt = common.job_list.type()
121     txt = jbt.wsSetupEnvironment(self.nj)
122     return txt
123 ewv 1.21
124 nsmirnov 1.2 def buildExe_(self):
125 nsmirnov 1.1 """
126 nsmirnov 1.2 Returns part of a job script which builds the binary executable.
127 nsmirnov 1.1 """
128 nsmirnov 1.2 jbt = common.job_list.type()
129 nsmirnov 1.1
130 nsmirnov 1.3 txt = jbt.wsBuildExe(self.nj)
131 nsmirnov 1.1
132 nsmirnov 1.2 job = common.job_list[self.nj]
133 nsmirnov 1.1 exe = job.type().executableName()
134    
135 nsmirnov 1.2 txt += 'executable='+exe+'\n'
136     return txt
137    
138     def runExe_(self):
139     """
140     Returns part of a job script which executes the application.
141     """
142 gutsche 1.9 job = common.job_list[self.nj]
143     args = job.type().executableArgs()
144     return '$executable '+args+'\n'
145 nsmirnov 1.1
146 nsmirnov 1.3 def renameOutput_(self):
147     """
148     Returns part of a job script which renames output files.
149     """
150     jbt = common.job_list.type()
151     txt = '\n'
152     txt += jbt.wsRenameOutput(self.nj)
153     return txt
154 fanzago 1.5
155 fanzago 1.8 def copyInput_(self):
156     """
157     Returns part of a job script which copies input files from SE.
158     """
159     txt = common.scheduler.wsCopyInput()
160     return txt
161    
162 fanzago 1.5 def copyOutput_(self):
163     """
164     Returns part of a job script which copies output files to SE.
165     """
166     txt = common.scheduler.wsCopyOutput()
167     return txt
168    
169 spiga 1.30 def parseReport_(self):
170     """
171     Returns part of a job script which parse the FrameworkJobReport.
172     """
173     jbt = common.job_list.type()
174 spiga 1.31 txt = jbt.wsParseFJR()
175 spiga 1.30 return txt
176    
177 fanzago 1.11 def modifyReport_(self):
178     """
179     Returns part of a job script which modifies the FrameworkJobReport.
180     """
181     jbt = common.job_list.type()
182     txt = jbt.modifyReport(self.nj)
183     return txt
184 fanzago 1.12
185     def cleanEnv_(self):
186     """
187     In OSG environment this function removes the WORKING_DIR
188     """
189     jbt = common.job_list.type()
190     txt = jbt.cleanEnv()
191     return txt
192 mcinquil 1.14
193 spiga 1.27 def func_exit_(self):
194 mcinquil 1.14 """
195 spiga 1.27 Returns part of a job script which does scheduler-specific
196     output checks and management.
197 mcinquil 1.14 """
198 spiga 1.27 txt = common.scheduler.wsExitFunc()
199 mcinquil 1.14 return txt
200 spiga 1.22
201 ewv 1.21 def rewriteCMSSWcfg_(self):
202     """
203     Returns part of the script that runs writeCfg.py on the WN
204     """
205     # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
206     txt = "# Rewrite cfg for this job\n"
207    
208 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
209 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.py pset.py\n"
210     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.py pset.py\n"
211 ewv 1.26 elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
212 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.py\n"
213     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.py\n"
214 ewv 1.26 else: # cfg in, cfg out for 1_x_y
215 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.cfg\n"
216     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.cfg\n"
217 ewv 1.21
218     return txt