CMS 3D CMS Logo

batchmanager.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from __future__ import absolute_import
5 from datetime import datetime
6 from optparse import OptionParser
7 
8 import sys
9 import os
10 import re
11 import pprint
12 import time
13 
14 from . import eostools as castortools
15 
17  """
18  This class manages batch jobs
19  Used in batch scripts
20  Colin Bernet 2008
21  """
22 
23  # constructor
24  # self is this
25  # parse batch manager options
26  def __init__(self):
27  self.DefineOptions()
28 
29 
30  def DefineOptions(self):
31  # define options and arguments ====================================
32  # how to add more doc to the help?
33  self.parser_ = OptionParser()
34  self.parser_.add_option("-o", "--output-dir", dest="outputDir",
35  help="Name of the local output directory for your jobs. This directory will be created automatically.",
36  default=None)
37  self.parser_.add_option("-r", "--remote-copy", dest="remoteCopy",
38  help="remote output directory for your jobs. Example: /store/cmst3/user/cbern/CMG/HT/Run2011A-PromptReco-v1/AOD/PAT_CMG/RA2. This directory *must* be provided as a logical file name (LFN). When this option is used, all root files produced by a job are copied to the remote directory, and the job index is appended to the root file name. The Logger directory will be sent back to the submision directory. For remote copy to PSI specify path like: '/pnfs/psi.ch/...'. Note: enviromental variable X509_USER_PROXY must point to home area before renewing proxy",
39  default=None)
40  self.parser_.add_option("-f", "--force", action="store_true",
41  dest="force", default=False,
42  help="Don't ask any questions, just over-write")
43  # this opt can be removed
44  self.parser_.add_option("-n", "--negate", action="store_true",
45  dest="negate", default=False,
46  help="create jobs, but does not submit the jobs.")
47  self.parser_.add_option("-b", "--batch", dest="batch",
48  help="batch command. default is: 'bsub -q 8nh < batchScript.sh'. You can also use 'nohup < ./batchScript.sh &' to run locally.",
49  default="bsub -q 8nh < ./batchScript.sh")
50  self.parser_.add_option( "--option",
51  dest="extraOptions",
52  type="string",
53  action="append",
54  default=[],
55  help="Save one extra option (either a flag, or a key=value pair) that can be then accessed from the job config file")
56 
57  def ParseOptions(self):
58  (self.options_,self.args_) = self.parser_.parse_args()
59  if self.options_.remoteCopy == None:
60  self.remoteOutputDir_ = ""
61  else:
62  # removing possible trailing slash
63  self.remoteOutputDir_ = self.options_.remoteCopy.rstrip('/')
64  if "psi.ch" in self.remoteOutputDir_: # T3 @ PSI:
65  # overwriting protection to be improved
66  if self.remoteOutputDir_.startswith("/pnfs/psi.ch"):
67  ld_lib_path = os.environ.get('LD_LIBRARY_PATH')
68  if ld_lib_path != "None":
69  os.environ['LD_LIBRARY_PATH'] = "/usr/lib64/:"+ld_lib_path # to solve gfal conflict with CMSSW
70  os.system("gfal-mkdir srm://t3se01.psi.ch/"+self.remoteOutputDir_)
71  outputDir = self.options_.outputDir
72  if outputDir==None:
73  today = datetime.today()
74  outputDir = 'OutCmsBatch_%s' % today.strftime("%d%h%y_%H%M")
75  self.remoteOutputDir_+="/"+outputDir
76  os.system("gfal-mkdir srm://t3se01.psi.ch/"+self.remoteOutputDir_)
77  if ld_lib_path != "None":
78  os.environ['LD_LIBRARY_PATH'] = ld_lib_path # back to original to avoid conflicts
79  else:
80  print("remote directory must start with /pnfs/psi.ch to send to the tier3 at PSI")
81  print(self.remoteOutputDir_, "not valid")
82  sys.exit(1)
83  else: # assume EOS
84  if not castortools.isLFN( self.remoteOutputDir_ ):
85  print('When providing an output directory, you must give its LFN, starting by /store. You gave:')
87  sys.exit(1)
88  self.remoteOutputDir_ = castortools.lfnToEOS( self.remoteOutputDir_ )
89  dirExist = castortools.isDirectory( self.remoteOutputDir_ )
90  # nsls = 'nsls %s > /dev/null' % self.remoteOutputDir_
91  # dirExist = os.system( nsls )
92  if dirExist is False:
93  print('creating ', self.remoteOutputDir_)
94  if castortools.isEOSFile( self.remoteOutputDir_ ):
95  # the output directory is currently a file..
96  # need to remove it.
97  castortools.rm( self.remoteOutputDir_ )
98  castortools.createEOSDir( self.remoteOutputDir_ )
99  else:
100  # directory exists.
101  if self.options_.negate is False and self.options_.force is False:
102  #COLIN need to reimplement protectedRemove in eostools
103  raise ValueError( ' '.join(['directory ', self.remoteOutputDir_, ' already exists.']))
104  # if not castortools.protectedRemove( self.remoteOutputDir_, '.*root'):
105  # the user does not want to delete the root files
106 
108  self.ManageOutputDir()
109  return (self.options_, self.args_)
110 
111 
112  def PrepareJobs(self, listOfValues, listOfDirNames=None):
113  print('PREPARING JOBS ======== ')
114  self.listOfJobs_ = []
115 
116  if listOfDirNames is None:
117  for value in listOfValues:
118  self.PrepareJob( value )
119  else:
120  for value, name in zip( listOfValues, listOfDirNames):
121  self.PrepareJob( value, name )
122  print("list of jobs:")
123  pp = pprint.PrettyPrinter(indent=4)
124  pp.pprint( self.listOfJobs_)
125 
126 
127  # create output dir, if necessary
128  def ManageOutputDir( self ):
129 
130  #if the output dir is not specified, generate a name
131  #else
132  #test if the directory exists
133  #if yes, returns
134 
135  outputDir = self.options_.outputDir
136 
137  if outputDir==None:
138  today = datetime.today()
139  outputDir = 'OutCmsBatch_%s' % today.strftime("%d%h%y_%H%M%S")
140  print('output directory not specified, using %s' % outputDir)
141 
142  self.outputDir_ = os.path.abspath(outputDir)
143 
144  if( os.path.isdir(self.outputDir_) == True ):
145  input = ''
146  if not self.options_.force:
147  while input != 'y' and input != 'n':
148  input = raw_input( 'The directory ' + self.outputDir_ + ' exists. Are you sure you want to continue? its contents will be overwritten [y/n] ' )
149  if input == 'n':
150  sys.exit(1)
151  else:
152  os.system( 'rm -rf ' + self.outputDir_)
153 
154  self.mkdir( self.outputDir_ )
155 
156 
157  def PrepareJob( self, value, dirname=None):
158  '''Prepare a job for a given value.
159 
160  calls PrepareJobUser, which should be overloaded by the user.
161  '''
162  print('PrepareJob : %s' % value)
163  dname = dirname
164  if dname is None:
165  dname = 'Job_{value}'.format( value=value )
166  jobDir = '/'.join( [self.outputDir_, dname])
167  print('\t',jobDir)
168  self.mkdir( jobDir )
169  self.listOfJobs_.append( jobDir )
170  self.PrepareJobUser( jobDir, value )
171 
172  def PrepareJobUser(self, value ):
173  '''Hook allowing user to define how one of his jobs should be prepared.'''
174  print('\to be customized')
175 
176 
177  def SubmitJobs( self, waitingTimeInSec=0 ):
178  '''Submit all jobs. Possibly wait between each job'''
179 
180  if(self.options_.negate):
181  print('*NOT* SUBMITTING JOBS - exit ')
182  return
183  print('SUBMITTING JOBS ======== ')
184  for jobDir in self.listOfJobs_:
185  root = os.getcwd()
186  # run it
187  print('processing ', jobDir)
188  os.chdir( jobDir )
189  self.SubmitJob( jobDir )
190  # and come back
191  os.chdir(root)
192  print('waiting %s seconds...' % waitingTimeInSec)
193  time.sleep( waitingTimeInSec )
194  print('done.')
195 
196  def SubmitJob( self, jobDir ):
197  '''Hook for job submission.'''
198  print('submitting (to be customized): ', jobDir)
199  os.system( self.options_.batch )
200 
201 
202  def SubmitJobArray( self, numbOfJobs = 1 ):
203  '''Hook for array job submission.'''
204  print('Submitting array with %s jobs' % numbOfJobs)
205 
206  def CheckBatchScript( self, batchScript ):
207 
208  if batchScript == '':
209  return
210 
211  if( os.path.isfile(batchScript)== False ):
212  print('file ',batchScript,' does not exist')
213  sys.exit(3)
214 
215  try:
216  ifile = open(batchScript)
217  except:
218  print('cannot open input %s' % batchScript)
219  sys.exit(3)
220  else:
221  for line in ifile:
222  p = re.compile("\s*cp.*\$jobdir\s+(\S+)$");
223  m=p.match(line)
224  if m:
225  if os.path.isdir( os.path.expandvars(m.group(1)) ):
226  print('output directory ', m.group(1), 'already exists!')
227  print('exiting')
228  sys.exit(2)
229  else:
230  if self.options_.negate==False:
231  os.mkdir( os.path.expandvars(m.group(1)) )
232  else:
233  print('not making dir', self.options_.negate)
234 
235  # create a directory
236  def mkdir( self, dirname ):
237  # there is probably a command for this in python
238  mkdir = 'mkdir -p %s' % dirname
239  ret = os.system( mkdir )
240  if( ret != 0 ):
241  print('please remove or rename directory: ', dirname)
242  sys.exit(4)
243 
244 
245  def RunningMode(self, batch):
246 
247  '''Return "LXPUS", "PSI", "NAF", "LOCAL", or None,
248 
249  "LXPLUS" : batch command is bsub, and logged on lxplus
250  "PSI" : batch command is qsub, and logged to t3uiXX
251  "NAF" : batch command is qsub, and logged on naf
252  "IC" : batch command is qsub, and logged on hep.ph.ic.ac.uk
253  "LOCAL" : batch command is nohup.
254 
255  In all other cases, a CmsBatchException is raised
256  '''
257 
258  hostName = os.environ['HOSTNAME']
259 
260  onLxplus = hostName.startswith('lxplus')
261  onPSI = hostName.startswith('t3ui')
262  onNAF = hostName.startswith('naf')
263 
264  batchCmd = batch.split()[0]
265 
266  if batchCmd == 'bsub':
267  if not onLxplus:
268  err = 'Cannot run %s on %s' % (batchCmd, hostName)
269  raise ValueError( err )
270  else:
271  print('running on LSF : %s from %s' % (batchCmd, hostName))
272  return 'LXPLUS'
273 
274  elif batchCmd == "qsub":
275  if onPSI:
276  print('running on SGE : %s from %s' % (batchCmd, hostName))
277  return 'PSI'
278  elif onNAF:
279  print('running on NAF : %s from %s' % (batchCmd, hostName))
280  return 'NAF'
281  elif onIC:
282  print('running on IC : %s from %s' % (batchCmd, hostName))
283  return 'IC'
284  else:
285  err = 'Cannot run %s on %s' % (batchCmd, hostName)
286  raise ValueError( err )
287 
288  elif batchCmd == 'nohup' or batchCmd == './batchScript.sh':
289  print('running locally : %s on %s' % (batchCmd, hostName))
290  return 'LOCAL'
291  else:
292  err = 'unknown batch command: X%sX' % batchCmd
293  raise ValueError( err )
batchmanager.BatchManager.PrepareJobUser
def PrepareJobUser(self, value)
Definition: batchmanager.py:172
batchmanager.BatchManager.ManageOutputDir
def ManageOutputDir(self)
Definition: batchmanager.py:128
batchmanager.BatchManager.mkdir
def mkdir(self, dirname)
Definition: batchmanager.py:236
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
batchmanager.BatchManager
Definition: batchmanager.py:16
batchmanager.BatchManager.PrepareJob
def PrepareJob(self, value, dirname=None)
Definition: batchmanager.py:157
batchmanager.BatchManager.DefineOptions
def DefineOptions(self)
Definition: batchmanager.py:30
batchmanager.BatchManager.outputDir_
outputDir_
Definition: batchmanager.py:142
batchmanager.BatchManager.SubmitJobArray
def SubmitJobArray(self, numbOfJobs=1)
Definition: batchmanager.py:202
batchmanager.BatchManager.listOfJobs_
listOfJobs_
Definition: batchmanager.py:114
batchmanager.BatchManager.PrepareJobs
def PrepareJobs(self, listOfValues, listOfDirNames=None)
Definition: batchmanager.py:112
batchmanager.BatchManager.SubmitJobs
def SubmitJobs(self, waitingTimeInSec=0)
Definition: batchmanager.py:177
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
batchmanager.BatchManager.SubmitJob
def SubmitJob(self, jobDir)
Definition: batchmanager.py:196
mps_setup.append
append
Definition: mps_setup.py:85
batchmanager.BatchManager.parser_
parser_
Definition: batchmanager.py:33
main.parse_args
def parse_args(args)
Definition: main.py:63
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
batchmanager.BatchManager.remoteOutputFile_
remoteOutputFile_
Definition: batchmanager.py:107
format
batchmanager.BatchManager.RunningMode
def RunningMode(self, batch)
Definition: batchmanager.py:245
batchmanager.BatchManager.__init__
def __init__(self)
Definition: batchmanager.py:26
batchmanager.BatchManager.CheckBatchScript
def CheckBatchScript(self, batchScript)
Definition: batchmanager.py:206
batchmanager.BatchManager.remoteOutputDir_
remoteOutputDir_
Definition: batchmanager.py:60
batchmanager.BatchManager.ParseOptions
def ParseOptions(self)
Definition: batchmanager.py:57