2 Parses an XML interface into a Python representation.
5 # Copyright (C) 2009, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall
import _
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.
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
))
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)
49 cached
= basedir
.load_first_cache(config_site
, 'interfaces', escape(url
))
51 debug(_("Loading cached information for %(interface)s from %(cached)s"), {'interface': url
, 'cached': cached
})
52 return load_feed(cached
, local
= False)
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.
63 user
= basedir
.load_first_config(config_site
, config_prog
,
64 'user_overrides', escape(interface
.uri
))
69 root
= qdom
.parse(file(user
))
71 warn(_("Error reading '%(user)s': %(exception)s"), {'user': user
, 'exception': ex
})
74 # This is a bit wrong; this information is about the feed,
77 last_checked
= root
.getAttribute('last-checked')
79 main_feed
.last_checked
= int(last_checked
)
81 stability_policy
= root
.getAttribute('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')
90 if not (id.startswith('/') or id.startswith('.') or id.startswith('package:')):
92 impl
= interface
.implementations
.get(id, None)
94 debug(_("Ignoring user-override for unknown implementation %(id)s in %(interface)s"), {'id': id, 'interface': interface
})
97 user_stability
= item
.getAttribute('user-stability')
99 impl
.user_stability
= stability_levels
[str(user_stability
)]
100 elif item
.name
== 'feed':
101 feed_src
= item
.getAttribute('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
112 @return: the modification time in src (usually just the mtime of the file)
114 @raise InvalidInterface: If the source's syntax is incorrect,
116 tmp
= Interface(interface_uri
)
119 except InvalidInterface
, ex
:
120 info(_("Error loading feed:\n"
121 "Interface URI: %(uri)s\n"
122 "Local file: %(source)s\n"
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
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
)
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
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
156 @param local: this is a local feed
158 @raise InvalidInterface: if the source's syntax is incorrect
159 @return: the new feed
161 @see: L{iface_cache.get_feed}, which calls this"""
163 root
= qdom
.parse(file(source
))
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
)
175 feed
= ZeroInstallFeed(root
, local_path
, distro
.get_host_distribution())
176 feed
.last_modified
= int(os
.stat(source
).st_mtime
)