CMS 3D CMS Logo

batchHippy.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 import sys
4 import imp
5 import copy
6 import os
7 import shutil
8 import pickle
9 import math
10 import pprint
11 import subprocess
12 from datetime import date
13 from optparse import OptionParser
14 
15 
16 
18  '''Batch manager specific to cmsRun processes.'''
19 
20  def __init__(self):
21  # define options and arguments ====================================
22  self.parser = OptionParser()
23  self.parser.add_option("-o", "--outdir", dest="outputdir", type="string",
24  help="Name of the local output directory for your jobs. This directory will be created automatically.",
25  default="./")
26  self.parser.add_option("--commoncfg", dest="commoncfg", type="string",
27  help="Name of the common config file.",
28  default="python/common_cff_py.txt")
29  self.parser.add_option("--aligncfg", dest="aligncfg", type="string",
30  help="Name of the align. config file.",
31  default="python/align_tpl_py.txt")
32  self.parser.add_option("--niter", dest="niter", type="int",
33  help="Number of iterations",
34  default="15")
35  self.parser.add_option("--lst", "--listfile", "--lstfile", dest="lstfile", type="string",
36  help="lst file to read",
37  default=None)
38  self.parser.add_option("--iovs", "--iovfile", dest="iovfile", type="string",
39  help="IOV list to read",
40  default=None)
41  self.parser.add_option("--trkselcfg", "--trackselectionconfig", dest="trkselcfg", type="string",
42  help="Track selection config location",
43  default="python")
44  self.parser.add_option("--notify", "--sendto", dest="sendto", type="string",
45  help="Email addresses (comma-separated) to notify when job is complete.",
46  default=None)
47  self.parser.add_option("--deform", action="store_true",
48  dest="useSD", default=False,
49  help="Include surface deformations in alignment")
50  self.parser.add_option("-f", "--force", action="store_true",
51  dest="force", default=False,
52  help="Don't ask any questions, just over-write")
53  self.parser.add_option("--resubmit", action="store_true",
54  dest="resubmit", default=False,
55  help="Resubmit a job from the last iteration")
56  self.parser.add_option("--dry", dest="dryRun", type="int",
57  default=0,
58  help="Do not submit jobs, just set up the cfg files")
59  (self.opt,self.args) = self.parser.parse_args()
60 
61  self.mkdir(self.opt.outputdir)
62 
63  if self.opt.lstfile is None:
64  print "Unspecified lst file."
65  sys.exit(1)
66  if self.opt.iovfile is None:
67  print "Unspecified IOV list."
68  sys.exit(1)
69 
70  self.jobname = self.opt.outputdir.split('/')[-1]
71 
72  if self.opt.sendto is not None:
73  self.opt.sendto.strip()
74  self.opt.sendto.replace(","," ")
75  print "Job {} is configured to notify {}.".format(self.jobname, self.opt.sendto)
76 
77  self.SDflag = 1 if self.opt.useSD else 0
78 
79 
80  def mkdir( self, dirname ):
81  mkdir = 'mkdir -p %s' % dirname
82  ret = os.system( mkdir )
83  if( ret != 0 ):
84  print 'Please remove or rename directory: ', dirname
85  sys.exit(4)
86 
87  def notify(self, desc):
88  print desc
89  if self.opt.sendto is not None:
90  strcmd = "mail -s {1} {0} <<< \"{2}\"".format(self.opt.sendto, self.jobname, desc)
91  os.system(strcmd)
92 
93  def checkLastIteration(self):
94  lastIter=self.opt.niter
95  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
96  while (doesExist != 0):
97  lastIter -= 1
98  if lastIter < 0:
99  break
100  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
101  return lastIter
102 
103  def finalize(self, ret):
104  strresult=""
105  exitCode=0
106  if( ret != 0 ):
107  strresult = "Jobs cannot be submitted for {}. Exiting...".format(self.jobname)
108  exitCode=1
109  elif self.opt.dryRun > 0:
110  strresult = "Dry run setup is complete for {}.".format(self.jobname)
111  else:
112  lastIter=self.checkLastIteration()
113  if lastIter == self.opt.niter:
114  strresult = "The final iteration {}/alignments_iter{}.db is recorded successfully.".format(self.jobname, lastIter)
115  elif lastIter>0:
116  strresult = "The last successful iteration was {}/alignments_iter{}.db out of the {} requested iterations.".format(self.jobname, lastIter, self.opt.niter)
117  exitCode=1
118  else:
119  strresult = "None of the {} iterations were successful in job {}.".format(self.opt.niter, self.jobname)
120  exitCode=1
121  self.notify(strresult)
122  if exitCode!=0:
123  sys.exit(strresult)
124 
125  def submitJobs(self):
126  jobcmd=""
127  if self.opt.resubmit:
128  jobcmd = 'scripts/reiterator_py {} {} {} {} {} {}'.format(
129  self.opt.niter,
130  self.opt.outputdir,
131  self.opt.iovfile
132  )
133  else:
134  if self.opt.dryRun > 0:
135  print 'Dry run option is enabled. Will not submit jobs to the queue'
136  jobcmd = 'scripts/iterator_py {} {} {} {} {} {} {} {} {}'.format(
137  self.opt.niter,
138  self.opt.outputdir,
139  self.opt.lstfile,
140  self.opt.iovfile,
141  self.opt.commoncfg,
142  self.opt.aligncfg,
143  self.opt.trkselcfg,
144  self.SDflag,
145  self.opt.dryRun
146  )
147  ret = os.system( jobcmd )
148  self.finalize(ret)
149 
150 
151 
152 
153 
154 
155 if __name__ == '__main__':
156  batchManager = MyBatchManager()
157  batchManager.submitJobs()
158 
def finalize(self, ret)
Definition: batchHippy.py:103
def checkLastIteration(self)
Definition: batchHippy.py:93
def notify(self, desc)
Definition: batchHippy.py:87
if(dp >Float(M_PI)) dp-
def mkdir(self, dirname)
Definition: batchHippy.py:80