Fixed bug where PackageKit downloaded the wrong architecture
[zeroinstall.git] / zeroinstall / support / xmltools.py
bloba7a42a1a693364f6eef90421f81a8705d4a27ff8
1 """Convenience functions for handling XML.
2 @since: 1.9
3 """
5 # Copyright (C) 2012, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from xml.dom import Node
10 def _compare_children(a, b):
11 ac = a.childNodes
12 bc = b.childNodes
14 if ac.length != bc.length:
15 return False
17 for i in range(ac.length):
18 if not nodes_equal(ac[i], bc[i]):
19 return False
21 return True
23 def nodes_equal(a, b):
24 """Compare two DOM nodes.
25 Warning: only supports documents containing elements, text nodes and attributes (will crash on comments, etc).
26 """
27 if a.nodeType != b.nodeType:
28 return False
30 if a.nodeType == Node.ELEMENT_NODE:
31 if a.namespaceURI != b.namespaceURI:
32 return False
34 if a.nodeName != b.nodeName:
35 return False
37 a_attrs = set([(name, value) for name, value in a.attributes.itemsNS()])
38 b_attrs = set([(name, value) for name, value in b.attributes.itemsNS()])
40 if a_attrs != b_attrs:
41 #print "%s != %s" % (a_attrs, b_attrs)
42 return False
44 return _compare_children(a, b)
45 elif a.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE):
46 return a.wholeText == b.wholeText
47 elif a.nodeType == Node.DOCUMENT_NODE:
48 return _compare_children(a, b)
49 else:
50 assert 0, ("Unknown node type", a)