1 from xml
.dom
import minidom
, Node
2 from zeroinstall
.injector
import namespaces
, model
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
)
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')
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
)
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
)
40 all_impls
= doc
.documentElement
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'implementation')
42 _released
= x
.getAttribute('released')
43 if released
is None or not re
.match(date_format
, _released
):
45 raise Exception('Multiple unreleased implementations!')
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 released
is not None:
59 x
.setAttribute('released', released
)
60 elif x
.hasAttribute('released'):
61 x
.removeAttribute('released')
62 if stability
is not None: x
.setAttribute('stability', stability
)
63 if main
is not None: x
.setAttribute('main', main
)
64 if arch
is not None: x
.setAttribute('arch', arch
)
65 if version
is not None:
66 x
.setAttribute('version', version
)
67 if x
.hasAttribute('version-modifier'):
68 x
.removeAttribute('version-modifier')