2 from xml
.dom
import minidom
, XMLNS_NAMESPACE
, Node
3 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
4 from zeroinstall
.injector
import model
, reader
5 from logging
import info
8 def childNodes(parent
, namespaceURI
, localName
= None):
9 for x
in parent
.childNodes
:
10 if x
.nodeType
!= Node
.ELEMENT_NODE
: continue
11 if x
.namespaceURI
!= namespaceURI
: continue
13 if localName
is None or x
.localName
== localName
:
17 def __init__(self
, impl
):
18 doc
= impl
.ownerDocument
24 for name
, value
in node
.attributes
.itemsNS():
25 if name
not in self
.attribs
:
26 self
.attribs
[name
] = value
27 if node
.nodeName
== 'group':
28 # We don't care about <requires> inside <implementation>; they'll get copied over anyway
29 for x
in childNodes(node
, XMLNS_IFACE
, 'requires'):
30 self
.requires
.append(x
)
31 node
= node
.parentNode
32 if node
.nodeName
!= 'group':
35 def find_impls(parent
):
36 """Return all <implementation> children, including those inside groups."""
37 for x
in childNodes(parent
, XMLNS_IFACE
):
38 if x
.localName
== 'implementation':
40 elif x
.localName
== 'group':
41 for y
in find_impls(x
):
44 def find_groups(parent
):
45 """Return all <group> children, including those inside other groups."""
46 for x
in childNodes(parent
, XMLNS_IFACE
, 'group'):
48 for y
in find_groups(x
):
51 def is_subset(group
, impl
):
52 for key
in group
.attribs
:
53 if key
not in impl
.attribs
.keys():
55 return False # Group sets an attribute the impl doesn't want
56 for g_req
in group
.requires
:
57 for i_req
in impl
.requires
:
58 if nodesEqual(g_req
, i_req
): break
60 return False # Group adds a requires that the impl doesn't want
63 def merge(data
, local
):
64 local_doc
= minidom
.parse(local
)
65 master_doc
= minidom
.parseString(data
)
67 # Merge each implementation in the local feed in turn (normally there will only be one)
68 for impl
in find_impls(local_doc
.documentElement
):
69 # 1. Get the context of the implementation to add. This is:
70 # - The set of its requirements
72 new_impl_context
= Context(impl
)
74 # 2. For each <group> in the master feed, see if it provides a compatible context:
75 # - A subset of the new implementation's requirements
76 # - A subset of the new implementation's attributes (names, not values)
77 # Choose the most compatible <group> (the root counts as a minimally compatible group)
79 best_group
= (0, master_doc
.documentElement
)
81 for group
in find_groups(master_doc
.documentElement
):
82 group_context
= Context(group
)
83 if is_subset(group_context
, new_impl_context
):
84 best_group
= (0, group
)
87 group_context
= Context(group
)
89 # If we have additional requirements, we'll need to create a subgroup and add them
90 if len(new_impl_context
.requires
) > len(group_context
.requires
):
91 subgroup
= xmltools
.create_element(group
, 'group')
93 group_context
= Context(group
)
94 for x
in new_impl_context
.requires
:
95 for y
in group_context
.requires
:
96 if nodesEqual(x
, y
): break
98 req
= master_doc
.importNode(x
, True)
100 xmltools
.insert_element(req
, group
)
102 new_impl
= master_doc
.importNode(impl
, True)
103 for name
, value
in new_impl
.attributes
.itemsNS():
104 if name
in group_context
.attribs
and group_context
.attribs
[name
] == value
:
105 #print "Deleting duplicate attribute", name, value
106 new_impl
.removeAttributeNS(name
[0], name
[1])
108 # Might have been on a parent group; move to the impl
109 #print "Set", name, value
110 new_impl
.setAttributeNS(name
[0], name
[1], value
)
112 xmltools
.insert_element(new_impl
, group
)
114 return master_doc
.toxml()