Added --select-version option to say which implementations you want to change
[0publish.git] / release.py
blobcc72a08c992cf6169729e8d7ef87ddc023110f22
1 from xml.dom import minidom, Node
2 from zeroinstall.injector import namespaces
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 set_attributes(data, selected_version, id = None, version = None, released = None, stability = None, main = None, arch = None):
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 all_impls = doc.documentElement.getElementsByTagNameNS(namespaces.XMLNS_IFACE, 'implementation')
41 if selected_version == None:
42 # Select the version with no "released" attribute
43 unreleased = None
44 for x in all_impls:
45 _released = x.getAttribute('released')
46 if released is None or not re.match(date_format, _released):
47 if unreleased:
48 raise Exception('Multiple unreleased implementations!')
49 unreleased = x
50 if unreleased is None:
51 if len(all_impls) == 0:
52 raise Exception('No implementations in feed file!')
53 if len(all_impls) > 1:
54 raise Exception("Multiple implementations, but all are released. Aborting.")
55 unreleased = all_impls[0]
56 versions_to_change = [unreleased]
57 else:
58 # Select the version the user asked for
59 versions_to_change = []
60 for x in all_impls:
61 node = x
62 while True:
63 _version = node.getAttribute('version')
64 if _version is not None: break
65 node = node.getParentNode()
66 assert node.localName == 'group'
67 if _version == selected_version:
68 versions_to_change.append(x)
69 if not versions_to_change:
70 if len(all_impls) == 0:
71 raise Exception('No implementations in feed file!')
72 raise Exception("No implementations with version=%s" % selected_version)
74 # In future, we may want to bulk change implementations...
75 for x in versions_to_change:
76 if id is not None: x.setAttribute('id', id)
77 if released is not None:
78 if released:
79 x.setAttribute('released', released)
80 elif x.hasAttribute('released'):
81 x.removeAttribute('released')
82 if stability is not None: x.setAttribute('stability', stability)
83 if main is not None: x.setAttribute('main', main)
84 if arch is not None: x.setAttribute('arch', arch)
85 if version is not None:
86 x.setAttribute('version', version)
87 if x.hasAttribute('version-modifier'):
88 x.removeAttribute('version-modifier')
90 return doc.toxml()