Added '0install run' sub-command
[zeroinstall/solver.git] / zeroinstall / cmd / select.py
blob850065962d73be95df0c78e9e51c3f6bb7f6f3c2
1 """
2 The B{0install select} command-line interface.
3 """
5 # Copyright (C) 2011, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from optparse import OptionParser
9 import os, sys
10 import logging
12 from zeroinstall import cmd, SafeException, _
13 from zeroinstall.cmd import UsageError
14 from zeroinstall.injector import model, autopolicy, selections, handler
15 from zeroinstall.injector.iface_cache import iface_cache
17 syntax = "URI"
19 def add_generic_select_options(parser):
20 """All options for selecting."""
21 parser.add_option("", "--before", help=_("choose a version before this"), metavar='VERSION')
22 parser.add_option("", "--command", help=_("command to select"), metavar='COMMAND')
23 parser.add_option("", "--cpu", help=_("target CPU type"), metavar='CPU')
24 parser.add_option("", "--message", help=_("message to display when interacting with user"))
25 parser.add_option("", "--not-before", help=_("minimum version to choose"), metavar='VERSION')
26 parser.add_option("-o", "--offline", help=_("try to avoid using the network"), action='store_true')
27 parser.add_option("", "--os", help=_("target operation system type"), metavar='OS')
28 parser.add_option("-r", "--refresh", help=_("refresh all used interfaces"), action='store_true')
29 parser.add_option("-s", "--source", help=_("select source code"), action='store_true')
31 def add_options(parser):
32 """Options for 'select' and 'download' (but not 'run')"""
33 add_generic_select_options(parser)
34 parser.add_option("", "--xml", help=_("write selected versions as XML"), action='store_true')
36 def get_selections(options, iface_uri, select_only, download_only, test_callback):
37 """Get selections for iface_uri, according to the options passed.
38 Will switch to GUI mode if necessary.
39 @param options: options from OptionParser
40 @param iface_uri: canonical URI of the interface
41 @param select_only: return immediately even if the selected versions aren't cached
42 @param download_only: wait for stale feeds, and display GUI button as Download, not Run
43 @return: the selected versions, or None if the user cancels
44 @rtype: L{selections.Selections} | None
45 """
46 root_iface = iface_cache.get_interface(iface_uri)
48 if os.isatty(1):
49 h = handler.ConsoleHandler()
50 else:
51 h = handler.Handler()
53 command_name = options.command
54 if command_name is None:
55 command_name = 'run'
56 elif command_name == '':
57 command_name = None
58 policy = autopolicy.AutoPolicy(iface_uri,
59 handler = h,
60 download_only = True, # unused?
61 src = options.source,
62 command = command_name)
64 if options.before or options.not_before:
65 policy.solver.extra_restrictions[root_iface] = [
66 model.VersionRangeRestriction(model.parse_version(options.before),
67 model.parse_version(options.not_before))]
69 if options.os or options.cpu:
70 from zeroinstall.injector import arch
71 policy.target_arch = arch.get_architecture(options.os, options.cpu)
73 if options.offline:
74 policy.network_use = model.network_offline
76 # Note that need_download() triggers a solve
77 if options.refresh or options.gui:
78 # We could run immediately, but the user asked us not to
79 can_run_immediately = False
80 else:
81 if select_only:
82 # --select-only: we only care that we've made a selection, not that we've cached the implementations
83 policy.need_download()
84 can_run_immediately = policy.ready
85 else:
86 can_run_immediately = not policy.need_download()
88 stale_feeds = [feed for feed in policy.solver.feeds_used if
89 not feed.startswith('distribution:') and # Ignore (memory-only) PackageKit feeds
90 policy.is_stale(iface_cache.get_feed(feed))]
92 if download_only and stale_feeds:
93 can_run_immediately = False
95 if can_run_immediately:
96 if stale_feeds:
97 if policy.network_use == model.network_offline:
98 logging.debug(_("No doing background update because we are in off-line mode."))
99 else:
100 # There are feeds we should update, but we can run without them.
101 # Do the update in the background while the program is running.
102 from zeroinstall.injector import background
103 background.spawn_background_update(policy, options.verbose > 0)
104 return policy.solver.selections
106 # If the user didn't say whether to use the GUI, choose for them.
107 if options.gui is None and os.environ.get('DISPLAY', None):
108 options.gui = True
109 # If we need to download anything, we might as well
110 # refresh all the feeds first.
111 options.refresh = True
112 logging.info(_("Switching to GUI mode... (use --console to disable)"))
114 if options.gui:
115 gui_args = []
116 if download_only:
117 # Just changes the button's label
118 gui_args.append('--download-only')
119 if options.refresh:
120 gui_args.append('--refresh')
121 if options.not_before:
122 gui_args.insert(0, options.not_before)
123 gui_args.insert(0, '--not-before')
124 if options.before:
125 gui_args.insert(0, options.before)
126 gui_args.insert(0, '--before')
127 if options.source:
128 gui_args.insert(0, '--source')
129 if options.message:
130 gui_args.insert(0, options.message)
131 gui_args.insert(0, '--message')
132 if options.verbose:
133 gui_args.insert(0, '--verbose')
134 if options.verbose > 1:
135 gui_args.insert(0, '--verbose')
136 if options.cpu:
137 gui_args.insert(0, options.cpu)
138 gui_args.insert(0, '--cpu')
139 if options.os:
140 gui_args.insert(0, options.os)
141 gui_args.insert(0, '--os')
142 if options.with_store:
143 for x in options.with_store:
144 gui_args += ['--with-store', x]
145 if select_only:
146 gui_args.append('--select-only')
147 if command_name is not None:
148 gui_args.append('--command')
149 gui_args.append(command_name)
151 from zeroinstall import helpers
152 sels = helpers.get_selections_gui(iface_uri, gui_args, test_callback)
154 if not sels:
155 return None # Aborted
156 else:
157 # Note: --download-only also makes us stop and download stale feeds first.
158 downloaded = policy.solve_and_download_impls(refresh = options.refresh or download_only or False,
159 select_only = select_only)
160 if downloaded:
161 policy.handler.wait_for_blocker(downloaded)
162 sels = selections.Selections(policy)
164 return sels
166 def handle(options, args):
167 if len(args) != 1:
168 raise UsageError()
169 iface_uri = model.canonical_iface_uri(args[0])
171 sels = get_selections(options, iface_uri,
172 select_only = True, download_only = False, test_callback = None)
173 if not sels:
174 sys.exit(1) # Aborted by user
176 if options.xml:
177 show_xml(sels)
178 else:
179 show_human(sels)
181 def show_xml(sels):
182 doc = sels.toDOM()
183 doc.writexml(sys.stdout)
184 sys.stdout.write('\n')
186 def show_human(sels):
187 from zeroinstall import zerostore
188 done = set() # detect cycles
189 def print_node(uri, command, indent):
190 if uri in done: return
191 done.add(uri)
192 impl = sels.selections.get(uri, None)
193 print indent + "- URI:", uri
194 if impl:
195 print indent + " Version:", impl.version
196 try:
197 if impl.id.startswith('package:'):
198 path = "(" + impl.id + ")"
199 else:
200 path = impl.local_path or iface_cache.stores.lookup_any(impl.digests)
201 except zerostore.NotStored:
202 path = "(not cached)"
203 print indent + " Path:", path
204 indent += " "
205 deps = impl.dependencies
206 if command is not None:
207 deps += sels.commands[command].requires
208 for child in deps:
209 if isinstance(child, model.InterfaceDependency):
210 if child.qdom.name == 'runner':
211 child_command = command + 1
212 else:
213 child_command = None
214 print_node(child.interface, child_command, indent)
215 else:
216 print indent + " No selected version"
219 if sels.commands:
220 print_node(sels.interface, 0, "")
221 else:
222 print_node(sels.interface, None, "")