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

◆ __init__()

def batchmanager.BatchManager.__init__ (   self)

Definition at line 26 of file batchmanager.py.

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

References batchmanager.BatchManager.DefineOptions().

Member Function Documentation

◆ CheckBatchScript()

def batchmanager.BatchManager.CheckBatchScript (   self,
  batchScript 
)

Definition at line 206 of file batchmanager.py.

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 

References TritonClient.options_, and print().

◆ DefineOptions()

def batchmanager.BatchManager.DefineOptions (   self)

Definition at line 30 of file batchmanager.py.

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 

Referenced by batchmanager.BatchManager.__init__().

◆ ManageOutputDir()

def batchmanager.BatchManager.ManageOutputDir (   self)

Definition at line 128 of file batchmanager.py.

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 

References TritonClient.options_, and print().

◆ mkdir()

def batchmanager.BatchManager.mkdir (   self,
  dirname 
)

Definition at line 236 of file batchmanager.py.

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 

References print().

Referenced by batchmanager.BatchManager.PrepareJob().

◆ ParseOptions()

def batchmanager.BatchManager.ParseOptions (   self)

Definition at line 57 of file batchmanager.py.

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:')
86  print(self.remoteOutputDir_)
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 
107  self.remoteOutputFile_ = ""
108  self.ManageOutputDir()
109  return (self.options_, self.args_)
110 
111 

References reco::parser::MethodInvoker.args_, reco::parser::ExpressionQuaterOperator< Op >.args_, LazyConstructed< WrappedClass, Args >.args_, LazyResult< Func, Args >.args_, ExternalLHEProducer.args_, cmdline::CmdLine.args_, Json::Path.args_, TritonClient.options_, main.parse_args(), batchmanager.BatchManager.parser_, MonitorElementsDb.parser_, l1t::XmlConfigParser.parser_, DCCTBBlockPrototype.parser_, DCCTBDataMapper.parser_, and MonitorXMLParser.parser_.

◆ PrepareJob()

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.

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 

References mps_setup.append, join(), batchmanager.BatchManager.listOfJobs_, TFileDirectory.mkdir(), TFileService.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 print().

◆ PrepareJobs()

def batchmanager.BatchManager.PrepareJobs (   self,
  listOfValues,
  listOfDirNames = None 
)

Definition at line 112 of file batchmanager.py.

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 

References print().

◆ PrepareJobUser()

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.

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 

References print().

Referenced by batchmanager.BatchManager.PrepareJob().

◆ RunningMode()

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.

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 )

References print().

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

◆ SubmitJob()

def batchmanager.BatchManager.SubmitJob (   self,
  jobDir 
)
Hook for job submission.

Definition at line 196 of file batchmanager.py.

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 

References TritonClient.options_, and print().

Referenced by batchmanager.BatchManager.SubmitJobs().

◆ SubmitJobArray()

def batchmanager.BatchManager.SubmitJobArray (   self,
  numbOfJobs = 1 
)
Hook for array job submission.

Definition at line 202 of file batchmanager.py.

202  def SubmitJobArray( self, numbOfJobs = 1 ):
203  '''Hook for array job submission.'''
204  print('Submitting array with %s jobs' % numbOfJobs)
205 

References print().

◆ SubmitJobs()

def batchmanager.BatchManager.SubmitJobs (   self,
  waitingTimeInSec = 0 
)
Submit all jobs. Possibly wait between each job

Definition at line 177 of file batchmanager.py.

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 

References batchmanager.BatchManager.listOfJobs_, TritonClient.options_, print(), and batchmanager.BatchManager.SubmitJob().

Member Data Documentation

◆ listOfJobs_

batchmanager.BatchManager.listOfJobs_

◆ outputDir_

batchmanager.BatchManager.outputDir_

◆ parser_

batchmanager.BatchManager.parser_

◆ remoteOutputDir_

batchmanager.BatchManager.remoteOutputDir_

◆ remoteOutputFile_

batchmanager.BatchManager.remoteOutputFile_

Definition at line 107 of file batchmanager.py.

join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
eostools.mkdir
def mkdir(path)
Definition: eostools.py:251
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
mps_setup.append
append
Definition: mps_setup.py:85
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
format