CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
WorkFlowRunner.py
Go to the documentation of this file.
1 
2 from threading import Thread
3 
4 from Configuration.PyReleaseValidation import WorkFlow
5 import os,time
6 import shutil
7 from subprocess import Popen
8 
9 class WorkFlowRunner(Thread):
10  def __init__(self, wf, noRun=False,dryRun=False,cafVeto=True):
11  Thread.__init__(self)
12  self.wf = wf
13 
14  self.status=-1
15  self.report=''
16  self.nfail=0
17  self.npass=0
18  self.noRun=noRun
19  self.dryRun=dryRun
20  self.cafVeto=cafVeto
21 
22  self.wfDir=str(self.wf.numId)+'_'+self.wf.nameId
23  return
24 
25  def doCmd(self, cmd):
26 
27  msg = "\n# in: " +os.getcwd()
28  if self.dryRun: msg += " dryRun for '"
29  else: msg += " going to execute "
30  msg += cmd.replace(';','\n')
31  print msg
32 
33  cmdLog = open(self.wfDir+'/cmdLog','a')
34  cmdLog.write(msg+'\n')
35  cmdLog.close()
36 
37  ret = 0
38  if not self.dryRun:
39  p = Popen(cmd, shell=True)
40  ret = os.waitpid(p.pid, 0)[1]
41  if ret != 0:
42  print "ERROR executing ",cmd,'ret=', ret
43 
44  return ret
45 
46  def run(self):
47 
48  startDir = os.getcwd()
49 
50  if not os.path.exists(self.wfDir):
51  os.makedirs(self.wfDir)
52  elif not self.dryRun: # clean up to allow re-running in the same overall devel area, then recreate the dir to make sure it exists
53  print "cleaning up ", self.wfDir, ' in ', os.getcwd()
54  shutil.rmtree(self.wfDir)
55  os.makedirs(self.wfDir)
56 
57  preamble = 'cd '+self.wfDir+'; '
58 
59  startime='date %s' %time.asctime()
60 
61  # check where we are running:
62  onCAF = False
63  if 'cms/caf/cms' in os.environ['CMS_PATH']:
64  onCAF = True
65 
66  ##needs to set
67  #self.report
68  self.npass = []
69  self.nfail = []
70  self.stat = []
71  self.retStep = []
72 
73  def closeCmd(i,ID):
74  return ' > %s 2>&1; ' % ('step%d_'%(i,)+ID+'.log ',)
75 
76  inFile=None
77  lumiRangeFile=None
78  aborted=False
79  for (istepmone,com) in enumerate(self.wf.cmds):
80  istep=istepmone+1
81  cmd = preamble
82  if aborted:
83  self.npass.append(0)
84  self.nfail.append(0)
85  self.retStep.append(0)
86  self.stat.append('NOTRUN')
87  continue
88  if not isinstance(com,str):
89  if self.cafVeto and (com.location == 'CAF' and not onCAF):
90  print "You need to be no CAF to run",self.wf.numId
91  self.npass.append(0)
92  self.nfail.append(0)
93  self.retStep.append(0)
94  self.stat.append('NOTRUN')
95  aborted=True
96  continue
97  #create lumiRange file first so if dbs fails we get its error code
98  cmd2 = com.lumiRanges()
99  if cmd2:
100  cmd2 =cmd+cmd2+closeCmd(istep,'lumiRanges')
101  lumiRangeFile='step%d_lumiRanges.log'%(istep,)
102  retStep = self.doCmd(cmd2)
103  cmd+=com.dbs()
104  cmd+=closeCmd(istep,'dbsquery')
105  retStep = self.doCmd(cmd)
106  #don't use the file list executed, but use the dbs command of cmsDriver for next step
107  inFile='filelist:step%d_dbsquery.log'%(istep,)
108  print "---"
109  else:
110  #chaining IO , which should be done in WF object already and not using stepX.root but <stepName>.root
111  cmd += com
112  if self.noRun:
113  cmd +=' --no_exec'
114  if inFile: #in case previous step used DBS query (either filelist of dbs:)
115  cmd += ' --filein '+inFile
116  inFile=None
117  if lumiRangeFile: #DBS query can also restrict lumi range
118  cmd += ' --lumiToProcess '+lumiRangeFile
119  lumiRangeFile=None
120  if 'HARVESTING' in cmd and not '134' in str(self.wf.numId) and not '--filein' in cmd:
121  cmd+=' --filein file:step%d_inDQM.root --fileout file:step%d.root '%(istep-1,istep)
122  else:
123  if istep!=1 and not '--filein' in cmd:
124  cmd+=' --filein file:step%s.root '%(istep-1,)
125  if not '--fileout' in com:
126  cmd+=' --fileout file:step%s.root '%(istep,)
127 
128 
129 
130  cmd+=closeCmd(istep,self.wf.nameId)
131  retStep = self.doCmd(cmd)
132 
133  self.retStep.append(retStep)
134  if (retStep!=0):
135  #error occured
136  self.npass.append(0)
137  self.nfail.append(1)
138  self.stat.append('FAILED')
139  #to skip processing
140  aborted=True
141  else:
142  #things went fine
143  self.npass.append(1)
144  self.nfail.append(0)
145  self.stat.append('PASSED')
146 
147 
148  os.chdir(startDir)
149 
150  endtime='date %s' %time.asctime()
151  tottime='%s-%s'%(endtime,startime)
152 
153 
154  #### wrap up ####
155 
156  logStat=''
157  for i,s in enumerate(self.stat):
158  logStat+='Step%d-%s '%(i,s)
159  self.report='%s_%s %s - time %s; exit: '%(self.wf.numId,self.wf.nameId,logStat,tottime)+' '.join(map(str,self.retStep))+'\n'
160 
161  return
162 
163 
164 
dictionary map
Definition: Association.py:205
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
npass
needs to set self.report