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
14 from zeroinstall
.injector
.policy
import Policy
18 def add_options(parser
):
19 parser
.add_option("-o", "--offline", help=_("try to avoid using the network"), action
='store_true')
21 def find_feed_import(iface
, feed_url
):
22 for f
in iface
.extra_feeds
:
27 def handle(config
, options
, args
, add_ok
= True, remove_ok
= False):
28 if len(args
) != 1: raise UsageError()
32 print(_("Feed '%s':") % x
+ '\n')
33 x
= model
.canonical_iface_uri(x
)
34 policy
= Policy(x
, config
= config
)
36 config
.network_use
= model
.network_offline
38 feed
= config
.iface_cache
.get_feed(x
)
39 if policy
.network_use
!= model
.network_offline
and policy
.is_stale(feed
):
40 blocker
= policy
.fetcher
.download_and_import_feed(x
, config
.iface_cache
)
41 print(_("Downloading feed; please wait..."))
42 tasks
.wait_for_blocker(blocker
)
45 candidate_interfaces
= policy
.get_feed_targets(x
)
46 assert candidate_interfaces
48 for i
in range(len(candidate_interfaces
)):
49 iface
= candidate_interfaces
[i
]
50 if find_feed_import(iface
, x
):
52 print(_("%(index)d) Remove as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
})
53 interfaces
.append(iface
)
56 print(_("%(index)d) Add as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
})
57 interfaces
.append(iface
)
60 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
61 {'feed': x
, 'interface': candidate_interfaces
[0]})
63 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
64 {'feed': x
, 'interface': candidate_interfaces
[0]})
68 i
= raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
69 except KeyboardInterrupt:
71 raise SafeException(_("Aborted at user request."))
79 if i
> 0 and i
<= len(interfaces
):
81 print(_("Invalid number. Try again. (1 to %d)") % len(interfaces
))
82 iface
= interfaces
[i
- 1]
83 feed_import
= find_feed_import(iface
, x
)
85 iface
.extra_feeds
.remove(feed_import
)
87 iface
.extra_feeds
.append(model
.Feed(x
, arch
= None, user_override
= True))
88 writer
.save_interface(iface
)
89 print('\n' + _("Feed list for interface '%s' is now:") % iface
.get_name())
91 for f
in iface
.extra_feeds
:
94 print(_("(no feeds)"))