Don't keep trying to check failed feeds that we never had
[zeroinstall/solver.git] / zeroinstall / cmd / add_feed.py
blobfb9f44d0f07fe2ef119ecc2de522809c11a37bac
1 """
2 The B{0install add-feed} command-line interface.
3 """
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from __future__ import print_function
10 from zeroinstall import SafeException, _
11 from zeroinstall.support import tasks
12 from zeroinstall.cmd import UsageError
13 from zeroinstall.injector import model, writer
15 syntax = "NEW-FEED"
17 def add_options(parser):
18 parser.add_option("-o", "--offline", help=_("try to avoid using the network"), action='store_true')
20 def find_feed_import(iface, feed_url):
21 for f in iface.extra_feeds:
22 if f.uri == feed_url:
23 return f
24 return None
26 def handle(config, options, args, add_ok = True, remove_ok = False):
27 if len(args) != 1: raise UsageError()
29 x = args[0]
31 print(_("Feed '%s':") % x + '\n')
32 x = model.canonical_iface_uri(x)
33 if options.offline:
34 config.network_use = model.network_offline
36 if config.network_use != model.network_offline and config.iface_cache.is_stale(x, config.freshness):
37 blocker = config.fetcher.download_and_import_feed(x, config.iface_cache)
38 print(_("Downloading feed; please wait..."))
39 tasks.wait_for_blocker(blocker)
40 print(_("Done"))
42 candidate_interfaces = config.iface_cache.get_feed_targets(x)
43 assert candidate_interfaces
44 interfaces = []
45 for i in range(len(candidate_interfaces)):
46 iface = candidate_interfaces[i]
47 if find_feed_import(iface, x):
48 if remove_ok:
49 print(_("%(index)d) Remove as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri})
50 interfaces.append(iface)
51 else:
52 if add_ok:
53 print(_("%(index)d) Add as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri})
54 interfaces.append(iface)
55 if not interfaces:
56 if remove_ok:
57 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
58 {'feed': x, 'interface': candidate_interfaces[0]})
59 else:
60 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
61 {'feed': x, 'interface': candidate_interfaces[0]})
62 print()
63 while True:
64 try:
65 i = raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
66 except KeyboardInterrupt:
67 print()
68 raise SafeException(_("Aborted at user request."))
69 if i == '':
70 i = 1
71 else:
72 try:
73 i = int(i)
74 except ValueError:
75 i = 0
76 if i > 0 and i <= len(interfaces):
77 break
78 print(_("Invalid number. Try again. (1 to %d)") % len(interfaces))
79 iface = interfaces[i - 1]
80 feed_import = find_feed_import(iface, x)
81 if feed_import:
82 iface.extra_feeds.remove(feed_import)
83 else:
84 iface.extra_feeds.append(model.Feed(x, arch = None, user_override = True))
85 writer.save_interface(iface)
86 print('\n' + _("Feed list for interface '%s' is now:") % iface.get_name())
87 if iface.extra_feeds:
88 for f in iface.extra_feeds:
89 print("- " + f.uri)
90 else:
91 print(_("(no feeds)"))