Only skip <environment insert> bindings for package implementations
[zeroinstall.git] / zeroinstall / injector / writer.py
blob5d62dd0fd8d16787fda8bf266696730cdc86335f
1 """
2 Save per-interface and per-feed configuration information.
3 """
5 # Copyright (C) 2009, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 import os
9 from xml.dom import minidom, XMLNS_NAMESPACE
11 from zeroinstall.support import basedir
13 from zeroinstall.injector import model
14 from zeroinstall.injector.namespaces import config_site, config_prog, XMLNS_IFACE
16 def _atomic_save(doc, parent, uri):
17 import tempfile
18 tmp_fd, tmp_name = tempfile.mkstemp(dir = parent)
19 try:
20 tmp_file = os.fdopen(tmp_fd, 'w')
21 doc.writexml(tmp_file, addindent = " ", newl = '\n')
22 tmp_file.close()
23 path = os.path.join(parent, model._pretty_escape(uri))
24 os.rename(tmp_name, path)
25 except:
26 os.unlink(tmp_name)
27 raise
29 def save_feed(feed):
30 """Save information about a feed. Currently, this is the last_checked time and any user-set stability ratings.
31 @since: 0.49"""
32 feeds = basedir.save_config_path(config_site, config_prog, 'feeds')
34 impl = minidom.getDOMImplementation()
35 doc = impl.createDocument(XMLNS_IFACE, 'feed-preferences', None)
36 root = doc.documentElement
37 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE)
39 if feed.last_checked:
40 root.setAttribute('last-checked', str(feed.last_checked))
42 impls = feed.implementations.values()
43 impls.sort()
44 for impl in impls:
45 if impl.user_stability:
46 node = doc.createElementNS(XMLNS_IFACE, 'implementation')
47 root.appendChild(node)
48 node.setAttribute('user-stability', str(impl.user_stability))
49 node.setAttribute('id', impl.id)
51 _atomic_save(doc, feeds, feed.url)
53 def save_interface(interface):
54 user_overrides = basedir.save_config_path(config_site, config_prog, 'interfaces')
56 impl = minidom.getDOMImplementation()
57 doc = impl.createDocument(XMLNS_IFACE, 'interface-preferences', None)
59 root = doc.documentElement
60 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE)
61 root.setAttribute('uri', interface.uri)
63 if interface.stability_policy:
64 root.setAttribute('stability-policy', str(interface.stability_policy))
66 for feed in interface.extra_feeds:
67 if feed.user_override:
68 elem = doc.createElementNS(XMLNS_IFACE, 'feed')
69 root.appendChild(elem)
70 elem.setAttribute('src', feed.uri)
71 if feed.arch:
72 elem.setAttribute('arch', feed.arch)
74 _atomic_save(doc, user_overrides, interface.uri)