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 
17  '''Batch manager specific to cmsRun processes.'''
18 
19  def __init__(self):
20  # define options and arguments ====================================
21  self.parser = OptionParser()
22  self.parser.add_option("-o", "--outdir", dest="outputdir", type="string",
23  help="Name of the local output directory for your jobs. This directory will be created automatically.",
24  default="./")
25  self.parser.add_option("--commoncfg", dest="commoncfg", type="string",
26  help="Name of the common config file.",
27  default="python/common_cff_py.txt")
28  self.parser.add_option("--aligncfg", dest="aligncfg", type="string",
29  help="Name of the align. config file.",
30  default="python/align_tpl_py.txt")
31  self.parser.add_option("--niter", dest="niter", type="int",
32  help="Number of iterations",
33  default="15")
34  self.parser.add_option("--lst", "--listfile", "--lstfile", dest="lstfile", type="string",
35  help="lst file to read",
36  default=None)
37  self.parser.add_option("--iovs", "--iovfile", dest="iovfile", type="string",
38  help="IOV list to read",
39  default=None)
40  self.parser.add_option("--trkselcfg", "--trackselectionconfig", dest="trkselcfg", type="string",
41  help="Track selection config location",
42  default="python")
43  self.parser.add_option("--notify", "--sendto", dest="sendto", type="string",
44  help="Email addresses (comma-separated) to notify when job is complete.",
45  default=None)
46  self.parser.add_option("--deform", action="store_true",
47  dest="useSD", default=False,
48  help="Include surface deformations in alignment")
49  self.parser.add_option("-f", "--force", action="store_true",
50  dest="force", default=False,
51  help="Don't ask any questions, just over-write")
52  self.parser.add_option("--resubmit", action="store_true",
53  dest="resubmit", default=False,
54  help="Resubmit a job from the last iteration")
55  self.parser.add_option("--redirectproxy", action="store_true",
56  dest="redirectproxy", default=False,
57  help="Redirect the proxy to a path visible in batch")
58  self.parser.add_option("--dry", dest="dryRun", type="int",
59  default=0,
60  help="Do not submit jobs, just set up the cfg files")
61  (self.opt,self.args) = self.parser.parse_args()
62 
63  self.checkProxy() # Check if Grid proxy initialized
64 
65  self.mkdir(self.opt.outputdir)
66 
67  if self.opt.lstfile is None:
68  print "Unspecified lst file."
69  sys.exit(1)
70  if self.opt.iovfile is None:
71  print "Unspecified IOV list."
72  sys.exit(1)
73 
74  self.jobname = self.opt.outputdir.split('/')[-1]
75 
76  if self.opt.redirectproxy:
77  print "Job {} is configured to redirect its Grid proxy.".format(self.jobname)
78  self.redirectProxy()
79 
80  if self.opt.sendto is not None:
81  self.opt.sendto.strip()
82  self.opt.sendto.replace(","," ")
83  print "Job {} is configured to notify {}.".format(self.jobname, self.opt.sendto)
84 
85  # Set numerical flags for iterator_py
86  self.SDflag = 1 if self.opt.useSD else 0
87  self.redirectproxyflag = 1 if self.opt.redirectproxy else 0
88 
89 
90  def mkdir(self, dirname):
91  mkdir = 'mkdir -p %s' % dirname
92  ret = os.system( mkdir )
93  if( ret != 0 ):
94  print 'Please remove or rename directory: ', dirname
95  sys.exit(4)
96 
97  def notify(self, desc):
98  print desc
99  if self.opt.sendto is not None:
100  strcmd = "mail -s {1} {0} <<< \"{2}\"".format(self.opt.sendto, self.jobname, desc)
101  os.system(strcmd)
102 
104  lastIter=self.opt.niter
105  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
106  while (doesExist != 0):
107  lastIter -= 1
108  if lastIter < 0:
109  break
110  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
111  return lastIter
112 
113  def finalize(self, ret):
114  strresult=""
115  exitCode=0
116  if( ret != 0 ):
117  strresult = "Jobs cannot be submitted for {}. Exiting...".format(self.jobname)
118  exitCode=1
119  elif self.opt.dryRun > 0:
120  strresult = "Dry run setup is complete for {}.".format(self.jobname)
121  else:
122  lastIter=self.checkLastIteration()
123  if lastIter == self.opt.niter:
124  strresult = "The final iteration {}/alignments_iter{}.db is recorded successfully.".format(self.jobname, lastIter)
125  elif lastIter>0:
126  strresult = "The last successful iteration was {}/alignments_iter{}.db out of the {} requested iterations.".format(self.jobname, lastIter, self.opt.niter)
127  exitCode=1
128  else:
129  strresult = "None of the {} iterations were successful in job {}.".format(self.opt.niter, self.jobname)
130  exitCode=1
131  self.notify(strresult)
132  if exitCode!=0:
133  sys.exit(strresult)
134 
135  def submitJobs(self):
136  jobcmd=""
137  if self.opt.resubmit:
138  jobcmd = 'scripts/reiterator_py {} {} {} {} {} {}'.format(
139  self.opt.niter,
140  self.opt.outputdir,
141  self.opt.iovfile
142  )
143  else:
144  if self.opt.dryRun > 0:
145  print 'Dry run option is enabled. Will not submit jobs to the queue'
146  jobcmd = 'scripts/iterator_py {} {} {} {} {} {} {} {} {} {}'.format(
147  self.opt.niter,
148  self.opt.outputdir,
149  self.opt.lstfile,
150  self.opt.iovfile,
151  self.opt.commoncfg,
152  self.opt.aligncfg,
153  self.opt.trkselcfg,
154  self.SDflag,
155  self.redirectproxyflag,
156  self.opt.dryRun
157  )
158  ret = os.system( jobcmd )
159  self.finalize(ret)
160 
161  def checkProxy(self):
162  try:
163  subprocess.check_call(["voms-proxy-info", "--exists"])
164  except subprocess.CalledProcessError:
165  print "Please initialize your proxy before submitting."
166  sys.exit(1)
167 
168  def redirectProxy(self):
169  local_proxy = subprocess.check_output(["voms-proxy-info", "--path"]).strip()
170  new_proxy_path = os.path.join(self.opt.outputdir,".user_proxy")
171  print "Copying local proxy {} to the job directory as {}.".format(local_proxy,new_proxy_path)
172  shutil.copyfile(local_proxy, new_proxy_path)
173 
174 
175 
176 if __name__ == '__main__':
177  batchManager = MyBatchManager()
178  batchManager.submitJobs()
def finalize(self, ret)
Definition: batchHippy.py:113
def notify(self, desc)
Definition: batchHippy.py:97
def mkdir(self, dirname)
Definition: batchHippy.py:90