Model information about distribution packages as separate feeds
[zeroinstall/zeroinstall-afb.git] / zeroinstall / injector / reader.py
blobee6bddabefc916da26f318232439290b79e086d9
1 """
2 Parses an XML feed into a Python representation. You should probably use L{iface_cache.iface_cache} rather than the functions here.
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 Internal: use L{iface_cache.iface_cache.get_interface} instread.
26 @rtype: bool"""
27 interface.reset()
28 from zeroinstall.injector.iface_cache import iface_cache
30 # Add the distribution package manager's version, if any
31 path = basedir.load_first_data(config_site, 'native_feeds', model._pretty_escape(interface.uri))
32 if path:
33 # Resolve any symlinks
34 info(_("Adding native packager feed '%s'"), path)
35 interface.extra_feeds.append(Feed(os.path.realpath(path), None, False))
37 update_user_overrides(interface)
39 main_feed = iface_cache.get_feed(interface.uri, force = True)
40 if main_feed:
41 update_user_feed_overrides(main_feed)
43 return main_feed is not None
45 def load_feed_from_cache(url):
46 """Load a feed. If the feed is remote, load from the cache. If local, load it directly.
47 @return: the feed, or None if it's remote and not cached."""
48 if os.path.isabs(url):
49 debug(_("Loading local feed file '%s'"), url)
50 return load_feed(url, local = True)
51 else:
52 cached = basedir.load_first_cache(config_site, 'interfaces', escape(url))
53 if cached:
54 debug(_("Loading cached information for %(interface)s from %(cached)s"), {'interface': url, 'cached': cached})
55 return load_feed(cached, local = False)
56 else:
57 return None
59 def update_user_feed_overrides(feed):
60 """Update a feed with user-supplied information.
61 Sets last_checked and user_stability ratings.
62 @param feed: feed to update
63 @since 0.49
64 """
65 user = basedir.load_first_config(config_site, config_prog,
66 'feeds', model._pretty_escape(feed.url))
67 if user is None:
68 # For files saved by 0launch < 0.49
69 user = basedir.load_first_config(config_site, config_prog,
70 'user_overrides', escape(feed.url))
71 if not user:
72 return
74 try:
75 root = qdom.parse(file(user))
76 except Exception, ex:
77 warn(_("Error reading '%(user)s': %(exception)s"), {'user': user, 'exception': ex})
78 raise
80 last_checked = root.getAttribute('last-checked')
81 if last_checked:
82 feed.last_checked = int(last_checked)
84 for item in root.childNodes:
85 if item.uri != XMLNS_IFACE: continue
86 if item.name == 'implementation':
87 id = item.getAttribute('id')
88 assert id is not None
89 impl = feed.implementations.get(id, None)
90 if not impl:
91 debug(_("Ignoring user-override for unknown implementation %(id)s in %(interface)s"), {'id': id, 'interface': feed})
92 continue
94 user_stability = item.getAttribute('user-stability')
95 if user_stability:
96 impl.user_stability = stability_levels[str(user_stability)]
98 def update_user_overrides(interface):
99 """Update an interface with user-supplied information.
100 Sets preferred stability and updates extra_feeds.
101 @param interface: the interface object to update
102 @type interface: L{model.Interface}
104 user = basedir.load_first_config(config_site, config_prog,
105 'interfaces', model._pretty_escape(interface.uri))
106 if user is None:
107 # For files saved by 0launch < 0.49
108 user = basedir.load_first_config(config_site, config_prog,
109 'user_overrides', escape(interface.uri))
110 if not user:
111 return
113 try:
114 root = qdom.parse(file(user))
115 except Exception, ex:
116 warn(_("Error reading '%(user)s': %(exception)s"), {'user': user, 'exception': ex})
117 raise
119 stability_policy = root.getAttribute('stability-policy')
120 if stability_policy:
121 interface.set_stability_policy(stability_levels[str(stability_policy)])
123 for item in root.childNodes:
124 if item.uri != XMLNS_IFACE: continue
125 if item.name == 'feed':
126 feed_src = item.getAttribute('src')
127 if not feed_src:
128 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
129 interface.extra_feeds.append(Feed(feed_src, item.getAttribute('arch'), True, langs = item.getAttribute('langs')))
131 def check_readable(feed_url, source):
132 """Test whether a feed file is valid.
133 @param feed_url: the feed's expected URL
134 @type feed_url: str
135 @param source: the name of the file to test
136 @type source: str
137 @return: the modification time in src (usually just the mtime of the file)
138 @rtype: int
139 @raise InvalidInterface: If the source's syntax is incorrect,
141 try:
142 feed = load_feed(source, local = False)
144 if feed.url != feed_url:
145 raise InvalidInterface(_("Incorrect URL used for feed.\n\n"
146 "%(feed_url)s is given in the feed, but\n"
147 "%(interface_uri)s was requested") %
148 {'feed_url': feed.url, 'interface_uri': feed_url})
149 return feed.last_modified
150 except InvalidInterface, ex:
151 info(_("Error loading feed:\n"
152 "Interface URI: %(uri)s\n"
153 "Local file: %(source)s\n"
154 "%(exception)s") %
155 {'uri': feed_url, 'source': source, 'exception': ex})
156 raise InvalidInterface(_("Error loading feed '%(uri)s':\n\n%(exception)s") % {'uri': feed_url, 'exception': ex})
158 def update(interface, source, local = False):
159 """Read in information about an interface.
160 Deprecated.
161 @param interface: the interface object to update
162 @type interface: L{model.Interface}
163 @param source: the name of the file to read
164 @type source: str
165 @param local: use file's mtime for last-modified, and uri attribute is ignored
166 @raise InvalidInterface: if the source's syntax is incorrect
167 @return: the new feed (since 0.32)
168 @see: L{update_from_cache}, which calls this"""
169 assert isinstance(interface, Interface)
171 feed = load_feed(source, local)
173 if not local:
174 if feed.url != interface.uri:
175 raise InvalidInterface(_("Incorrect URL used for feed.\n\n"
176 "%(feed_url)s is given in the feed, but\n"
177 "%(interface_uri)s was requested") %
178 {'feed_url': feed.url, 'interface_uri': interface.uri})
180 # Hack.
181 from zeroinstall.injector.iface_cache import iface_cache
182 iface_cache._feeds[unicode(interface.uri)] = feed
184 return feed
186 def load_feed(source, local = False):
187 """Load a feed from a local file.
188 @param source: the name of the file to read
189 @type source: str
190 @param local: this is a local feed
191 @type local: bool
192 @raise InvalidInterface: if the source's syntax is incorrect
193 @return: the new feed
194 @since: 0.48
195 @see: L{iface_cache.iface_cache}, which uses this to load the feeds"""
196 try:
197 root = qdom.parse(file(source))
198 except IOError, ex:
199 if ex.errno == 2:
200 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)
201 raise InvalidInterface(_("Can't read file"), ex)
202 except Exception, ex:
203 raise InvalidInterface(_("Invalid XML"), ex)
205 if local:
206 local_path = source
207 else:
208 local_path = None
209 feed = ZeroInstallFeed(root, local_path)
210 feed.last_modified = int(os.stat(source).st_mtime)
211 return feed