CMS 3D CMS Logo

WorkFlowRunner.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from threading import Thread
3 from Configuration.PyReleaseValidation import WorkFlow
4 import os,time
5 import shutil
6 from subprocess import Popen
7 from os.path import exists, basename, join
8 from datetime import datetime
9 
10 class WorkFlowRunner(Thread):
11  def __init__(self, wf, noRun=False,dryRun=False,cafVeto=True,dasOptions="",jobReport=False, nThreads=1, nStreams=0, maxSteps=9999):
12  Thread.__init__(self)
13  self.wf = wf
14 
15  self.status=-1
16  self.report=''
17  self.nfail=0
18  self.npass=0
19  self.noRun=noRun
20  self.dryRun=dryRun
21  self.cafVeto=cafVeto
22  self.dasOptions=dasOptions
23  self.jobReport=jobReport
24  self.nThreads=nThreads
25  self.nStreams=nStreams
26  self.maxSteps=maxSteps
27 
28  self.wfDir=str(self.wf.numId)+'_'+self.wf.nameId
29  return
30 
31  def doCmd(self, cmd):
32 
33  msg = "\n# in: " +os.getcwd()
34  if self.dryRun: msg += " dryRun for '"
35  else: msg += " going to execute "
36  msg += cmd.replace(';','\n')
37  print(msg)
38 
39  cmdLog = open(self.wfDir+'/cmdLog','a')
40  cmdLog.write(msg+'\n')
41  cmdLog.close()
42 
43  ret = 0
44  if not self.dryRun:
45  p = Popen(cmd, shell=True)
46  ret = os.waitpid(p.pid, 0)[1]
47  if ret != 0:
48  print("ERROR executing ",cmd,'ret=', ret)
49 
50  return ret
51 
52  def run(self):
53 
54  startDir = os.getcwd()
55 
56  if not os.path.exists(self.wfDir):
57  os.makedirs(self.wfDir)
58  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
59  print("cleaning up ", self.wfDir, ' in ', os.getcwd())
60  shutil.rmtree(self.wfDir)
61  os.makedirs(self.wfDir)
62 
63  preamble = 'cd '+self.wfDir+'; '
64 
65  realstarttime = datetime.now()
66  startime='date %s' %time.asctime()
67 
68  # check where we are running:
69  onCAF = False
70  if 'cms/caf/cms' in os.environ['CMS_PATH']:
71  onCAF = True
72 
73 
75  self.npass = []
76  self.nfail = []
77  self.stat = []
78  self.retStep = []
79 
80  def closeCmd(i,ID):
81  return ' > %s 2>&1; ' % ('step%d_'%(i,)+ID+'.log ',)
82 
83  inFile=None
84  lumiRangeFile=None
85  aborted=False
86  for (istepmone,com) in enumerate(self.wf.cmds):
87  # isInputOk is used to keep track of the das result. In case this
88  # is False we use a different error message to indicate the failed
89  # das query.
90  isInputOk=True
91  istep=istepmone+1
92  cmd = preamble
93  if aborted:
94  self.npass.append(0)
95  self.nfail.append(0)
96  self.retStep.append(0)
97  self.stat.append('NOTRUN')
98  continue
99  if not isinstance(com,str):
100  if self.cafVeto and (com.location == 'CAF' and not onCAF):
101  print("You need to be no CAF to run",self.wf.numId)
102  self.npass.append(0)
103  self.nfail.append(0)
104  self.retStep.append(0)
105  self.stat.append('NOTRUN')
106  aborted=True
107  continue
108  #create lumiRange file first so if das fails we get its error code
109  cmd2 = com.lumiRanges()
110  if cmd2:
111  cmd2 =cmd+cmd2+closeCmd(istep,'lumiRanges')
112  lumiRangeFile='step%d_lumiRanges.log'%(istep,)
113  retStep = self.doCmd(cmd2)
114  if (com.dataSetParent):
115  cmd3=cmd+com.das(self.dasOptions,com.dataSetParent)+closeCmd(istep,'dasparentquery')
116  retStep = self.doCmd(cmd3)
117  cmd+=com.das(self.dasOptions,com.dataSet)
118  cmd+=closeCmd(istep,'dasquery')
119  retStep = self.doCmd(cmd)
120  #don't use the file list executed, but use the das command of cmsDriver for next step
121  # If the das output is not there or it's empty, consider it an
122  # issue of this step, not of the next one.
123  dasOutputPath = join(self.wfDir, 'step%d_dasquery.log'%(istep,))
124  # Check created das output in no-dryRun mode only
125  if not self.dryRun:
126  if not exists(dasOutputPath):
127  retStep = 1
128  dasOutput = None
129  else:
130  # We consider only the files which have at least one logical filename
131  # in it. This is because sometimes das fails and still prints out junk.
132  dasOutput = [l for l in open(dasOutputPath).read().split("\n") if l.startswith("/")]
133  if not dasOutput:
134  retStep = 1
135  isInputOk = False
136 
137  inFile = 'filelist:' + basename(dasOutputPath)
138  print("---")
139  else:
140  #chaining IO , which should be done in WF object already and not using stepX.root but <stepName>.root
141  cmd += com
142  if self.noRun:
143  cmd +=' --no_exec'
144  # in case previous step used DAS query (either filelist of das:)
145  # not to be applied for premixing stage1 to allow combiend stage1+stage2 workflow
146  if inFile and not 'premix_stage1' in cmd:
147  cmd += ' --filein '+inFile
148  inFile=None
149  if lumiRangeFile: #DAS query can also restrict lumi range
150  cmd += ' --lumiToProcess '+lumiRangeFile
151  lumiRangeFile=None
152  # 134 is an existing workflow where harvesting has to operate on AlcaReco and NOT on DQM; hard-coded..
153  if 'HARVESTING' in cmd and not 134==self.wf.numId and not '--filein' in cmd:
154  cmd+=' --filein file:step%d_inDQM.root --fileout file:step%d.root '%(istep-1,istep)
155  else:
156  # Disable input for premix stage1 to allow combined stage1+stage2 workflow
157  # Disable input for premix stage2 in FastSim to allow combined stage1+stage2 workflow (in FS, stage2 does also GEN)
158  # Ugly hack but works
159  if istep!=1 and not '--filein' in cmd and not 'premix_stage1' in cmd and not ("--fast" in cmd and "premix_stage2" in cmd):
160  cmd+=' --filein file:step%s.root '%(istep-1,)
161  if not '--fileout' in com:
162  cmd+=' --fileout file:step%s.root '%(istep,)
163  if self.jobReport:
164  cmd += ' --suffix "-j JobReport%s.xml " ' % istep
165  if (self.nThreads > 1) and ('HARVESTING' not in cmd) and ('ALCAHARVEST' not in cmd):
166  cmd += ' --nThreads %s' % self.nThreads
167  if (self.nStreams > 0) and ('HARVESTING' not in cmd) and ('ALCAHARVEST' not in cmd):
168  cmd += ' --nStreams %s' % self.nStreams
169  cmd+=closeCmd(istep,self.wf.nameId)
170  retStep = 0
171  if istep>self.maxSteps:
172  wf_stats = open("%s/wf_steps.txt" % self.wfDir,"a")
173  wf_stats.write('step%s:%s\n' % (istep, cmd))
174  wf_stats.close()
175  else: retStep = self.doCmd(cmd)
176 
177  self.retStep.append(retStep)
178  if retStep == 32000:
179  # A timeout occurred
180  self.npass.append(0)
181  self.nfail.append(1)
182  self.stat.append('TIMEOUT')
183  aborted = True
184  elif (retStep!=0):
185  #error occured
186  self.npass.append(0)
187  self.nfail.append(1)
188  if not isInputOk:
189  self.stat.append("DAS_ERROR")
190  else:
191  self.stat.append('FAILED')
192  #to skip processing
193  aborted=True
194  else:
195  #things went fine
196  self.npass.append(1)
197  self.nfail.append(0)
198  self.stat.append('PASSED')
199 
200  os.chdir(startDir)
201  endtime='date %s' %time.asctime()
202  tottime='%s-%s'%(endtime,startime)
203 
204 
205 
206 
207  logStat=''
208  for i,s in enumerate(self.stat):
209  logStat+='Step%d-%s '%(i,s)
210  #self.report='%s_%s+%s %s - time %s; exit: '%(self.wf.numId,self.wf.nameId,'+'.join(self.wf.stepList),logStat,tottime)+' '.join(map(str,self.retStep))+'\n'
211  self.report='%s_%s %s - time %s; exit: '%(self.wf.numId,self.wf.nameId,logStat,tottime)+' '.join(map(str,self.retStep))+'\n'
212 
213  return
214 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
npass
needs to set self.report
def __init__(self, wf, noRun=False, dryRun=False, cafVeto=True, dasOptions="", jobReport=False, nThreads=1, nStreams=0, maxSteps=9999)
#define str(s)