1 from xml
.dom
import minidom
, XMLNS_NAMESPACE
, Node
2 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
5 def childNodes(parent
, namespaceURI
= None, localName
= None):
6 for x
in parent
.childNodes
:
7 if x
.nodeType
!= Node
.ELEMENT_NODE
: continue
8 if namespaceURI
is not None and x
.namespaceURI
!= namespaceURI
: continue
10 if localName
is None or x
.localName
== localName
:
14 def __init__(self
, impl
):
15 doc
= impl
.ownerDocument
21 for name
, value
in node
.attributes
.itemsNS():
22 if name
[0] == XMLNS_NAMESPACE
:
23 xmltools
.register_namespace(value
, name
[1])
24 elif name
not in self
.attribs
:
25 self
.attribs
[name
] = value
26 if node
.nodeName
== 'group':
27 # We don't care about <requires> inside <implementation>; they'll get copied over anyway
28 for x
in childNodes(node
, XMLNS_IFACE
, 'requires'):
29 self
.requires
.append(x
)
30 node
= node
.parentNode
31 if node
.nodeName
!= 'group':
34 def find_impls(parent
):
35 """Return all <implementation> children, including those inside groups."""
36 for x
in childNodes(parent
, XMLNS_IFACE
):
37 if x
.localName
== 'implementation':
39 elif x
.localName
== 'group':
40 for y
in find_impls(x
):
43 def find_groups(parent
):
44 """Return all <group> children, including those inside other groups."""
45 for x
in childNodes(parent
, XMLNS_IFACE
, 'group'):
47 for y
in find_groups(x
):
51 assert a
.nodeType
== Node
.ELEMENT_NODE
52 assert b
.nodeType
== Node
.ELEMENT_NODE
54 if a
.namespaceURI
!= b
.namespaceURI
:
57 if a
.nodeName
!= b
.nodeName
:
60 a_attrs
= set(["%s %s" % (name
, value
) for name
, value
in a
.attributes
.itemsNS()])
61 b_attrs
= set(["%s %s" % (name
, value
) for name
, value
in b
.attributes
.itemsNS()])
63 if a_attrs
!= b_attrs
:
64 #print "%s != %s" % (a_attrs, b_attrs)
67 a_children
= list(childNodes(a
))
68 b_children
= list(childNodes(b
))
70 if len(a_children
) != len(b_children
):
73 for a_child
, b_child
in zip(a_children
, b_children
):
74 if not nodesEqual(a_child
, b_child
):
79 def score_subset(group
, impl
):
80 """Returns (is_subset, goodness)"""
81 for key
in group
.attribs
:
82 if key
not in impl
.attribs
.keys():
84 return (0,) # Group sets an attribute the impl doesn't want
85 for g_req
in group
.requires
:
86 for i_req
in impl
.requires
:
87 if nodesEqual(g_req
, i_req
): break
89 return (0,) # Group adds a requires that the impl doesn't want
90 # Score result so we get groups that have all the same requires first, then ones with all the same attribs
91 return (1, len(group
.requires
), len(group
.attribs
))
93 # Note: the namespace stuff isn't quite right yet.
94 # Might get conflicts if both documents use the same prefix for different things.
95 def merge(data
, local
):
96 local_doc
= minidom
.parse(local
)
97 master_doc
= minidom
.parseString(data
)
99 # Merge each implementation in the local feed in turn (normally there will only be one)
100 for impl
in find_impls(local_doc
.documentElement
):
101 # 1. Get the context of the implementation to add. This is:
102 # - The set of its requirements
104 new_impl_context
= Context(impl
)
106 # 2. For each <group> in the master feed, see if it provides a compatible context:
107 # - A subset of the new implementation's requirements
108 # - A subset of the new implementation's attributes (names, not values)
109 # Choose the most compatible <group> (the root counts as a minimally compatible group)
111 best_group
= ((1, 0, 0), master_doc
.documentElement
) # (score, element)
113 for group
in find_groups(master_doc
.documentElement
):
114 group_context
= Context(group
)
115 score
= score_subset(group_context
, new_impl_context
)
116 if score
> best_group
[0]:
117 best_group
= (score
, group
)
119 group
= best_group
[1]
120 group_context
= Context(group
)
122 # If we have additional requirements, we'll need to create a subgroup and add them
123 if len(new_impl_context
.requires
) > len(group_context
.requires
):
124 subgroup
= xmltools
.create_element(group
, 'group')
126 group_context
= Context(group
)
127 for x
in new_impl_context
.requires
:
128 for y
in group_context
.requires
:
129 if nodesEqual(x
, y
): break
131 req
= master_doc
.importNode(x
, True)
133 xmltools
.insert_element(req
, group
)
135 new_impl
= master_doc
.importNode(impl
, True)
137 # Attributes might have been set on a parent group; move to the impl
138 for name
in new_impl_context
.attribs
:
139 #print "Set", name, value
140 xmltools
.add_attribute_ns(new_impl
, name
[0], name
[1], new_impl_context
.attribs
[name
])
142 for name
, value
in new_impl
.attributes
.itemsNS():
143 if name
[0] == XMLNS_NAMESPACE
or \
144 (name
in group_context
.attribs
and group_context
.attribs
[name
] == value
):
145 #print "Deleting duplicate attribute", name, value
146 new_impl
.removeAttributeNS(name
[0], name
[1])
148 xmltools
.insert_element(new_impl
, group
)
150 return master_doc
.toxml()