All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ScriptTest_V6.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 
3 
4 import os
5 import xml.etree.ElementTree as ET
6 import sys
7 import xml.sax.saxutils as XSS
8 import BaseTest as GT
9 import QMTTest as QT
10 import re
11 import time
12 
13 
14 #-------------------------------------------------------------------------#
15 
16 
17 # XMLwriter parse the results of the tests in a single xml file
18 # The 3 elements Name, Status and Measurment must not be checked during the NamedMeasurment generation loop, that's why they are deleted
19 # The keys and text are formated to be html safe
20 def XMLwriter(resultDic, fileName):
21  if resultDic is not None:
22 
23  #Creating the xml tree
24  try :
25  tree = ET.parse(fileName)
26  except :
27  cleanXml(fileName)
28  tree = ET.parse(fileName)
29  root = tree.getroot()
30 
31  #Test is the root
32  Test = ET.Element('Test')
33  Test.set('Status',resultDic['Status'])
34  del resultDic['Status']
35  Name = ET.SubElement(Test,'Name')
36  Name.text= XSS.escape(resultDic['Name'][:-4])
37  del resultDic['Name']
38 
39  # Branch containing all the measurments
40  Results = ET.SubElement(Test,'Results')
41 
42  NamedMeasurement = dict()
43  Value = dict()
44 
45  # Storing the measurments in dictionnaries, if not a String type, add an "if" condition
46  for key in resultDic :
47  if resultDic[key] !='' and key!='Measurement':
48  NamedMeasurement[key] = ET.SubElement(Results,'NamedMeasurement')
49  NamedMeasurement[key].set('name',key)
50 
51  # Setting type
52  if key=="Execution Time" : NamedMeasurement[key].set('type',"numeric/float")
53  if key=="exit_code" : NamedMeasurement[key].set('type',"numeric/integer")
54  else : NamedMeasurement[key].set('type',"String")
55 
56  # Setting value
57  Value[key] = ET.SubElement(NamedMeasurement[key],'Value')
58  if key=='Environment' :
59  env = resultDic[key]
60  Value[key].text = XSS.escape('\n'.join('{0}={1}'.format(k, env[k]) for k in sorted(env)))
61  elif key=='Causes':
62  Value[key].text = XSS.escape('\n'.join(resultDic[key]))
63  elif key=='Validator results':
64  valres= resultDic[key]
65  Value[key].text = XSS.escape('\n'.join('{0}={1}'.format(k, valres[k]) for k in valres)).encode("ascii", "xmlcharrefreplace")
66  elif key=='Unsupported platforms':
67  Value[key].text = XSS.escape('\n'.join(resultDic[key]))
68  else :
69  Value[key].text = XSS.escape(str(resultDic[key]))
70 
71 
72 
73  Measurement = ET.SubElement(Results,'Measurement')
74  Value['Measurement'] = ET.SubElement(Measurement,'Value')
75  Value['Measurement'].text = XSS.escape(resultDic['Measurement'])
76 
77 
78 
79  root.append(Test)
80  tree.write(fileName,encoding='utf-8')
81 
82 
83 #----------------------------------------------------------------------------------------#
84 
85 def cleanXml(xmlFileName):
86  """
87  Removes xml illegal characters from a file.
88  @param xmlFileName: The name of the xml file.
89  """
90  _illegal_xml_chars_Re = re.compile(u'[\x00-\x08\x0b\x0c\x0e-\x1F\uD800-\uDFFF\uFFFE\uFFFF]')
91  xmlFile = open(xmlFileName,'r')
92  data = xmlFile.read()
93  xmlFile.close()
94  xmlFile = open(xmlFileName,'w')
95  xmlFile.write(_illegal_xml_chars_Re.sub("", data))
96  xmlFile.close()
97 
98 #-------------------------------------------------------------------------#
99 
100 def main(fileList):
101  if not os.path.exists("./results"):
102  os.makedirs("./results")
103  DOB = time.localtime()
104  dateOfBegining = str(DOB[1])+"-"+str(DOB[2])+"-"+str(DOB[0])+"_"+str(DOB[3])+":"+str(DOB[4])
105  # Preparing the result file
106  nameResult = "./results/results_"+dateOfBegining+"_"+str(len(fileList))+".xml"
107  file = open(nameResult,"w+")
108  file.close()
109  Doc = ET.Element('Doc')
110  tree = ET.ElementTree(Doc)
111  tree.write(nameResult)
112 
113 
114 
115  #fileList=["/afs/cern.ch/user/v/valentin/workspace/Gaudi/GaudiExamples/tests/qmtest/gaudiexamples.qms/event_timeout_abort.qmt"]
116 
117  #fileList = ['./gaudiexamples.qms/root_io.qms/read.qmt']
118 
119  # Testing the file begining with "Test" or if it is a qmt file and doing the test
120  for file in fileList :
121  if file.endswith('_test.py') :
122  indexFilePart= file.rfind("/")
123  fileToImport = file[indexFilePart+1:]
124  sys.path.append(GT.RationalizePath(file)[:-len(fileToImport)-1])
125  imp = __import__(fileToImport[:-3])
126  fileToExec = imp.Test()
127  XMLwriter(fileToExec.runTest(),nameResult)
128  if file.endswith(".qmt"):
129  fileToTest = QT.QMTTest()
130  fileToTest.XMLParser(file)
131  XMLwriter(fileToTest.runTest(),nameResult)
132  cleanXml(nameResult)
133 
134 main(sys.argv)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133