Added load_feed_from_cache
[zeroinstall/zeroinstall-afb.git] / zeroinstall / injector / reader.py
blobaf0f76979bafae1b3b08e79f5ee6a9806888721d
1 """
2 Parses an XML interface into a Python representation.
3 """
5 # Copyright (C) 2009, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall import _
9 import os
10 from logging import debug, info, warn
12 from zeroinstall.support import basedir
13 from zeroinstall.injector import qdom, distro
14 from zeroinstall.injector.namespaces import config_site, config_prog, XMLNS_IFACE
15 from zeroinstall.injector.model import Interface, InvalidInterface, ZeroInstallFeed, escape, Feed, stability_levels
16 from zeroinstall.injector import model
18 def update_from_cache(interface):
19 """Read a cached interface and any native feeds or user overrides.
20 @param interface: the interface object to update
21 @type interface: L{model.Interface}
22 @return: True if cached version and user overrides loaded OK.
23 False if upstream not cached. Local interfaces (starting with /) are
24 always considered to be cached, although they are not actually stored in the cache.
25 @rtype: bool"""
26 interface.reset()
27 main_feed = load_feed_from_cache(interface.uri)
28 if main_feed is not None:
29 interface._main_feed = main_feed
31 # Add the distribution package manager's version, if any
32 path = basedir.load_first_data(config_site, 'native_feeds', model._pretty_escape(interface.uri))
33 if path:
34 # Resolve any symlinks
35 info(_("Adding native packager feed '%s'"), path)
36 interface.extra_feeds.append(Feed(os.path.realpath(path), None, False))
38 update_user_overrides(interface, main_feed)
40 return main_feed is not None
42 def load_feed_from_cache(url):
43 """Load a feed. If the feed is remote, load from the cache. If local, load it directly.
44 @return the feed, or None if it's remote and not cached."""
45 if url.startswith('/'):
46 debug(_("Loading local feed file '%s'"), url)
47 return load_feed(url, local = True)
48 else:
49 cached = basedir.load_first_cache(config_site, 'interfaces', escape(url))
50 if cached:
51 debug(_("Loading cached information for %(interface)s from %(cached)s"), {'interface': url, 'cached': cached})
52 return load_feed(cached, local = False)
53 else:
54 return None
56 def update_user_overrides(interface, main_feed = None):
57 """Update an interface with user-supplied information.
58 @param interface: the interface object to update
59 @type interface: L{model.Interface}
60 @param main_feed: feed to update with last_checked information
61 @note: feed updates shouldn't really be here. main_feed may go away in future.
62 """
63 user = basedir.load_first_config(config_site, config_prog,
64 'user_overrides', escape(interface.uri))
65 if not user:
66 return
68 try:
69 root = qdom.parse(file(user))
70 except Exception, ex:
71 warn(_("Error reading '%(user)s': %(exception)s"), {'user': user, 'exception': ex})
72 raise
74 # This is a bit wrong; this information is about the feed,
75 # not the interface.
76 if main_feed:
77 last_checked = root.getAttribute('last-checked')
78 if last_checked:
79 main_feed.last_checked = int(last_checked)
81 stability_policy = root.getAttribute('stability-policy')
82 if stability_policy:
83 interface.set_stability_policy(stability_levels[str(stability_policy)])
85 for item in root.childNodes:
86 if item.uri != XMLNS_IFACE: continue
87 if item.name == 'implementation':
88 id = item.getAttribute('id')
89 assert id is not None
90 if not (id.startswith('/') or id.startswith('.') or id.startswith('package:')):
91 assert '=' in id
92 impl = interface.implementations.get(id, None)
93 if not impl:
94 debug(_("Ignoring user-override for unknown implementation %(id)s in %(interface)s"), {'id': id, 'interface': interface})
95 continue
97 user_stability = item.getAttribute('user-stability')
98 if user_stability:
99 impl.user_stability = stability_levels[str(user_stability)]
100 elif item.name == 'feed':
101 feed_src = item.getAttribute('src')
102 if not feed_src:
103 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
104 interface.extra_feeds.append(Feed(feed_src, item.getAttribute('arch'), True, langs = item.getAttribute('langs')))
106 def check_readable(interface_uri, source):
107 """Test whether an interface file is valid.
108 @param interface_uri: the interface's URI
109 @type interface_uri: str
110 @param source: the name of the file to test
111 @type source: str
112 @return: the modification time in src (usually just the mtime of the file)
113 @rtype: int
114 @raise InvalidInterface: If the source's syntax is incorrect,
116 tmp = Interface(interface_uri)
117 try:
118 update(tmp, source)
119 except InvalidInterface, ex:
120 info(_("Error loading feed:\n"
121 "Interface URI: %(uri)s\n"
122 "Local file: %(source)s\n"
123 "%(exception)s") %
124 {'uri': interface_uri, 'source': source, 'exception': ex})
125 raise InvalidInterface(_("Error loading feed '%(uri)s':\n\n%(exception)s") % {'uri': interface_uri, 'exception': ex})
126 return tmp.last_modified
128 def update(interface, source, local = False):
129 """Read in information about an interface.
130 @param interface: the interface object to update
131 @type interface: L{model.Interface}
132 @param source: the name of the file to read
133 @type source: str
134 @param local: use file's mtime for last-modified, and uri attribute is ignored
135 @raise InvalidInterface: if the source's syntax is incorrect
136 @return: the new feed (since 0.32)
137 @see: L{update_from_cache}, which calls this"""
138 assert isinstance(interface, Interface)
140 feed = load_feed(source, local)
142 if not local:
143 if feed.url != interface.uri:
144 raise InvalidInterface(_("Incorrect URL used for feed.\n\n"
145 "%(feed_url)s is given in the feed, but\n"
146 "%(interface_uri)s was requested") %
147 {'feed_url': feed.url, 'interface_uri': interface.uri})
149 interface._main_feed = feed
150 return feed
152 def load_feed(source, local = False):
153 """Load a feed from a local file.
154 @param source: the name of the file to read
155 @type source: str
156 @param local: this is a local feed
157 @type local: bool
158 @raise InvalidInterface: if the source's syntax is incorrect
159 @return: the new feed
160 @since: 0.48
161 @see: L{iface_cache.get_feed}, which calls this"""
162 try:
163 root = qdom.parse(file(source))
164 except IOError, ex:
165 if ex.errno == 2:
166 raise InvalidInterface(_("Feed not found. Perhaps this is a local feed that no longer exists? You can remove it from the list of feeds in that case."), ex)
167 raise InvalidInterface(_("Can't read file"), ex)
168 except Exception, ex:
169 raise InvalidInterface(_("Invalid XML"), ex)
171 if local:
172 local_path = source
173 else:
174 local_path = None
175 feed = ZeroInstallFeed(root, local_path, distro.get_host_distribution())
176 feed.last_modified = int(os.stat(source).st_mtime)
177 return feed