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 add_version(data
, version
):
8 """Create a new <implementation> after the last one in the file."""
9 doc
= minidom
.parseString(data
)
10 all_impls
= doc
.documentElement
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'implementation')
12 raise Exception('No existing <implementation> elements found!')
13 new_impl
= doc
.createElementNS(namespaces
.XMLNS_IFACE
, 'implementation')
14 new_impl
.setAttribute('version', version
)
15 new_impl
.setAttribute('id', '.')
17 last_impl
= all_impls
[-1]
18 previous
= last_impl
.previousSibling
19 next
= last_impl
.nextSibling
20 parent
= last_impl
.parentNode
21 if previous
and previous
.nodeType
== Node
.TEXT_NODE
:
22 parent
.insertBefore(doc
.createTextNode(previous
.nodeValue
), next
)
23 parent
.insertBefore(new_impl
, next
)
26 def make_release(data
, id, version
, released
, stability
, main
, arch
):
27 """Normally there's only one implementation, but we can cope with several."""
28 if released
== 'today': released
= time
.strftime('%Y-%m-%d')
29 if released
and released
!= 'Snapshot' and not re
.match(date_format
, released
):
30 raise Exception('Invalid date format. Use YYYY-MM-DD.')
32 doc
= minidom
.parseString(data
)
34 all_impls
= doc
.documentElement
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'implementation')
36 _released
= x
.getAttribute('released')
37 if released
is None or not re
.match(date_format
, _released
):
39 raise Exception('Multiple unreleased implementations!')
41 if unreleased
is None:
42 if len(all_impls
) == 0:
43 raise Exception('No implementations in interface file!')
44 if len(all_impls
) > 1:
45 raise Exception("Multiple implementations, but all are released. Aborting.")
46 unreleased
= all_impls
[0]
48 # In future, we may want to bulk change implementations...
49 for x
in [unreleased
]:
50 if id is not None: x
.setAttribute('id', id)
51 if version
is not None: x
.setAttribute('version', version
)
52 if released
is not None: x
.setAttribute('released', released
)
53 if stability
is not None: x
.setAttribute('stability', stability
)
54 if main
is not None: x
.setAttribute('main', main
)
55 if arch
is not None: x
.setAttribute('arch', arch
)