4 from pprint
import pprint
5 from copy
import deepcopy
7 from xml.etree.ElementTree
import ElementTree
10 """ parse file and return XML tree """
17 """ return a dict of all processors and their parameters """
19 procElements = tree.findall(
'processor')
20 for proc
in procElements:
21 procName = proc.attrib.get(
"name")
23 parameters = proc.findall(
'parameter')
24 for param
in parameters:
25 paramName = param.attrib.get(
'name')
26 paramDict[paramName] =
getValue( param ,
"<This Value did not Exist in this file or was empty>" )
27 processors[procName] = paramDict
29 groupElements = tree.findall(
'group')
30 for group
in groupElements:
32 parameters = group.findall(
'parameter')
33 for param
in parameters:
34 paramName = param.attrib.get(
'name')
35 groupParam[paramName] =
getValue( param ,
"<This Value did not Exist in this file or was empty>" )
37 procElements = group.findall(
"processor")
38 for proc
in procElements:
39 procName = proc.attrib.get(
"name")
40 paramDict = deepcopy(groupParam)
41 parameters = proc.findall(
'parameter')
42 for param
in parameters:
43 paramName = param.attrib.get(
'name')
44 paramDict[paramName] =
getValue( param ,
"<This Value did not Exist in this file or was empty>" )
45 processors[procName] = paramDict
52 if element.get(
'value'):
53 return element.get(
'value').strip()
55 return element.text.strip()
60 """return dict of global parameters and values """
62 for param
in globalElements:
63 name = param.get(
'name')
70 """ compare the global parameters in the marlin steering files """
74 parCopy1 = deepcopy(par1)
75 parCopy2 = deepcopy(par2)
77 for name1, value1
in par1.iteritems():
78 if name1
in par2
and value1 == par2.get(name1,
"<Unknown Parameter>"):
83 print "\n++++ GlobalParameters that exist in the first file that are different or non-existent in the second file"
85 print "\n++++ GlobalParameters that exist in the second file that are different or non-existent in the first file"
90 """ compare the list of processors to execute, order matters """
91 exec1 = tree1.findall(
'execute/processor')
92 exec2 = tree2.findall(
'execute/processor')
93 if len(exec1) != len(exec2):
94 print "Mismatch in number of executing processors!", len(exec1),
"vs", len(exec2)
96 for index, proc1
in enumerate( exec1 ):
97 if index >= len(exec2):
98 print "More processors in first file than in second"
101 name1 = proc1.get(
'name')
102 name2 = proc2.get(
'name')
104 print "Difference in executing processors", name1,
"does not match processor", name2
105 print "Check the order of processor execution!"
109 """ compare the list of groups to execute, order matters """
110 exec1 = tree1.findall(
'execute/group')
111 exec2 = tree2.findall(
'execute/group')
112 if len(exec1) != len(exec2):
113 print "Mismatch in number of executing groups!", len(exec1),
"vs", len(exec2)
115 for index, grp1
in enumerate( exec1 ):
116 if index >= len(exec2):
117 print "More groups in first file than in second"
120 name1 = grp1.get(
'name')
121 name2 = grp2.get(
'name')
123 print "Difference in executing groups", name1,
"does not match group", name2
124 print "Check the order of group execution!"
128 """ keep only those processors we want to compare """
129 if not selectedProcessors:
132 for processor
in processors.keys():
134 for procString
in selectedProcessors:
135 if procString.lower()
in processor.lower():
140 del processors[processor]
143 """compare the content of the two xml trees, as these are marlin steering
144 files we only look for processors, get their parameters and then compare the
145 value of all the processor parameters
156 procCopy1 = deepcopy(processors1)
157 procCopy2 = deepcopy(processors2)
159 for proc1, parameters1
in processors1.iteritems():
160 if proc1
in processors2:
161 parameters2 = processors2[ proc1 ]
163 for param1, value1
in parameters1.iteritems():
164 if param1
in parameters2:
165 value2 = parameters2[param1].strip()
166 if value2 == value1.strip():
167 del procCopy1[proc1][param1]
168 del procCopy2[proc1][param1]
171 if not procCopy1.get(proc1):
174 if not procCopy2.get(proc1,
True):
177 print "\n++++ Entries that exist in the first file that are different or non-existent in the second file"
179 print "\n++++ Entries that exist in the second file that are different or non-existent in the first file"
186 print "incorrect number of input files, need two marlin steering files as argument and optionally select processors to compare"
187 print "compareMarlinSteeringFiles.py file1.xml file2.xml [processor1] [...]"
193 selectedProcessors = args[3:]
194 except Exception
as ex:
195 print "Exception when getting trees: %r " % ex
198 print "\n++++ Start comparing"
206 print "\n++++ Done comparing"
209 if __name__ ==
"__main__":
def compareGlobalParameters
def compareExecutingProcessors
def compareExecutingGroups