CMS 3D CMS Logo

batchHippy.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 from __future__ import print_function
4 import sys
5 import imp
6 import copy
7 import os
8 import shutil
9 import pickle
10 import math
11 import pprint
12 import subprocess
13 from datetime import date
14 from optparse import OptionParser
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("--redirectproxy", action="store_true",
57  dest="redirectproxy", default=False,
58  help="Redirect the proxy to a path visible in batch")
59  self.parser.add_option("--dry", dest="dryRun", type="int",
60  default=0,
61  help="Do not submit jobs, just set up the cfg files")
62  (self.opt,self.args) = self.parser.parse_args()
63 
64  self.checkProxy() # Check if Grid proxy initialized
65 
66  self.mkdir(self.opt.outputdir)
67 
68  if self.opt.lstfile is None:
69  print("Unspecified lst file.")
70  sys.exit(1)
71  if self.opt.iovfile is None:
72  print("Unspecified IOV list.")
73  sys.exit(1)
74 
75  self.jobname = self.opt.outputdir.split('/')[-1]
76 
77  if self.opt.redirectproxy:
78  print("Job {} is configured to redirect its Grid proxy.".format(self.jobname))
79  self.redirectProxy()
80 
81  if self.opt.sendto is not None:
82  self.opt.sendto.strip()
83  self.opt.sendto.replace(","," ")
84  print("Job {} is configured to notify {}.".format(self.jobname, self.opt.sendto))
85 
86  # Set numerical flags for iterator_py
87  self.SDflag = 1 if self.opt.useSD else 0
88  self.redirectproxyflag = 1 if self.opt.redirectproxy else 0
89 
90 
91  def mkdir(self, dirname):
92  mkdir = 'mkdir -p %s' % dirname
93  ret = os.system( mkdir )
94  if( ret != 0 ):
95  print('Please remove or rename directory: ', dirname)
96  sys.exit(4)
97 
98  def notify(self, desc):
99  print(desc)
100  if self.opt.sendto is not None:
101  strcmd = "mail -s {1} {0} <<< \"{2}\"".format(self.opt.sendto, self.jobname, desc)
102  os.system(strcmd)
103 
105  lastIter=self.opt.niter
106  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
107  while (doesExist != 0):
108  lastIter -= 1
109  if lastIter < 0:
110  break
111  doesExist=os.system("test -s {}/alignments_iter{}.db".format(self.opt.outputdir, lastIter))
112  return lastIter
113 
114  def finalize(self, ret):
115  strresult=""
116  exitCode=0
117  if( ret != 0 ):
118  strresult = "Jobs cannot be submitted for {}. Exiting...".format(self.jobname)
119  exitCode=1
120  elif self.opt.dryRun > 0:
121  strresult = "Dry run setup is complete for {}.".format(self.jobname)
122  else:
123  lastIter=self.checkLastIteration()
124  if lastIter == self.opt.niter:
125  strresult = "The final iteration {}/alignments_iter{}.db is recorded successfully.".format(self.jobname, lastIter)
126  elif lastIter>0:
127  strresult = "The last successful iteration was {}/alignments_iter{}.db out of the {} requested iterations.".format(self.jobname, lastIter, self.opt.niter)
128  exitCode=1
129  else:
130  strresult = "None of the {} iterations were successful in job {}.".format(self.opt.niter, self.jobname)
131  exitCode=1
132  self.notify(strresult)
133  if exitCode!=0:
134  sys.exit(strresult)
135 
136  def submitJobs(self):
137  jobcmd=""
138  if self.opt.resubmit:
139  jobcmd = 'scripts/reiterator_py {} {} {} {} {} {}'.format(
140  self.opt.niter,
141  self.opt.outputdir,
142  self.opt.iovfile
143  )
144  else:
145  if self.opt.dryRun > 0:
146  print('Dry run option is enabled. Will not submit jobs to the queue')
147  jobcmd = 'scripts/iterator_py {} {} {} {} {} {} {} {} {} {}'.format(
148  self.opt.niter,
149  self.opt.outputdir,
150  self.opt.lstfile,
151  self.opt.iovfile,
152  self.opt.commoncfg,
153  self.opt.aligncfg,
154  self.opt.trkselcfg,
155  self.SDflag,
156  self.redirectproxyflag,
157  self.opt.dryRun
158  )
159  ret = os.system( jobcmd )
160  self.finalize(ret)
161 
162  def checkProxy(self):
163  try:
164  subprocess.check_call(["voms-proxy-info", "--exists"])
165  except subprocess.CalledProcessError:
166  print("Please initialize your proxy before submitting.")
167  sys.exit(1)
168 
169  def redirectProxy(self):
170  local_proxy = subprocess.check_output(["voms-proxy-info", "--path"]).strip()
171  new_proxy_path = os.path.join(self.opt.outputdir,".user_proxy")
172  print("Copying local proxy {} to the job directory as {}.".format(local_proxy,new_proxy_path))
173  shutil.copyfile(local_proxy, new_proxy_path)
174 
175 
176 
177 if __name__ == '__main__':
178  batchManager = MyBatchManager()
179  batchManager.submitJobs()
batchHippy.MyBatchManager.checkProxy
def checkProxy(self)
Definition: batchHippy.py:162
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
batchHippy.MyBatchManager.jobname
jobname
Definition: batchHippy.py:75
batchHippy.MyBatchManager.submitJobs
def submitJobs(self)
Definition: batchHippy.py:136
batchHippy.MyBatchManager
Definition: batchHippy.py:17
batchHippy.MyBatchManager.parser
parser
Definition: batchHippy.py:22
batchHippy.MyBatchManager.checkLastIteration
def checkLastIteration(self)
Definition: batchHippy.py:104
batchHippy.MyBatchManager.redirectProxy
def redirectProxy(self)
Definition: batchHippy.py:169
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
batchHippy.MyBatchManager.mkdir
def mkdir(self, dirname)
Definition: batchHippy.py:91
batchHippy.MyBatchManager.redirectproxyflag
redirectproxyflag
Definition: batchHippy.py:88
batchHippy.MyBatchManager.__init__
def __init__(self)
Definition: batchHippy.py:20
main.parse_args
def parse_args(args)
Definition: main.py:63
batchHippy.MyBatchManager.SDflag
SDflag
Definition: batchHippy.py:87
format
batchHippy.MyBatchManager.notify
def notify(self, desc)
Definition: batchHippy.py:98
batchHippy.MyBatchManager.finalize
def finalize(self, ret)
Definition: batchHippy.py:114