1 """
2 The B{0install add-feed} command-line interface.
3 """
4
5
6
7
8 from __future__ import print_function
9
10 from zeroinstall import SafeException, _
11 from zeroinstall.support import tasks, raw_input
12 from zeroinstall.cmd import UsageError
13 from zeroinstall.injector import model, writer
14
15 syntax = "[INTERFACE] NEW-FEED"
16
18 parser.add_option("-o", "--offline", help=_("try to avoid using the network"), action='store_true')
19
21 """@type iface: L{zeroinstall.injector.model.Interface}
22 @type feed_url: str
23 @rtype: L{zeroinstall.injector.model.Feed}"""
24 for f in iface.extra_feeds:
25 if f.uri == feed_url:
26 return f
27 return None
28
29 -def handle(config, options, args, add_ok = True, remove_ok = False):
30 """@type add_ok: bool
31 @type remove_ok: bool"""
32 if len(args) == 2:
33 iface = config.iface_cache.get_interface(model.canonical_iface_uri(args[0]))
34 feed_url = model.canonical_iface_uri(args[1])
35
36 feed_import = find_feed_import(iface, feed_url)
37 if feed_import:
38 raise SafeException(_('Interface %(interface)s already has a feed %(feed)s') %
39 {'interface': iface.uri, 'feed': feed_url})
40 iface.extra_feeds.append(model.Feed(feed_url, arch = None, user_override = True))
41 writer.save_interface(iface)
42 return
43 elif len(args) != 1: raise UsageError()
44
45 x = args[0]
46
47 print(_("Feed '%s':") % x + '\n')
48 x = model.canonical_iface_uri(x)
49 if options.offline:
50 config.network_use = model.network_offline
51
52 if config.network_use != model.network_offline and config.iface_cache.is_stale(x, config.freshness):
53 blocker = config.fetcher.download_and_import_feed(x, config.iface_cache)
54 print(_("Downloading feed; please wait..."))
55 tasks.wait_for_blocker(blocker)
56 print(_("Done"))
57
58 candidate_interfaces = config.iface_cache.get_feed_targets(x)
59 assert candidate_interfaces
60 interfaces = []
61 for i in range(len(candidate_interfaces)):
62 iface = candidate_interfaces[i]
63 if find_feed_import(iface, x):
64 if remove_ok:
65 print(_("%(index)d) Remove as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri})
66 interfaces.append(iface)
67 else:
68 if add_ok:
69 print(_("%(index)d) Add as feed for '%(uri)s'") % {'index': i + 1, 'uri': iface.uri})
70 interfaces.append(iface)
71 if not interfaces:
72 if remove_ok:
73 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
74 {'feed': x, 'interface': candidate_interfaces[0]})
75 else:
76 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
77 {'feed': x, 'interface': candidate_interfaces[0]})
78 print()
79 while True:
80 try:
81 i = raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
82 except KeyboardInterrupt:
83 print()
84 raise SafeException(_("Aborted at user request."))
85 if i == '':
86 i = 1
87 else:
88 try:
89 i = int(i)
90 except ValueError:
91 i = 0
92 if i > 0 and i <= len(interfaces):
93 break
94 print(_("Invalid number. Try again. (1 to %d)") % len(interfaces))
95 iface = interfaces[i - 1]
96 feed_import = find_feed_import(iface, x)
97 if feed_import:
98 iface.extra_feeds.remove(feed_import)
99 else:
100 iface.extra_feeds.append(model.Feed(x, arch = None, user_override = True))
101 writer.save_interface(iface)
102 print('\n' + _("Feed list for interface '%s' is now:") % iface.get_name())
103 if iface.extra_feeds:
104 for f in iface.extra_feeds:
105 print("- " + f.uri)
106 else:
107 print(_("(no feeds)"))
108
110 """@type completion: L{zeroinstall.cmd._Completion}
111 @type args: [str]
112 @type cword: int"""
113 if cword > 1: return
114 if cword == 0:
115 completion.expand_interfaces()
116 else:
117 completion.expand_files()
118