1 from xml
.dom
import minidom
, Node
2 from zeroinstall
.injector
import namespaces
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('utf-8')
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 new_impl
= doc
.createElementNS(namespaces
.XMLNS_IFACE
, 'implementation')
18 new_impl
.setAttribute('version', version
)
19 new_impl
.setAttribute('id', '.')
22 print 'No existing <implementation> elements found; creating first one!'
24 last_impl
= doc
.documentElement
.childNodes
[-1]
26 last_impl
= all_impls
[-1]
27 previous
= last_impl
.previousSibling
28 next
= last_impl
.nextSibling
29 parent
= last_impl
.parentNode
30 if previous
and previous
.nodeType
== Node
.TEXT_NODE
:
31 parent
.insertBefore(doc
.createTextNode(previous
.nodeValue
), next
)
32 parent
.insertBefore(new_impl
, next
)
33 return doc
.toxml('utf-8')
35 def set_attributes(data
, selected_version
, id = None, version
= None, released
= None, stability
= None, main
= None, arch
= None):
36 """Normally there's only one implementation, but we can cope with several."""
37 if released
== 'today': released
= time
.strftime('%Y-%m-%d')
38 if released
and released
!= 'Snapshot' and not re
.match(date_format
, released
):
39 raise Exception('Invalid date format. Use YYYY-MM-DD.')
41 doc
= minidom
.parseString(data
)
42 all_impls
= doc
.documentElement
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'implementation')
44 if selected_version
== None:
45 # Select the version with no "released" attribute
48 _released
= x
.getAttribute('released')
49 if released
is None or not re
.match(date_format
, _released
):
51 raise Exception('Multiple unreleased implementations!')
53 if unreleased
is None:
54 if len(all_impls
) == 0:
55 raise Exception('No implementations in feed file!')
56 if len(all_impls
) > 1:
57 raise Exception("Multiple implementations, but all are released. Aborting.")
58 unreleased
= all_impls
[0]
59 versions_to_change
= [unreleased
]
61 # Select the version the user asked for
62 versions_to_change
= []
66 _version
= node
.getAttribute('version')
67 if _version
is not None: break
68 node
= node
.getParentNode()
69 assert node
.localName
== 'group'
70 if _version
== selected_version
:
71 versions_to_change
.append(x
)
72 if not versions_to_change
:
73 if len(all_impls
) == 0:
74 raise Exception('No implementations in feed file!')
75 raise Exception("No implementations with version=%s" % selected_version
)
77 # In future, we may want to bulk change implementations...
78 for x
in versions_to_change
:
79 if id is not None: x
.setAttribute('id', id)
80 if released
is not None:
82 x
.setAttribute('released', released
)
83 elif x
.hasAttribute('released'):
84 x
.removeAttribute('released')
85 if stability
is not None: x
.setAttribute('stability', stability
)
86 if main
is not None: x
.setAttribute('main', main
)
87 if arch
is not None: x
.setAttribute('arch', arch
)
88 if version
is not None:
89 x
.setAttribute('version', version
)
90 if x
.hasAttribute('version-modifier'):
91 x
.removeAttribute('version-modifier')
93 return doc
.toxml('utf-8')