Package zeroinstall :: Package injector :: Module writer
[frames] | no frames]

Source Code for Module zeroinstall.injector.writer

 1  """ 
 2  Save per-interface and per-feed configuration information. 
 3  """ 
 4   
 5  # Copyright (C) 2009, Thomas Leonard 
 6  # See the README file for details, or visit http://0install.net. 
 7   
 8  import os 
 9  from xml.dom import minidom, XMLNS_NAMESPACE 
10   
11  from zeroinstall import support 
12  from zeroinstall.support import basedir 
13   
14  from zeroinstall.injector import model 
15  from zeroinstall.injector.namespaces import config_site, config_prog, XMLNS_IFACE 
16   
17 -def _atomic_save(doc, parent, uri):
18 """@type parent: str 19 @type uri: str""" 20 import tempfile 21 tmp_fd, tmp_name = tempfile.mkstemp(dir = parent) 22 try: 23 tmp_file = os.fdopen(tmp_fd, 'w') 24 doc.writexml(tmp_file, addindent = " ", newl = '\n') 25 tmp_file.close() 26 path = os.path.join(parent, model._pretty_escape(uri)) 27 support.portable_rename(tmp_name, path) 28 except: 29 os.unlink(tmp_name) 30 raise
31
32 -def save_feed(feed):
33 """Save information about a feed. Currently, this is the last_checked time and any user-set stability ratings. 34 @type feed: L{zeroinstall.injector.model.ZeroInstallFeed} 35 @since: 0.49""" 36 feeds = basedir.save_config_path(config_site, config_prog, 'feeds') 37 38 impl = minidom.getDOMImplementation() 39 doc = impl.createDocument(XMLNS_IFACE, 'feed-preferences', None) 40 root = doc.documentElement 41 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE) 42 43 if feed.last_checked: 44 root.setAttribute('last-checked', str(feed.last_checked)) 45 46 impls = list(feed.implementations.values()) 47 impls.sort() 48 for impl in impls: 49 if impl.user_stability: 50 node = doc.createElementNS(XMLNS_IFACE, 'implementation') 51 root.appendChild(node) 52 node.setAttribute('user-stability', str(impl.user_stability)) 53 node.setAttribute('id', impl.id) 54 55 _atomic_save(doc, feeds, feed.url)
56
57 -def save_interface(interface):
58 """@type interface: L{zeroinstall.injector.model.Interface}""" 59 user_overrides = basedir.save_config_path(config_site, config_prog, 'interfaces') 60 61 impl = minidom.getDOMImplementation() 62 doc = impl.createDocument(XMLNS_IFACE, 'interface-preferences', None) 63 64 root = doc.documentElement 65 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE) 66 root.setAttribute('uri', interface.uri) 67 68 if interface.stability_policy: 69 root.setAttribute('stability-policy', str(interface.stability_policy)) 70 71 for feed in interface.extra_feeds: 72 if feed.user_override: 73 elem = doc.createElementNS(XMLNS_IFACE, 'feed') 74 root.appendChild(elem) 75 elem.setAttribute('src', feed.uri) 76 if feed.arch: 77 elem.setAttribute('arch', feed.arch) 78 if feed.site_package: 79 elem.setAttribute('is-site-package', 'True') 80 81 _atomic_save(doc, user_overrides, interface.uri)
82