Also merge <restricts> elements
[0publish.git] / merge.py
blob7feb550e0a0aefa5b1cce846c1648748193685bb
1 from xml.dom import minidom, XMLNS_NAMESPACE, Node
2 from zeroinstall.injector.namespaces import XMLNS_IFACE
3 import xmltools
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:
11 yield x
13 class Context:
14 def __init__(self, impl):
15 doc = impl.ownerDocument
16 self.attribs = {}
17 self.requires = []
18 self.commands = {}
20 node = impl
21 while True:
22 for name, value in node.attributes.itemsNS():
23 if name[0] == XMLNS_NAMESPACE:
24 xmltools.register_namespace(value, name[1])
25 elif name not in self.attribs:
26 self.attribs[name] = value
27 if node.nodeName == 'group':
28 # We don't care about <requires> or <command> inside <implementation>;
29 # they'll get copied over anyway
30 for x in childNodes(node, XMLNS_IFACE, 'requires'):
31 self.requires.append(x)
32 for x in childNodes(node, XMLNS_IFACE, 'restricts'):
33 self.requires.append(x)
34 for x in childNodes(node, XMLNS_IFACE, 'command'):
35 command_name = x.getAttribute('name')
36 if command_name not in self.commands:
37 self.commands[command_name] = x
38 # (else the existing definition on the child should be used)
39 node = node.parentNode
40 if node.nodeName != 'group':
41 break
43 def find_impls(parent):
44 """Return all <implementation> children, including those inside groups."""
45 for x in childNodes(parent, XMLNS_IFACE):
46 if x.localName == 'implementation':
47 yield x
48 elif x.localName == 'group':
49 for y in find_impls(x):
50 yield y
52 def find_groups(parent):
53 """Return all <group> children, including those inside other groups."""
54 for x in childNodes(parent, XMLNS_IFACE, 'group'):
55 yield x
56 for y in find_groups(x):
57 yield y
59 def nodesEqual(a, b):
60 assert a.nodeType == Node.ELEMENT_NODE
61 assert b.nodeType == Node.ELEMENT_NODE
63 if a.namespaceURI != b.namespaceURI:
64 return False
66 if a.nodeName != b.nodeName:
67 return False
69 a_attrs = set(["%s %s" % (name, value) for name, value in a.attributes.itemsNS()])
70 b_attrs = set(["%s %s" % (name, value) for name, value in b.attributes.itemsNS()])
72 if a_attrs != b_attrs:
73 #print "%s != %s" % (a_attrs, b_attrs)
74 return False
76 a_children = list(childNodes(a))
77 b_children = list(childNodes(b))
79 if len(a_children) != len(b_children):
80 return False
82 for a_child, b_child in zip(a_children, b_children):
83 if not nodesEqual(a_child, b_child):
84 return False
86 return True
88 def score_subset(group, impl):
89 """Returns (is_subset, goodness)"""
90 for key in group.attribs:
91 if key not in impl.attribs.keys():
92 #print "BAD", key
93 return (0,) # Group sets an attribute the impl doesn't want
94 matching_commands = 0
95 for name, g_command in group.commands.iteritems():
96 if name not in impl.commands:
97 return (0,) # Group sets a command the impl doesn't want
98 if nodesEqual(g_command, impl.commands[name]):
99 # Prefer matching commands to overriding them
100 matching_commands += 1
101 for g_req in group.requires:
102 for i_req in impl.requires:
103 if nodesEqual(g_req, i_req): break
104 else:
105 return (0,) # Group adds a requires that the impl doesn't want
106 # Score result so we get groups that have all the same requires/commands first, then ones with all the same attribs
107 return (1, len(group.requires) + len(group.commands), len(group.attribs) + matching_commands)
109 # Note: the namespace stuff isn't quite right yet.
110 # Might get conflicts if both documents use the same prefix for different things.
111 def merge(data, local):
112 local_doc = minidom.parse(local)
113 master_doc = minidom.parseString(data)
115 # Merge each implementation in the local feed in turn (normally there will only be one)
116 for impl in find_impls(local_doc.documentElement):
117 # 1. Get the context of the implementation to add. This is:
118 # - The set of its requirements
119 # - The set of its commands
120 # - Its attributes
121 new_impl_context = Context(impl)
123 # 2. For each <group> in the master feed, see if it provides a compatible context:
124 # - A subset of the new implementation's requirements
125 # - A subset of the new implementation's command names
126 # - A subset of the new implementation's attributes (names, not values)
127 # Choose the most compatible <group> (the root counts as a minimally compatible group)
129 best_group = ((1, 0, 0), master_doc.documentElement) # (score, element)
131 for group in find_groups(master_doc.documentElement):
132 group_context = Context(group)
133 score = score_subset(group_context, new_impl_context)
134 if score > best_group[0]:
135 best_group = (score, group)
137 group = best_group[1]
138 group_context = Context(group)
140 new_commands = []
141 for name, new_command in new_impl_context.commands.iteritems():
142 old_command = group_context.commands.get(name, None)
143 if not (old_command and nodesEqual(old_command, new_command)):
144 new_commands.append(master_doc.importNode(new_command, True))
146 # If we have additional requirements, we'll need to create a subgroup and add them
147 if len(new_impl_context.requires) > len(group_context.requires) or new_commands:
148 subgroup = xmltools.create_element(group, 'group')
149 group = subgroup
150 #group_context = Context(group)
151 for x in new_impl_context.requires:
152 for y in group_context.requires:
153 if nodesEqual(x, y): break
154 else:
155 req = master_doc.importNode(x, True)
156 #print "Add", req
157 xmltools.insert_element(req, group)
158 for c in new_commands:
159 xmltools.insert_element(c, group)
161 new_impl = master_doc.importNode(impl, True)
163 # Attributes might have been set on a parent group; move to the impl
164 for name in new_impl_context.attribs:
165 #print "Set", name, value
166 xmltools.add_attribute_ns(new_impl, name[0], name[1], new_impl_context.attribs[name])
168 for name, value in new_impl.attributes.itemsNS():
169 if name[0] == XMLNS_NAMESPACE or \
170 (name in group_context.attribs and group_context.attribs[name] == value):
171 #print "Deleting duplicate attribute", name, value
172 new_impl.removeAttributeNS(name[0], name[1])
174 xmltools.insert_element(new_impl, group)
176 return master_doc.toxml('utf-8')