When merging, check the IDs are unique
[0publish.git] / merge.py
blob33ca9c1905d496a92c099abc65f11afce4b40015
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 = {} # (name, version-expr) -> <command>
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'), x.getAttribute('if-0install-version'))
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_expr, g_command in group.commands.iteritems():
96 if name_expr not in impl.commands:
97 return (0,) # Group sets a command the impl doesn't want
98 if nodesEqual(g_command, impl.commands[name_expr]):
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 known_ids = set()
116 def check_unique(elem):
117 impl_id = impl.getAttribute("id")
118 if impl_id in known_ids:
119 raise Exception("Duplicate ID " + impl_id)
120 known_ids.add(impl_id)
122 for impl in find_impls(master_doc.documentElement):
123 check_unique(impl)
125 # Merge each implementation in the local feed in turn (normally there will only be one)
126 for impl in find_impls(local_doc.documentElement):
127 check_unique(impl)
129 # 1. Get the context of the implementation to add. This is:
130 # - The set of its requirements
131 # - The set of its commands
132 # - Its attributes
133 new_impl_context = Context(impl)
135 # 2. For each <group> in the master feed, see if it provides a compatible context:
136 # - A subset of the new implementation's requirements
137 # - A subset of the new implementation's command names
138 # - A subset of the new implementation's attributes (names, not values)
139 # Choose the most compatible <group> (the root counts as a minimally compatible group)
141 best_group = ((1, 0, 0), master_doc.documentElement) # (score, element)
143 for group in find_groups(master_doc.documentElement):
144 group_context = Context(group)
145 score = score_subset(group_context, new_impl_context)
146 if score > best_group[0]:
147 best_group = (score, group)
149 group = best_group[1]
150 group_context = Context(group)
152 new_commands = []
153 for name_expr, new_command in new_impl_context.commands.iteritems():
154 old_command = group_context.commands.get(name_expr, None)
155 if not (old_command and nodesEqual(old_command, new_command)):
156 new_commands.append(master_doc.importNode(new_command, True))
158 # If we have additional requirements, we'll need to create a subgroup and add them
159 if len(new_impl_context.requires) > len(group_context.requires) or new_commands:
160 subgroup = xmltools.create_element(group, 'group')
161 group = subgroup
162 #group_context = Context(group)
163 for x in new_impl_context.requires:
164 for y in group_context.requires:
165 if nodesEqual(x, y): break
166 else:
167 req = master_doc.importNode(x, True)
168 #print "Add", req
169 xmltools.insert_element(req, group)
170 for c in new_commands:
171 xmltools.insert_element(c, group)
173 new_impl = master_doc.importNode(impl, True)
175 # Attributes might have been set on a parent group; move to the impl
176 for name in new_impl_context.attribs:
177 #print "Set", name, value
178 xmltools.add_attribute_ns(new_impl, name[0], name[1], new_impl_context.attribs[name])
180 for name, value in new_impl.attributes.itemsNS():
181 if name[0] == XMLNS_NAMESPACE or \
182 (name in group_context.attribs and group_context.attribs[name] == value):
183 #print "Deleting duplicate attribute", name, value
184 new_impl.removeAttributeNS(name[0], name[1])
186 xmltools.insert_element(new_impl, group)
188 return master_doc.toxml('utf-8')