コード例 #1
0
ファイル: cli.py プロジェクト: sugar-activities/4037-activity
def _manage_feeds(options, args):
	from zeroinstall.injector import writer
	from zeroinstall.injector.handler import Handler
	from zeroinstall.injector.policy import Policy
	handler = Handler(dry_run = options.dry_run)
	if not args: raise UsageError()
	for x in args:
		print _("Feed '%s':") % x + '\n'
		x = model.canonical_iface_uri(x)
		policy = Policy(x, handler)
		if options.offline:
			policy.network_use = model.network_offline

		feed = iface_cache.get_feed(x)
		if policy.network_use != model.network_offline and policy.is_stale(feed):
			blocker = policy.fetcher.download_and_import_feed(x, iface_cache.iface_cache)
			print _("Downloading feed; please wait...")
			handler.wait_for_blocker(blocker)
			print _("Done")

		interfaces = policy.get_feed_targets(x)
		for i in range(len(interfaces)):
			feed = interfaces[i].get_feed(x)
			if feed:
				print _("%(index)d) Remove as feed for '%(uri)s'") % {'index': i + 1, 'uri': interfaces[i].uri}
			else:
				print _("%(index)d) Add as feed for '%(uri)s'") % {'index': i + 1, 'uri': interfaces[i].uri}
		print
		while True:
			try:
				i = raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
			except KeyboardInterrupt:
				print
				raise SafeException(_("Aborted at user request."))
			if i == '':
				i = 1
			else:
				try:
					i = int(i)
				except ValueError:
					i = 0
			if i > 0 and i <= len(interfaces):
				break
			print _("Invalid number. Try again. (1 to %d)") % len(interfaces)
		iface = interfaces[i - 1]
		feed = iface.get_feed(x)
		if feed:
			iface.extra_feeds.remove(feed)
		else:
			iface.extra_feeds.append(model.Feed(x, arch = None, user_override = True))
		writer.save_interface(iface)
		print '\n' + _("Feed list for interface '%s' is now:") % iface.get_name()
		if iface.feeds:
			for f in iface.feeds:
				print "- " + f.uri
		else:
			print _("(no feeds)")
コード例 #2
0
def handle(config, options, args, add_ok=True, remove_ok=False):
    if options.offline:
        config.network_use = model.network_offline

    if len(args) == 2:
        iface = config.iface_cache.get_interface(
            model.canonical_iface_uri(args[0]))
        feed_url = args[1]

        if find_feed_import(iface, feed_url):
            raise SafeException(
                _('Interface %(interface)s already has feed %(feed)s') % {
                    'interface': iface.uri,
                    'feed': feed_url
                })

        feed = config.iface_cache.get_feed(feed_url)
        if not feed:
            blocker = config.fetcher.download_and_import_feed(
                feed_url, config.iface_cache)
            tasks.wait_for_blocker(blocker)

        iface.extra_feeds.append(
            model.Feed(feed_url, arch=None, user_override=True))
        writer.save_interface(iface)
        return
    elif len(args) != 1:
        raise UsageError()

    x = args[0]

    print _("Feed '%s':") % x + '\n'
    x = model.canonical_iface_uri(x)
    policy = Policy(x, config=config)

    feed = config.iface_cache.get_feed(x)
    if policy.network_use != model.network_offline and policy.is_stale(feed):
        blocker = config.fetcher.download_and_import_feed(
            x, config.iface_cache)
        tasks.wait_for_blocker(blocker)

    candidate_interfaces = policy.get_feed_targets(x)
    assert candidate_interfaces
    interfaces = []
    for i in range(len(candidate_interfaces)):
        iface = candidate_interfaces[i]
        if find_feed_import(iface, x):
            if remove_ok:
                print _("%(index)d) Remove as feed for '%(uri)s'") % {
                    'index': i + 1,
                    'uri': iface.uri
                }
                interfaces.append(iface)
        else:
            if add_ok:
                print _("%(index)d) Add as feed for '%(uri)s'") % {
                    'index': i + 1,
                    'uri': iface.uri
                }
                interfaces.append(iface)
    if not interfaces:
        if remove_ok:
            raise SafeException(
                _("%(feed)s is not registered as a feed for %(interface)s") % {
                    'feed': x,
                    'interface': candidate_interfaces[0]
                })
        else:
            raise SafeException(
                _("%(feed)s already registered as a feed for %(interface)s") %
                {
                    'feed': x,
                    'interface': candidate_interfaces[0]
                })
    print
    while True:
        try:
            i = raw_input(
                _('Enter a number, or CTRL-C to cancel [1]: ')).strip()
        except KeyboardInterrupt:
            print
            raise SafeException(_("Aborted at user request."))
        if i == '':
            i = 1
        else:
            try:
                i = int(i)
            except ValueError:
                i = 0
        if i > 0 and i <= len(interfaces):
            break
        print _("Invalid number. Try again. (1 to %d)") % len(interfaces)
    iface = interfaces[i - 1]
    feed_import = find_feed_import(iface, x)
    if feed_import:
        iface.extra_feeds.remove(feed_import)
    else:
        iface.extra_feeds.append(model.Feed(x, arch=None, user_override=True))
    writer.save_interface(iface)
    print '\n' + _("Feed list for interface '%s' is now:") % iface.get_name()
    if iface.extra_feeds:
        for f in iface.extra_feeds:
            print "- " + f.uri
    else:
        print _("(no feeds)")
コード例 #3
0
def handle(config, options, args, add_ok = True, remove_ok = False):
	if options.offline:
		config.network_use = model.network_offline

	if len(args) == 2:
		iface = config.iface_cache.get_interface(model.canonical_iface_uri(args[0]))
		feed_url = args[1]

		if find_feed_import(iface, feed_url):
			raise SafeException(_('Interface %(interface)s already has feed %(feed)s') %
						{'interface': iface.uri, 'feed': feed_url})

		feed = config.iface_cache.get_feed(feed_url)
		if not feed:
			blocker = config.fetcher.download_and_import_feed(feed_url, config.iface_cache)
			tasks.wait_for_blocker(blocker)

		iface.extra_feeds.append(model.Feed(feed_url, arch = None, user_override = True))
		writer.save_interface(iface)
		return
	elif len(args) != 1:
		raise UsageError()

	x = args[0]

	print _("Feed '%s':") % x + '\n'
	x = model.canonical_iface_uri(x)
	policy = Policy(x, config = config)

	feed = config.iface_cache.get_feed(x)
	if policy.network_use != model.network_offline and policy.is_stale(feed):
		blocker = config.fetcher.download_and_import_feed(x, config.iface_cache)
		tasks.wait_for_blocker(blocker)

	candidate_interfaces = policy.get_feed_targets(x)
	assert candidate_interfaces
	interfaces = []
	for i in range(len(candidate_interfaces)):
		iface = candidate_interfaces[i]
		if find_feed_import(iface, x):
			if remove_ok:
				print _("%(index)d) Remove as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri}
				interfaces.append(iface)
		else:
			if add_ok:
				print _("%(index)d) Add as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri}
				interfaces.append(iface)
	if not interfaces:
		if remove_ok:
			raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
						{'feed': x, 'interface': candidate_interfaces[0]})
		else:
			raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
						{'feed': x, 'interface': candidate_interfaces[0]})
	print
	while True:
		try:
			i = raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
		except KeyboardInterrupt:
			print
			raise SafeException(_("Aborted at user request."))
		if i == '':
			i = 1
		else:
			try:
				i = int(i)
			except ValueError:
				i = 0
		if i > 0 and i <= len(interfaces):
			break
		print _("Invalid number. Try again. (1 to %d)") % len(interfaces)
	iface = interfaces[i - 1]
	feed_import = find_feed_import(iface, x)
	if feed_import:
		iface.extra_feeds.remove(feed_import)
	else:
		iface.extra_feeds.append(model.Feed(x, arch = None, user_override = True))
	writer.save_interface(iface)
	print '\n' + _("Feed list for interface '%s' is now:") % iface.get_name()
	if iface.extra_feeds:
		for f in iface.extra_feeds:
			print "- " + f.uri
	else:
		print _("(no feeds)")