2 The B{0install add-feed} command-line interface.
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
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
:
26 def handle(config
, options
, args
, add_ok
= True, remove_ok
= False):
27 if len(args
) != 1: raise UsageError()
31 print(_("Feed '%s':") % x
+ '\n')
32 x
= model
.canonical_iface_uri(x
)
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
)
42 candidate_interfaces
= config
.iface_cache
.get_feed_targets(x
)
43 assert candidate_interfaces
45 for i
in range(len(candidate_interfaces
)):
46 iface
= candidate_interfaces
[i
]
47 if find_feed_import(iface
, x
):
49 print(_("%(index)d) Remove as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
})
50 interfaces
.append(iface
)
53 print(_("%(index)d) Add as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
})
54 interfaces
.append(iface
)
57 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
58 {'feed': x
, 'interface': candidate_interfaces
[0]})
60 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
61 {'feed': x
, 'interface': candidate_interfaces
[0]})
65 i
= raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
66 except KeyboardInterrupt:
68 raise SafeException(_("Aborted at user request."))
76 if i
> 0 and i
<= len(interfaces
):
78 print(_("Invalid number. Try again. (1 to %d)") % len(interfaces
))
79 iface
= interfaces
[i
- 1]
80 feed_import
= find_feed_import(iface
, x
)
82 iface
.extra_feeds
.remove(feed_import
)
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())
88 for f
in iface
.extra_feeds
:
91 print(_("(no feeds)"))