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 das 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.das()
104  cmd+=closeCmd(istep,'dasquery')
105  retStep = self.doCmd(cmd)
106  #don't use the file list executed, but use the das command of cmsDriver for next step
107  inFile='filelist:step%d_dasquery.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 DAS query (either filelist of das:)
115  cmd += ' --filein '+inFile
116  inFile=None
117  if lumiRangeFile: #DAS query can also restrict lumi range
118  cmd += ' --lumiToProcess '+lumiRangeFile
119  lumiRangeFile=None
120  # 134 is an existing workflow where harvesting has to operate on AlcaReco and NOT on DQM; hard-coded..
121  if 'HARVESTING' in cmd and not 134==self.wf.numId and not '--filein' in cmd:
122  cmd+=' --filein file:step%d_inDQM.root --fileout file:step%d.root '%(istep-1,istep)
123  else:
124  if istep!=1 and not '--filein' in cmd:
125  cmd+=' --filein file:step%s.root '%(istep-1,)
126  if not '--fileout' in com:
127  cmd+=' --fileout file:step%s.root '%(istep,)
128 
129 
130 
131  cmd+=closeCmd(istep,self.wf.nameId)
132  retStep = self.doCmd(cmd)
133 
134  self.retStep.append(retStep)
135  if (retStep!=0):
136  #error occured
137  self.npass.append(0)
138  self.nfail.append(1)
139  self.stat.append('FAILED')
140  #to skip processing
141  aborted=True
142  else:
143  #things went fine
144  self.npass.append(1)
145  self.nfail.append(0)
146  self.stat.append('PASSED')
147 
148 
149  os.chdir(startDir)
150 
151  endtime='date %s' %time.asctime()
152  tottime='%s-%s'%(endtime,startime)
153 
154 
155  #### wrap up ####
156 
157  logStat=''
158  for i,s in enumerate(self.stat):
159  logStat+='Step%d-%s '%(i,s)
160  self.report='%s_%s %s - time %s; exit: '%(self.wf.numId,self.wf.nameId,logStat,tottime)+' '.join(map(str,self.retStep))+'\n'
161 
162  return
163 
164 
165 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
npass
needs to set self.report