Added --set-interface-uri to set the main URI.
[0publish.git] / release.py
blobaeaa1b90b41b86ac0c0d8e093eae7c4adf216bb6
1 from xml.dom import minidom, Node
2 from zeroinstall.injector import namespaces, model
3 import time, re
5 date_format = '\d{4}-\d{2}-\d{2}'
7 def set_interface_uri(data, uri):
8 """Set the uri attribute on the root element."""
9 doc = minidom.parseString(data)
10 doc.documentElement.setAttribute('uri', uri)
11 return doc.toxml()
13 def add_version(data, version):
14 """Create a new <implementation> after the last one in the file."""
15 doc = minidom.parseString(data)
16 all_impls = doc.documentElement.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation')
17 if not all_impls:
18 raise Exception('No existing <implementation> elements found!')
19 new_impl = doc.createElementNS(namespaces.XMLNS_IFACE, 'implementation')
20 new_impl.setAttribute('version', version)
21 new_impl.setAttribute('id', '.')
23 last_impl = all_impls[-1]
24 previous = last_impl.previousSibling
25 next = last_impl.nextSibling
26 parent = last_impl.parentNode
27 if previous and previous.nodeType == Node.TEXT_NODE:
28 parent.insertBefore(doc.createTextNode(previous.nodeValue), next)
29 parent.insertBefore(new_impl, next)
30 return doc.toxml()
32 def make_release(data, id, version, released, stability, main, arch):
33 """Normally there's only one implementation, but we can cope with several."""
34 if released == 'today': released = time.strftime('%Y-%m-%d')
35 if released and released != 'Snapshot' and not re.match(date_format, released):
36 raise Exception('Invalid date format. Use YYYY-MM-DD.')
38 doc = minidom.parseString(data)
39 unreleased = None
40 all_impls = doc.documentElement.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation')
41 for x in all_impls:
42 _released = x.getAttribute('released')
43 if released is None or not re.match(date_format, _released):
44 if unreleased:
45 raise Exception('Multiple unreleased implementations!')
46 unreleased = x
47 if unreleased is None:
48 if len(all_impls) == 0:
49 raise Exception('No implementations in interface file!')
50 if len(all_impls) > 1:
51 raise Exception("Multiple implementations, but all are released. Aborting.")
52 unreleased = all_impls[0]
54 # In future, we may want to bulk change implementations...
55 for x in [unreleased]:
56 if id is not None: x.setAttribute('id', id)
57 if version is not None: x.setAttribute('version', version)
58 if released is not None: x.setAttribute('released', released)
59 if stability is not None: x.setAttribute('stability', stability)
60 if main is not None: x.setAttribute('main', main)
61 if arch is not None: x.setAttribute('arch', arch)
63 return doc.toxml()