CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
run_regression.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 import os
4 import re
5 import subprocess
6 import getopt
7 import random
8 from xml.dom.minidom import parse, parseString
9 import common_db
10 import results_db
11 import reference_db
12 
13 def ParseXML(filename, label):
14  initSeq = []
15  mainSeq = []
16  finalSeq = []
17  dom = parse(filename)
18  xml = dom.getElementsByTagName("xml")
19  foundLabel = False
20  if xml != None:
21  for xm in xml:
22  testData = xm.getElementsByTagName("test")
23  for it in testData:
24  #print it.toxml()
25  if "name" in it.attributes.keys():
26  tLabel = str(it.attributes["name"].value)
27  if tLabel == label:
28  foundLabel = True
29  if foundLabel == True:
30  inits = it.getElementsByTagName("init")
31  finals = it.getElementsByTagName("final")
32  seqs = it.getElementsByTagName("sequence")
33  for init in inits:
34  commands = init.getElementsByTagName("command")
35  for command in commands:
36  commPars0 = None
37  commPars1 = None
38  if "exec" in command.attributes.keys():
39  commPars0 = str(command.attributes["exec"].value)
40  if "env" in command.attributes.keys():
41  commPars1 = str(command.attributes["env"].value)
42  initSeq.append((commPars0,commPars1))
43  for seq in seqs:
44  commands = seq.getElementsByTagName("command")
45  for command in commands:
46  commPars0 = None
47  commPars1 = None
48  commPars2 = None
49  if "exec" in command.attributes.keys():
50  commPars0 = str(command.attributes["exec"].value)
51  if "result" in command.attributes.keys():
52  commPars2 = str(command.attributes["result"].value)
53  if "env" in command.attributes.keys():
54  commPars1 = str(command.attributes["env"].value)
55  mainSeq.append( (commPars0,commPars1,commPars2) )
56  for final in finals:
57  commands = final.getElementsByTagName("command")
58  for command in commands:
59  commPars0 = None
60  commPars1 = None
61  if "exec" in command.attributes.keys():
62  commPars0 = str(command.attributes["exec"].value)
63  if "env" in command.attributes.keys():
64  commPars1 = str(command.attributes["env"].value)
65  finalSeq.append( (commPars0,commPars1) )
66  foundLabel = False
67  return initSeq,mainSeq,finalSeq
68 
69 def SetEnv( release, arch, path):
70  CONNECTION_STRING,USERNAME,PASSWORD,AUTH_PATH = common_db.getDBConnectionParams()
71  random.seed()
72  srcPath = os.path.join(path, release,"src")
73  cmds = """
74 if [ $RETVAL = 0 ]; then
75 echo "Setting environment variables for """+release+""" """+arch+""" "
76 echo "path : """+path+""""
77  eval pushd """+srcPath+"""
78  RETVAL=$?
79  if [ $RETVAL = 0 ]; then
80  export SCRAM_ARCH="""+arch+"""
81  RETVAL=$?
82  if [ $RETVAL = 0 ]; then
83  eval `scram runtime -sh`
84  RETVAL=$?
85  if [ $RETVAL = 0 ]; then
86  export TNS_ADMIN=/afs/cern.ch/project/oracle/admin
87  RETVAL=$?
88  if [ $RETVAL = 0 ]; then
89  eval popd
90  RETVAL=$?
91  fi
92  fi
93  fi
94  TRELEASE="""+release+"""
95  TARCH="""+arch+"""
96  TPATH="""+path+"""
97  TMAPNAME="""+release+"""_"""+arch+"""
98  TMAINDB="""+CONNECTION_STRING+"""
99  TAUXDB="""+"oracle://cms_orcoff_prep/CMS_COND_WEB"+"""
100  TUSERNAME="""+USERNAME+"""
101  TPASSWORD="""+PASSWORD+"""
102  TTEST=$LOCALRT/test/$TARCH
103  TBIN=$LOCALRT/bin/$TARCH
104  TAUTH="""+AUTH_PATH+"""
105  TSEED="""+str(random.randrange(1, 10))+"""
106 echo "Environment variables set successfully"
107  else
108 echo "Setting environment failed on """+release+""" """+arch+""" return code : $RETVAL"
109  fi
110 fi
111 echo "----------------------------------------------"
112 """
113  return cmds
114 
115 def Command(runStr):
116  cmds = """
117 if [ $RETVAL = 0 ]; then
118 echo "Executing Command """+runStr+""" "
119 echo "with $TRELEASE $TARCH :"
120  """+runStr+"""
121  RETVAL=$?
122  if [ $RETVAL != 0 ]; then
123 echo "Task failed on $TRELEASE $TARCH return code : $RETVAL"
124  else
125 echo "Task performed successfully"
126  fi
127 fi
128 """
129  return cmds
130 
131 def RunTest(label,testSeq, release, arch, path, refRelease, refArch, refPath ):
132  cmds ="""
133  RETVAL=0
134 echo "*****************************************************************************************"
135 echo "Reference release: """+refRelease+""" "
136 echo "Arch: """+refArch+""" "
137 echo "Path: """+refPath+""" "
138 echo "*****************************************************************************************"
139 """
140  nr = 0
141  currEnv = 0
142  print "-> init"
143  initSeq = testSeq[0]
144  mainSeq = testSeq[1]
145  finalSeq = testSeq[2]
146  for step in initSeq:
147  cmds += """
148 echo "==============================================="
149 """
150  print step[0]
151  if step[1] == "cand" and currEnv != 1:
152  cmds += SetEnv(release,arch,path)
153  currEnv = 1
154  elif step[1] == "ref" and currEnv != 2:
155  cmds += SetEnv(refRelease, refArch,refPath)
156  currEnv = 2
157  cmds +=Command(step[0])
158  print "-> test sequence"
159  for step in mainSeq:
160  cmds += """
161 echo "==============================================="
162 """
163  print step[0]
164  if step[1] == "cand" and currEnv != 1:
165  cmds += SetEnv(release,arch,path)
166  currEnv = 1
167  elif step[1] == "ref" and currEnv != 2:
168  cmds += SetEnv(refRelease, refArch,refPath)
169  currEnv = 2
170  cmds +=Command(step[0])
171  cmds += 'RCODE['+str(nr)+']=$RETVAL'
172  nr +=1
173  print "-> final"
174  cmds += """
175  RETVAL=0
176  """
177  for step in finalSeq:
178  cmds += """
179 echo "==============================================="
180 """
181  print step[0]
182  if step[1] == "cand" and currEnv != 1:
183  cmds += SetEnv(release,arch,path)
184  currEnv = 1
185  elif step[1] == "ref" and currEnv != 2:
186  cmds += SetEnv(refRelease, refArch,refPath)
187  currEnv = 2
188  cmds +=Command(step[0])
189  cmds += """
190 echo "==============================================="
191  echo "Script return code : ${RCODE[1]}"
192 echo "!L!"""+label+"""!TR!"""+release+"""!TA!"""+arch+"""!RR!"""+refRelease+"""!RA!"""+refArch
193  for i in range (0, nr):
194  cmds+= "!C"+str(i)+"!${RCODE["+str(i)+"]}"
195  cmds += "\""
196  return cmds
197 
198 def ExecuteCommand( cmdList ):
199  stdout_value = None
200  if cmdList != "":
201  cmdList+="""
202  echo "End of test"
203  echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" """
204  pipe = subprocess.Popen(cmdList, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
205  stdout_value = pipe.communicate()[0]
206  return stdout_value
207 
209 
210  def __init__( self, conn ):
212  self.refDb = reference_db.ReferenceDB( conn )
213  self.label = None
214  self.n_res = 0
215  self.resTags = None
216  self.out_value = None
217 
218  def runOnDb(self, label, release, arch, path):
219  relID = reference_db.ExtractID(release)
220  print 'Running test "'+label+'" on rel='+release+' arch='+arch+' from path= '+path
221  cmds = """
222 echo ""
223 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
224 echo "Candidate release: """+release+""" "
225 echo "Arch: """+arch+""" "
226 echo "Path: """+path+""" "
227 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
228 """
229  testSeq = ParseXML("sequences.xml", label)
230  self.n_res = len(testSeq[1])
231  if(self.n_res == 0):
232  print "Error : no test sequence found for label %s"%label
233  return 0
234  self.resTags = []
235  for step in testSeq[1]:
236  self.resTags.append( step[2] )
237  releases = self.refDb.listReleases( relID )
238  for rel in releases:
239  cmd = RunTest( label,testSeq, release, arch, path, rel[0], rel[1], rel[2])
240  cmds += cmd
241  self.out_value = ExecuteCommand( cmds )
242  self.label = label
243  return len(releases)
244 
245  def runOnReference(self, label, release, arch, path, refRelease, refArch, refPath):
246  relID = reference_db.ExtractID(release)
247  print "Testing "+release+" "+arch+" from path: "+path
248  curs = conn.cursor()
249  cmds = """
250 echo ""
251 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
252 echo "Candidate release: """+release+""" "
253 echo "Arch: """+arch+""" "
254 echo "Path: """+path+""" "
255 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
256 """
257  testSeq = ParseXML("sequences.xml", label)
258  self.n_res = len( testSeq[1] )
259  if(self.n_res == 0):
260  print "Error : no test sequence found for label %s"%label
261  return 0
262  self.resTags = []
263  for step in testSeq[1]:
264  self.resTags.append( step[2] )
265  cmd = RunTest(label,testSeq, release, arch, path, refRelease, refArch, refPath)
266  cmds += cmd
267  self.out_value = ExecuteCommand( cmds )
268  self.label = label
269  return 1
270 
271  def finalize( self, writeFlag ):
272 
273  reStr = "\!L\!([^!]+)\!TR\!([^!]+)\!TA\!([^!]+)\!RR\!([^!]+)\!RA\!([^!]+)"
274  for i in range (0, self.n_res):
275  reStr += "\!C"+str(i)+"\!(\d+)"
276  #print 'restr1=',reStr
277 
278  pattern = re.compile(reStr)
279  matching = pattern.findall(self.out_value)
280  stdoutMod = pattern.sub("", self.out_value)
281  timeStamp = self.resDb.getDate()
282  stat = "SUCCESS"
283  runID = 0
284  if( writeFlag ):
285  runID = self.resDb.getNewRunId()
286  for match in matching:
287  #print match
288  if( writeFlag):
289  self.resDb.writeResult(runID, timeStamp, match,self.resTags)
290  for i in range(5, len(match)):
291  if( match[i] != str(0) ):
292  stat = "FAILURE"
293  print stdoutMod
294  if( writeFlag ):
295  self.resDb.addResultLog(runID, stdoutMod)
296  print "Test '%s' runID=%d" %(self.label, runID)
297  print "Exit status=%s" %stat
298  print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
299 
300 def CmdUsage():
301  print "Command line arguments :"
302  print "-F (--full) -t [test_label] -r [release] -a [arch] -p [path]: runs the full test. "
303  print "-S (--self) -t [test_label] -r [release] -a [arch] -p [path]: runs the self test. "
304  print "-R [ref_release] -A [ref_arch] -P [ref_path] -t [test_label] -r [release] -a [arch] -p [path]: runs the test against the specified ref release. "
305  print " optional flag -w: write the test result in the database. "
306 
307 def CheckPath (release, arch, path):
308  if(os.path.exists(path)):
309  if(os.path.exists(os.path.join(path, release))):
310  if(os.path.exists(os.path.join(path, release, "test", arch))):
311  return True
312  else:
313  print "Architecture not found"
314  return False
315  else:
316  print "Release not found"
317  return False
318  else:
319  print "Path not found"
320  return False
321 
322 try:
323  opts, args = getopt.getopt(sys.argv[1:], "FSR:A:P:t:r:a:p:hw", ['full', 'self', 'help'])
324 except getopt.GetoptError, err:
325  # print help information and exit:
326  print str(err) # will print something like "option -a not recognized"
327  CmdUsage()
328  sys.exit(2)
329 RELEASE = None
330 ARCH = None
331 PATH = None
332 REF_RELEASE = None
333 REF_ARCH = None
334 REF_PATH = None
335 LABEL = None
336 fflag = False
337 sflag = False
338 wflag = False
339 for o, a in opts:
340  if o in ("-F", "--full"):
341  fflag = True
342  elif o in ("-S", "--self"):
343  sflag = True
344  elif o == "-R":
345  REF_RELEASE = a
346  elif o == "-A":
347  REF_ARCH = a
348  elif o == "-P":
349  REF_PATH = a
350  elif o == "-t":
351  LABEL = a
352  elif o == "-r":
353  RELEASE = a
354  elif o == "-a":
355  ARCH = a
356  elif o == "-p":
357  PATH = a
358  elif o == "-w":
359  wflag = True
360  elif o in ("-h", "--help"):
361  CmdUsage()
362  sys.exit(2)
363  else:
364  assert False, "unhandled option"
365 if( fflag == False and sflag == False and REF_RELEASE == None ):
366  print "Error: missing main run option."
367 else:
368  okPar = True
369  if ( LABEL == None ):
370  okPar = False
371  print "Error: missing -l (label) parameter"
372  if(RELEASE == None ):
373  okPar = False
374  print "Error: missing -r (release) parameter"
375  if( ARCH == None ):
376  okPar = False
377  print "Error: missing -a (architecture) parameter"
378  if( PATH == None):
379  okPar = False
380  print "Error: missing -p (path) parameter"
381  if(CheckPath(RELEASE, ARCH, PATH) == False):
382  okPar = False
383  print "Error: bad path specified for the release"
384  if( okPar == True ):
386  test = RegressionTest( conn )
387  done = False
388  ret = 0
389  if( fflag == True and done==False ):
390  ret = test.runOnDb(LABEL, RELEASE, ARCH, PATH)
391  done = True
392  if( sflag == True and done==False ):
393  ret = test.runOnReference(LABEL, RELEASE, ARCH, PATH, RELEASE, ARCH, PATH)
394  done = True
395  if( REF_RELEASE != None and done==False ):
396  if( REF_ARCH == None ):
397  okPar = False
398  print "Error: missing -A (ref architecture) parameter"
399  if( REF_PATH == None):
400  okPar = False
401  print "Error: missing -P (ref path) parameter"
402  if(CheckPath(REF_RELEASE, REF_ARCH, REF_PATH) == False):
403  okPar = False
404  print "Error: bad path specified for the ref release"
405  if( okPar == True ):
406  ret = test.runOnReference(LABEL, RELEASE, ARCH, PATH, REF_RELEASE, REF_ARCH, REF_PATH)
407  done = True
408  if ( done == True and ret>0 ):
409  test.finalize( wflag )
410  conn.close()
Evaluator * parse(const T &text)
def getDBConnectionParams
Definition: common_db.py:38
def createDBConnection
Definition: common_db.py:32
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31