ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/ScriptWriter.py
Revision: 1.28
Committed: Tue May 27 13:22:53 2008 UTC (16 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.27: +10 -7 lines
Log Message:
Printing CMSSW Pset in STDOUT is now optional. Specify USER.pset_debug=True to enable the print. Updated related documentation

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.27 def __init__(self, template):
12 nsmirnov 1.1 # pattern -> action
13 fanzago 1.11 ### FEDE added modify_report FOR DBS OUTPUT PUBLICATION
14 nsmirnov 1.1 self.actions = {
15 nsmirnov 1.3 'title' : self.title_,
16 fanzago 1.23 'untar_software' : self.untarSoftware_,
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 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 fanzago 1.11 def modifyReport_(self):
170     """
171     Returns part of a job script which modifies the FrameworkJobReport.
172     """
173     jbt = common.job_list.type()
174     txt = jbt.modifyReport(self.nj)
175     return txt
176 fanzago 1.12
177     def cleanEnv_(self):
178     """
179     In OSG environment this function removes the WORKING_DIR
180     """
181     jbt = common.job_list.type()
182     txt = jbt.cleanEnv()
183     return txt
184 mcinquil 1.14
185 spiga 1.27 def func_exit_(self):
186 mcinquil 1.14 """
187 spiga 1.27 Returns part of a job script which does scheduler-specific
188     output checks and management.
189 mcinquil 1.14 """
190 spiga 1.27 txt = common.scheduler.wsExitFunc()
191 mcinquil 1.14 return txt
192 spiga 1.22
193 ewv 1.21 def rewriteCMSSWcfg_(self):
194     """
195     Returns part of the script that runs writeCfg.py on the WN
196     """
197     # FUTURE: This function tests the CMSSW version. Can be simplified as we drop support for old versions
198     txt = "# Rewrite cfg for this job\n"
199    
200 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
201 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.py pset.py\n"
202     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.py pset.py\n"
203 ewv 1.26 elif self.CMSSW_major >= 2: # cfg in, py out for 2_0_x
204 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.py\n"
205     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.py\n"
206 ewv 1.26 else: # cfg in, cfg out for 1_x_y
207 spiga 1.28 txt += "echo $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.cfg\n"
208     txt += "python $RUNTIME_AREA/writeCfg.py "+str(self.debug_pset)+" pset.cfg pset.cfg\n"
209 ewv 1.21
210     return txt