2 The B{0install select} 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 _
12 from zeroinstall
.cmd
import UsageError
13 from zeroinstall
.injector
import model
, selections
, requirements
14 from zeroinstall
.injector
.policy
import Policy
18 def add_generic_select_options(parser
):
19 """All options for selecting."""
20 parser
.add_option("", "--before", help=_("choose a version before this"), metavar
='VERSION')
21 parser
.add_option("", "--command", help=_("command to select"), metavar
='COMMAND')
22 parser
.add_option("", "--cpu", help=_("target CPU type"), metavar
='CPU')
23 parser
.add_option("", "--message", help=_("message to display when interacting with user"))
24 parser
.add_option("", "--not-before", help=_("minimum version to choose"), metavar
='VERSION')
25 parser
.add_option("-o", "--offline", help=_("try to avoid using the network"), action
='store_true')
26 parser
.add_option("", "--os", help=_("target operation system type"), metavar
='OS')
27 parser
.add_option("-r", "--refresh", help=_("refresh all used interfaces"), action
='store_true')
28 parser
.add_option("-s", "--source", help=_("select source code"), action
='store_true')
30 def add_options(parser
):
31 """Options for 'select' and 'download' (but not 'run')"""
32 add_generic_select_options(parser
)
33 parser
.add_option("", "--xml", help=_("write selected versions as XML"), action
='store_true')
35 def get_selections(config
, options
, iface_uri
, select_only
, download_only
, test_callback
):
36 """Get selections for iface_uri, according to the options passed.
37 Will switch to GUI mode if necessary.
38 @param options: options from OptionParser
39 @param iface_uri: canonical URI of the interface
40 @param select_only: return immediately even if the selected versions aren't cached
41 @param download_only: wait for stale feeds, and display GUI button as Download, not Run
42 @return: the selected versions, or None if the user cancels
43 @rtype: L{selections.Selections} | None
46 config
.network_use
= model
.network_offline
48 # Try to load it as a feed. If it is a feed, it'll get cached. If not, it's a
49 # selections document and we return immediately.
50 maybe_selections
= config
.iface_cache
.get_feed(iface_uri
, selections_ok
= True)
51 if isinstance(maybe_selections
, selections
.Selections
):
53 blocker
= maybe_selections
.download_missing(config
)
55 logging
.info(_("Waiting for selected implementations to be downloaded..."))
56 config
.handler
.wait_for_blocker(blocker
)
57 return maybe_selections
59 r
= requirements
.Requirements(iface_uri
)
60 r
.parse_options(options
)
62 policy
= Policy(config
= config
, requirements
= r
)
64 # Note that need_download() triggers a solve
65 if options
.refresh
or options
.gui
:
66 # We could run immediately, but the user asked us not to
67 can_run_immediately
= False
70 # --select-only: we only care that we've made a selection, not that we've cached the implementations
71 policy
.need_download()
72 can_run_immediately
= policy
.ready
74 can_run_immediately
= not policy
.need_download()
76 stale_feeds
= [feed
for feed
in policy
.solver
.feeds_used
if
77 not feed
.startswith('distribution:') and # Ignore (memory-only) PackageKit feeds
78 policy
.is_stale(config
.iface_cache
.get_feed(feed
))]
80 if download_only
and stale_feeds
:
81 can_run_immediately
= False
83 if can_run_immediately
:
85 if policy
.network_use
== model
.network_offline
:
86 logging
.debug(_("No doing background update because we are in off-line mode."))
88 # There are feeds we should update, but we can run without them.
89 # Do the update in the background while the program is running.
90 from zeroinstall
.injector
import background
91 background
.spawn_background_update(policy
, options
.verbose
> 0)
92 return policy
.solver
.selections
94 # If the user didn't say whether to use the GUI, choose for them.
95 if options
.gui
is None and os
.environ
.get('DISPLAY', None):
97 # If we need to download anything, we might as well
98 # refresh all the feeds first.
99 options
.refresh
= True
100 logging
.info(_("Switching to GUI mode... (use --console to disable)"))
103 gui_args
= policy
.requirements
.get_as_options()
105 # Just changes the button's label
106 gui_args
.append('--download-only')
108 gui_args
.append('--refresh')
110 gui_args
.insert(0, '--verbose')
111 if options
.verbose
> 1:
112 gui_args
.insert(0, '--verbose')
113 if options
.with_store
:
114 for x
in options
.with_store
:
115 gui_args
+= ['--with-store', x
]
117 gui_args
.append('--select-only')
119 from zeroinstall
import helpers
120 sels
= helpers
.get_selections_gui(iface_uri
, gui_args
, test_callback
)
123 return None # Aborted
125 # Note: --download-only also makes us stop and download stale feeds first.
126 downloaded
= policy
.solve_and_download_impls(refresh
= options
.refresh
or download_only
or False,
127 select_only
= select_only
)
129 config
.handler
.wait_for_blocker(downloaded
)
130 sels
= selections
.Selections(policy
)
134 def handle(config
, options
, args
):
137 iface_uri
= model
.canonical_iface_uri(args
[0])
139 sels
= get_selections(config
, options
, iface_uri
,
140 select_only
= True, download_only
= False, test_callback
= None)
142 sys
.exit(1) # Aborted by user
147 show_human(sels
, config
.stores
)
151 doc
.writexml(sys
.stdout
)
152 sys
.stdout
.write('\n')
154 def show_human(sels
, stores
):
155 from zeroinstall
import zerostore
156 done
= set() # detect cycles
157 def print_node(uri
, command
, indent
):
158 if uri
in done
: return
160 impl
= sels
.selections
.get(uri
, None)
161 print indent
+ "- URI:", uri
163 print indent
+ " Version:", impl
.version
165 if impl
.id.startswith('package:'):
166 path
= "(" + impl
.id + ")"
168 path
= impl
.local_path
or stores
.lookup_any(impl
.digests
)
169 except zerostore
.NotStored
:
170 path
= "(not cached)"
171 print indent
+ " Path:", path
173 deps
= impl
.dependencies
174 if command
is not None:
175 deps
+= sels
.commands
[command
].requires
177 if isinstance(child
, model
.InterfaceDependency
):
178 if child
.qdom
.name
== 'runner':
179 child_command
= command
+ 1
182 print_node(child
.interface
, child_command
, indent
)
184 print indent
+ " No selected version"
188 print_node(sels
.interface
, 0, "")
190 print_node(sels
.interface
, None, "")