def check_readable(feed_url, source): """Test whether a feed file is valid. @param feed_url: the feed's expected URL @type feed_url: str @param source: the name of the file to test @type source: str @return: the modification time in src (usually just the mtime of the file) @rtype: int @raise InvalidInterface: If the source's syntax is incorrect""" try: feed = load_feed(source, local = False) if feed.url != feed_url: raise InvalidInterface(_("Incorrect URL used for feed.\n\n" "%(feed_url)s is given in the feed, but\n" "%(interface_uri)s was requested") % {'feed_url': feed.url, 'interface_uri': feed_url}) return feed.last_modified except InvalidInterface as ex: logger.info(_("Error loading feed:\n" "Interface URI: %(uri)s\n" "Local file: %(source)s\n" "%(exception)s") % {'uri': feed_url, 'source': source, 'exception': ex}) raise InvalidInterface(_("Error loading feed '%(uri)s':\n\n%(exception)s") % {'uri': feed_url, 'exception': ex})
def load_feed(source, local=False, selections_ok=False): """Load a feed from a local file. @param source: the name of the file to read @type source: str @param local: this is a local feed @type local: bool @param selections_ok: if it turns out to be a local selections document, return that instead @type selections_ok: bool @raise InvalidInterface: if the source's syntax is incorrect @return: the new feed @since: 0.48 @see: L{iface_cache.iface_cache}, which uses this to load the feeds""" try: root = qdom.parse(open(source)) except IOError as ex: if ex.errno == errno.ENOENT and local: raise MissingLocalFeed( _("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." )) raise InvalidInterface(_("Can't read file"), ex) except Exception as ex: raise InvalidInterface(_("Invalid XML"), ex) if local: if selections_ok and root.uri == XMLNS_IFACE and root.name == 'selections': from zeroinstall.injector import selections return selections.Selections(root) local_path = source else: local_path = None feed = ZeroInstallFeed(root, local_path) feed.last_modified = int(os.stat(source).st_mtime) return feed
def update(interface, source, local=False, iface_cache=None): """Read in information about an interface. Deprecated. @param interface: the interface object to update @type interface: L{model.Interface} @param source: the name of the file to read @type source: str @param local: use file's mtime for last-modified, and uri attribute is ignored @raise InvalidInterface: if the source's syntax is incorrect @return: the new feed (since 0.32) @see: L{update_from_cache}, which calls this""" assert isinstance(interface, Interface) feed = load_feed(source, local) if not local: if feed.url != interface.uri: raise InvalidInterface( _("Incorrect URL used for feed.\n\n" "%(feed_url)s is given in the feed, but\n" "%(interface_uri)s was requested") % { 'feed_url': feed.url, 'interface_uri': interface.uri }) if iface_cache is None: from zeroinstall.injector import policy iface_cache = policy.get_deprecated_singleton_config().iface_cache iface_cache._feeds[unicode(interface.uri)] = feed return feed
def check_readable(interface_uri, source): """Test whether an interface file is valid. @param interface_uri: the interface's URI @type interface_uri: str @param source: the name of the file to test @type source: str @return: the modification time in src (usually just the mtime of the file) @rtype: int @raise InvalidInterface: If the source's syntax is incorrect, """ tmp = Interface(interface_uri) try: update(tmp, source) except InvalidInterface, ex: info( _("Error loading feed:\n" "Interface URI: %(uri)s\n" "Local file: %(source)s\n" "%(exception)s") % { 'uri': interface_uri, 'source': source, 'exception': ex }) raise InvalidInterface( _("Error loading feed '%(uri)s':\n\n%(exception)s") % { 'uri': interface_uri, 'exception': ex })
def update_user_overrides(interface): """Update an interface with user-supplied information. Sets preferred stability and updates extra_feeds. @param interface: the interface object to update @type interface: L{model.Interface} """ user = basedir.load_first_config(config_site, config_prog, 'interfaces', model._pretty_escape(interface.uri)) if user is None: # For files saved by 0launch < 0.49 user = basedir.load_first_config(config_site, config_prog, 'user_overrides', escape(interface.uri)) if not user: return try: root = qdom.parse(file(user)) except Exception as ex: warn(_("Error reading '%(user)s': %(exception)s"), {'user': user, 'exception': ex}) raise stability_policy = root.getAttribute('stability-policy') if stability_policy: interface.set_stability_policy(stability_levels[str(stability_policy)]) for item in root.childNodes: if item.uri != XMLNS_IFACE: continue if item.name == 'feed': feed_src = item.getAttribute('src') if not feed_src: raise InvalidInterface(_('Missing "src" attribute in <feed>')) interface.extra_feeds.append(Feed(feed_src, item.getAttribute('arch'), True, langs = item.getAttribute('langs')))
def load_feed(source, local = False, selections_ok = False, generate_sizes = False, implementation_id_alg=None, config=None): """Load a feed from a local file. @param source: the name of the file to read @type source: str @param local: this is a local feed @type local: bool @param selections_ok: if it turns out to be a local selections document, return that instead @type selections_ok: bool @param generate_sizes: if True, sizes of archives with missing size attributes will be generated @type generate_sizes: bool @param implementation_id_alg: if specified, missing impl ids will be generated with this alg @type implementation_id_alg: L{Algorithm} @raise InvalidInterface: if the source's syntax is incorrect @return: the new feed @since: 0.48 @see: L{iface_cache.iface_cache}, which uses this to load the feeds""" try: root = qdom.parse(file(source)) except IOError as ex: if ex.errno == 2 and local: raise MissingLocalFeed(_("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.")) raise InvalidInterface(_("Can't read file"), ex) except Exception as ex: raise InvalidInterface(_("Invalid XML"), ex) if local: if selections_ok and root.uri == XMLNS_IFACE and root.name == 'selections': from zeroinstall.injector import selections return selections.Selections(root) local_path = source else: local_path = None if implementation_id_alg or generate_sizes: from zeroinstall.injector.config import load_config if config is None: config = load_config() feed = ZeroInstallFeed(root, local_path, None, generate_sizes, implementation_id_alg, config.fetcher, config.stores) else: feed = ZeroInstallFeed(root, local_path) feed.last_modified = int(os.stat(source).st_mtime) return feed
def update(interface, source, local=False): """Read in information about an interface. @param interface: the interface object to update @type interface: L{model.Interface} @param source: the name of the file to read @type source: str @param local: use file's mtime for last-modified, and uri attribute is ignored @raise InvalidInterface: if the source's syntax is incorrect @return: the new feed (since 0.32) @see: L{update_from_cache}, which calls this""" assert isinstance(interface, Interface) try: root = qdom.parse(file(source)) except IOError, ex: if ex.errno == 2: 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) raise InvalidInterface(_("Can't read file"), ex)
def update_user_overrides(interface, known_site_feeds=frozenset()): """Update an interface with user-supplied information. Sets preferred stability and updates extra_feeds. @param interface: the interface object to update @type interface: L{model.Interface} @param known_site_feeds: feeds to ignore (for backwards compatibility) """ user = basedir.load_first_config(config_site, config_prog, 'interfaces', model._pretty_escape(interface.uri)) if user is None: # For files saved by 0launch < 0.49 user = basedir.load_first_config(config_site, config_prog, 'user_overrides', escape(interface.uri)) if not user: return try: with open(user, 'rb') as stream: root = qdom.parse(stream) except Exception as ex: logger.warn(_("Error reading '%(user)s': %(exception)s"), { 'user': user, 'exception': ex }) raise stability_policy = root.getAttribute('stability-policy') if stability_policy: interface.set_stability_policy(stability_levels[str(stability_policy)]) for item in root.childNodes: if item.uri != XMLNS_IFACE: continue if item.name == 'feed': feed_src = item.getAttribute('src') if not feed_src: raise InvalidInterface(_('Missing "src" attribute in <feed>')) # (note: 0install 1.9..1.12 used a different scheme and the "site-package" attribute; # we deliberately use a different attribute name to avoid confusion) if item.getAttribute('is-site-package'): # Site packages are detected earlier. This test isn't completely reliable, # since older versions will remove the attribute when saving the config # (hence the next test). continue if feed_src in known_site_feeds: continue interface.extra_feeds.append( Feed(feed_src, item.getAttribute('arch'), True, langs=item.getAttribute('langs')))
if not impl: debug( _("Ignoring user-override for unknown implementation %(id)s in %(interface)s" ), { 'id': id, 'interface': interface }) continue user_stability = item.getAttribute('user-stability') if user_stability: impl.user_stability = stability_levels[str(user_stability)] elif item.name == 'feed': feed_src = item.getAttribute('src') if not feed_src: raise InvalidInterface(_('Missing "src" attribute in <feed>')) interface.extra_feeds.append( Feed(feed_src, item.getAttribute('arch'), True, langs=item.getAttribute('langs'))) def check_readable(interface_uri, source): """Test whether an interface file is valid. @param interface_uri: the interface's URI @type interface_uri: str @param source: the name of the file to test @type source: str @return: the modification time in src (usually just the mtime of the file) @rtype: int