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 handle(config
, options
, args
, add_ok
= True, remove_ok
= False):
20 if not args
: raise UsageError()
22 def find_feed_import(iface
, feed_url
):
23 for f
in iface
.extra_feeds
:
29 print _("Feed '%s':") % x
+ '\n'
30 x
= model
.canonical_iface_uri(x
)
31 policy
= Policy(x
, config
= config
)
33 config
.network_use
= model
.network_offline
35 feed
= config
.iface_cache
.get_feed(x
)
36 if policy
.network_use
!= model
.network_offline
and policy
.is_stale(feed
):
37 blocker
= policy
.fetcher
.download_and_import_feed(x
, config
.iface_cache
)
38 print _("Downloading feed; please wait...")
39 tasks
.wait_for_blocker(blocker
)
42 candidate_interfaces
= policy
.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
: