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 zeroinstall
import SafeException
, _
9 from zeroinstall
.support
import tasks
10 from zeroinstall
.cmd
import UsageError
11 from zeroinstall
.injector
import model
, writer
12 from zeroinstall
.injector
.policy
import Policy
16 def add_options(parser
):
17 parser
.add_option("-o", "--offline", help=_("try to avoid using the network"), action
='store_true')
19 def find_feed_import(iface
, feed_url
):
20 for f
in iface
.extra_feeds
:
25 def handle(config
, options
, args
, add_ok
= True, remove_ok
= False):
26 if len(args
) != 1: raise UsageError()
30 print _("Feed '%s':") % x
+ '\n'
31 x
= model
.canonical_iface_uri(x
)
32 policy
= Policy(x
, config
= config
)
34 config
.network_use
= model
.network_offline
36 feed
= config
.iface_cache
.get_feed(x
)
37 if policy
.network_use
!= model
.network_offline
and policy
.is_stale(feed
):
38 blocker
= policy
.fetcher
.download_and_import_feed(x
, config
.iface_cache
)
39 print _("Downloading feed; please wait...")
40 tasks
.wait_for_blocker(blocker
)
43 candidate_interfaces
= policy
.get_feed_targets(x
)
44 assert candidate_interfaces
46 for i
in range(len(candidate_interfaces
)):
47 iface
= candidate_interfaces
[i
]
48 if find_feed_import(iface
, x
):
50 print _("%(index)d) Remove as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
}
51 interfaces
.append(iface
)
54 print _("%(index)d) Add as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
}
55 interfaces
.append(iface
)
58 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
59 {'feed': x
, 'interface': candidate_interfaces
[0]})
61 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
62 {'feed': x
, 'interface': candidate_interfaces
[0]})
66 i
= raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
67 except KeyboardInterrupt:
69 raise SafeException(_("Aborted at user request."))
77 if i
> 0 and i
<= len(interfaces
):
79 print _("Invalid number. Try again. (1 to %d)") % len(interfaces
)
80 iface
= interfaces
[i
- 1]
81 feed_import
= find_feed_import(iface
, x
)
83 iface
.extra_feeds
.remove(feed_import
)
85 iface
.extra_feeds
.append(model
.Feed(x
, arch
= None, user_override
= True))
86 writer
.save_interface(iface
)
87 print '\n' + _("Feed list for interface '%s' is now:") % iface
.get_name()
89 for f
in iface
.extra_feeds
: