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.
11 from zeroinstall
import cmd
, SafeException
, _
12 from zeroinstall
.cmd
import UsageError
13 from zeroinstall
.injector
import model
, writer
, handler
14 from zeroinstall
.injector
.policy
import Policy
15 from zeroinstall
.injector
.iface_cache
import iface_cache
19 def add_options(parser
):
20 parser
.add_option("-o", "--offline", help=_("try to avoid using the network"), action
='store_true')
22 def handle(options
, args
, add_ok
= True, remove_ok
= False):
23 if not args
: raise UsageError()
26 h
= handler
.ConsoleHandler()
29 h
.dry_run
= bool(options
.dry_run
)
31 def find_feed_import(iface
, feed_url
):
32 for f
in iface
.extra_feeds
:
38 print _("Feed '%s':") % x
+ '\n'
39 x
= model
.canonical_iface_uri(x
)
42 policy
.network_use
= model
.network_offline
44 feed
= iface_cache
.get_feed(x
)
45 if policy
.network_use
!= model
.network_offline
and policy
.is_stale(feed
):
46 blocker
= policy
.fetcher
.download_and_import_feed(x
, iface_cache
)
47 print _("Downloading feed; please wait...")
48 h
.wait_for_blocker(blocker
)
51 candidate_interfaces
= policy
.get_feed_targets(x
)
52 assert candidate_interfaces
54 for i
in range(len(candidate_interfaces
)):
55 iface
= candidate_interfaces
[i
]
56 if find_feed_import(iface
, x
):
58 print _("%(index)d) Remove as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
}
59 interfaces
.append(iface
)
62 print _("%(index)d) Add as feed for '%(uri)s'") % {'index': i
+ 1, 'uri': iface
.uri
}
63 interfaces
.append(iface
)
66 raise SafeException(_("%(feed)s is not registered as a feed for %(interface)s") %
67 {'feed': x
, 'interface': candidate_interfaces
[0]})
69 raise SafeException(_("%(feed)s already registered as a feed for %(interface)s") %
70 {'feed': x
, 'interface': candidate_interfaces
[0]})
74 i
= raw_input(_('Enter a number, or CTRL-C to cancel [1]: ')).strip()
75 except KeyboardInterrupt:
77 raise SafeException(_("Aborted at user request."))
85 if i
> 0 and i
<= len(interfaces
):
87 print _("Invalid number. Try again. (1 to %d)") % len(interfaces
)
88 iface
= interfaces
[i
- 1]
89 feed_import
= find_feed_import(iface
, x
)
91 iface
.extra_feeds
.remove(feed_import
)
93 iface
.extra_feeds
.append(model
.Feed(x
, arch
= None, user_override
= True))
94 writer
.save_interface(iface
)
95 print '\n' + _("Feed list for interface '%s' is now:") % iface
.get_name()
97 for f
in iface
.extra_feeds
:
100 print _("(no feeds)")