Commit fe64e381 authored by Nicolas Delaby's avatar Nicolas Delaby

Use better algorithm to compare two xml nodes.

parent 68726ed4
......@@ -40,13 +40,17 @@ import zope.interface
class FileContentNotEqual(Exception):
pass
def fileComparisonIterator(file1, file2):
value1 = file1.next()
value2 = file2.next()
if value1 == value2:
yield value1, value2
else:
raise FileContentNotEqual
def isNodeEquals(old, new):
if old.tag != new.tag or old.attrib != new.attrib:
return False
if old.text != new.text or old.tail != new.tail:
return False
if len(old) != len(new):
return False
for old_child, new_child in zip(old, new):
if not isNodeEquals(old_child, new_child):
return False
return True
class ERP5Diff:
"""
......@@ -481,27 +485,12 @@ class ERP5Diff:
for old_index, old_element in enumerate(old_list):
if old_element not in old_candidate_list:
continue
old_tree = deepcopy(old_element).getroottree()
old_c14n = StringIO()
old_tree.write_c14n(old_c14n)
old_c14n.seek(0)
for new_element in new_list:
new_index = new_list.index(new_element)
if new_element not in new_candidate_list:
continue
new_tree = deepcopy(new_element).getroottree()
new_c14n = StringIO()
new_tree.write_c14n(new_c14n)
new_c14n.seek(0)
file_equality = True
try:
#Use generator to avoid reading file entirely
#Stop iteration at first difference
list(fileComparisonIterator(old_c14n, new_c14n))
except FileContentNotEqual:
file_equality = False
old_c14n.seek(0)
if file_equality:
node_equality = isNodeEquals(old_element, new_element)
if node_equality:
index_key_on_new_tree = new_element.getparent().index(new_element)
old_new_index_mapping[index_key_on_new_tree] = old_element
new_start = new_index + 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment