CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
batchmanager.BatchManager Class Reference

Public Member Functions

def __init__ (self)
 
def CheckBatchScript (self, batchScript)
 
def DefineOptions (self)
 
def ManageOutputDir (self)
 
def mkdir (self, dirname)
 
def ParseOptions (self)
 
def PrepareJob (self, value, dirname=None)
 
def PrepareJobs (self, listOfValues, listOfDirNames=None)
 
def PrepareJobUser (self, value)
 
def RunningMode (self, batch)
 
def SubmitJob (self, jobDir)
 
def SubmitJobArray (self, numbOfJobs=1)
 
def SubmitJobs (self, waitingTimeInSec=0)
 

Public Attributes

 listOfJobs_
 
 outputDir_
 
 parser_
 
 remoteOutputDir_
 
 remoteOutputFile_
 

Detailed Description

This class manages batch jobs
Used in batch scripts
Colin Bernet 2008

Definition at line 16 of file batchmanager.py.

Constructor & Destructor Documentation

def batchmanager.BatchManager.__init__ (   self)

Definition at line 26 of file batchmanager.py.

References batchmanager.BatchManager.DefineOptions().

26  def __init__(self):
27  self.DefineOptions()
28 
29 

Member Function Documentation

def batchmanager.BatchManager.CheckBatchScript (   self,
  batchScript 
)

Definition at line 206 of file batchmanager.py.

References edm.print().

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 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def CheckBatchScript(self, batchScript)
def batchmanager.BatchManager.DefineOptions (   self)

Definition at line 30 of file batchmanager.py.

Referenced by batchmanager.BatchManager.__init__().

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 
def batchmanager.BatchManager.ManageOutputDir (   self)

Definition at line 128 of file batchmanager.py.

References edm.print().

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 
def mkdir(self, dirname)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def batchmanager.BatchManager.mkdir (   self,
  dirname 
)

Definition at line 236 of file batchmanager.py.

References edm.print().

Referenced by batchmanager.BatchManager.PrepareJob().

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 
def mkdir(self, dirname)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def batchmanager.BatchManager.ParseOptions (   self)

Definition at line 57 of file batchmanager.py.

References reco::parser::ExpressionQuaterOperator< Op >.args_, reco::parser::MethodInvoker.args_, ExternalLHEProducer.args_, cmdline::CmdLine.args_, Json::Path.args_, tensorflow::TBBSession.options_, and tensorflow::NTSession.options_.

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 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def batchmanager.BatchManager.PrepareJob (   self,
  value,
  dirname = None 
)
Prepare a job for a given value.

calls PrepareJobUser, which should be overloaded by the user.

Definition at line 157 of file batchmanager.py.

References join(), TFileService.mkdir(), TFileDirectory.mkdir(), batchHippy.MyBatchManager.mkdir(), batchmanager.BatchManager.mkdir(), dqmoffline::l1t::L1TDiffHarvesting::L1TDiffPlotHandler.outputDir_, MillePedeFileExtractor.outputDir_, L1GtVhdlWriter.outputDir_, dqmoffline::l1t::L1TEfficiencyPlotHandler.outputDir_, batchmanager.BatchManager.outputDir_, L1GtVhdlWriterCore.outputDir_, LaserSorter.outputDir_, cmsBatch.MyBatchManager.PrepareJobUser(), batchmanager.BatchManager.PrepareJobUser(), and edm.print().

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 
def mkdir(self, dirname)
def PrepareJob(self, value, dirname=None)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def PrepareJobUser(self, value)
def batchmanager.BatchManager.PrepareJobs (   self,
  listOfValues,
  listOfDirNames = None 
)

Definition at line 112 of file batchmanager.py.

References edm.print().

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 
def PrepareJob(self, value, dirname=None)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def PrepareJobs(self, listOfValues, listOfDirNames=None)
def batchmanager.BatchManager.PrepareJobUser (   self,
  value 
)
Hook allowing user to define how one of his jobs should be prepared.

Definition at line 172 of file batchmanager.py.

References edm.print().

Referenced by batchmanager.BatchManager.PrepareJob().

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 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def PrepareJobUser(self, value)
def batchmanager.BatchManager.RunningMode (   self,
  batch 
)
Return "LXPUS", "PSI", "NAF", "LOCAL", or None,

"LXPLUS" : batch command is bsub, and logged on lxplus
"PSI"    : batch command is qsub, and logged to t3uiXX
"NAF"    : batch command is qsub, and logged on naf
"IC"     : batch command is qsub, and logged on hep.ph.ic.ac.uk
"LOCAL"  : batch command is nohup.

In all other cases, a CmsBatchException is raised

Definition at line 245 of file batchmanager.py.

References edm.print().

Referenced by cmsBatch.MyBatchManager.PrepareJobUser(), and heppy_batch.MyBatchManager.PrepareJobUser().

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 )
294 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def RunningMode(self, batch)
def batchmanager.BatchManager.SubmitJob (   self,
  jobDir 
)
Hook for job submission.

Definition at line 196 of file batchmanager.py.

References edm.print().

Referenced by batchmanager.BatchManager.SubmitJobs().

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 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def SubmitJob(self, jobDir)
def batchmanager.BatchManager.SubmitJobArray (   self,
  numbOfJobs = 1 
)
Hook for array job submission.

Definition at line 202 of file batchmanager.py.

References edm.print().

202  def SubmitJobArray( self, numbOfJobs = 1 ):
203  '''Hook for array job submission.'''
204  print('Submitting array with %s jobs' % numbOfJobs)
205 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def SubmitJobArray(self, numbOfJobs=1)
def batchmanager.BatchManager.SubmitJobs (   self,
  waitingTimeInSec = 0 
)
Submit all jobs. Possibly wait between each job

Definition at line 177 of file batchmanager.py.

References batchmanager.BatchManager.listOfJobs_, edm.print(), and batchmanager.BatchManager.SubmitJob().

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 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def SubmitJobs(self, waitingTimeInSec=0)
def SubmitJob(self, jobDir)

Member Data Documentation

batchmanager.BatchManager.listOfJobs_

Definition at line 114 of file batchmanager.py.

Referenced by batchmanager.BatchManager.SubmitJobs().

batchmanager.BatchManager.outputDir_
batchmanager.BatchManager.parser_

Definition at line 33 of file batchmanager.py.

batchmanager.BatchManager.remoteOutputDir_

Definition at line 60 of file batchmanager.py.

batchmanager.BatchManager.remoteOutputFile_

Definition at line 107 of file batchmanager.py.