Auto-throw exceptions when resuming tasks
[zeroinstall.git] / zeroinstall / injector / driver.py
blobc29119563296efdaf630d31c5df50ff9af8748a2
1 """
2 This class brings together a L{solve.Solver} to choose a set of implmentations, a
3 L{fetch.Fetcher} to download additional components, and the user's configuration
4 settings.
5 @since: 0.53
6 """
8 # Copyright (C) 2011, Thomas Leonard
9 # See the README file for details, or visit http://0install.net.
11 from zeroinstall import _
12 import os
13 from logging import info, debug
15 from zeroinstall.injector import arch, model
16 from zeroinstall.injector.model import network_offline
17 from zeroinstall.support import tasks
19 class Driver(object):
20 """Chooses a set of implementations based on a policy.
21 Typical use:
22 1. Create a Driver object, giving it the requirements about the program to be run.
23 2. Call L{solve_with_downloads}. If more information is needed, a L{fetch.Fetcher} will be used to download it.
24 3. When all downloads are complete, the L{solver} contains the chosen versions.
25 4. Use L{get_uncached_implementations} to find where to get these versions and download them
26 using L{download_uncached_implementations}.
28 @ivar target_arch: target architecture for binaries
29 @type target_arch: L{arch.Architecture}
30 @ivar solver: solver used to choose a set of implementations
31 @type solver: L{solve.Solver}
32 @ivar watchers: callbacks to invoke after solving
33 """
34 __slots__ = ['watchers', 'requirements', 'config', 'target_arch', 'solver']
36 def __init__(self, config, requirements):
37 """
38 @param config: The configuration settings to use
39 @type config: L{config.Config}
40 @param requirements: Details about the program we want to run
41 @type requirements: L{requirements.Requirements}
42 @since: 0.53
43 """
44 self.watchers = []
46 assert config
47 self.config = config
49 assert requirements
50 self.requirements = requirements
52 self.target_arch = arch.get_architecture(requirements.os, requirements.cpu)
54 from zeroinstall.injector.solver import DefaultSolver
55 self.solver = DefaultSolver(self.config)
57 debug(_("Supported systems: '%s'"), arch.os_ranks)
58 debug(_("Supported processors: '%s'"), arch.machine_ranks)
60 if requirements.before or requirements.not_before:
61 self.solver.extra_restrictions[config.iface_cache.get_interface(requirements.interface_uri)] = [
62 model.VersionRangeRestriction(model.parse_version(requirements.before),
63 model.parse_version(requirements.not_before))]
65 def get_uncached_implementations(self):
66 """List all chosen implementations which aren't yet available locally.
67 @rtype: [(L{model.Interface}, L{model.Implementation})]"""
68 iface_cache = self.config.iface_cache
69 stores = self.config.stores
70 uncached = []
71 for uri, selection in self.solver.selections.selections.iteritems():
72 impl = selection.impl
73 assert impl, self.solver.selections
74 if not impl.is_available(stores):
75 uncached.append((iface_cache.get_interface(uri), impl))
76 return uncached
78 @tasks.async
79 def solve_with_downloads(self, force = False, update_local = False):
80 """Run the solver, then download any feeds that are missing or
81 that need to be updated. Each time a new feed is imported into
82 the cache, the solver is run again, possibly adding new downloads.
83 @param force: whether to download even if we're already ready to run.
84 @param update_local: fetch PackageKit feeds even if we're ready to run."""
86 downloads_finished = set() # Successful or otherwise
87 downloads_in_progress = {} # URL -> Download
89 host_arch = self.target_arch
90 if self.requirements.source:
91 host_arch = arch.SourceArchitecture(host_arch)
93 # There are three cases:
94 # 1. We want to run immediately if possible. If not, download all the information we can.
95 # (force = False, update_local = False)
96 # 2. We're in no hurry, but don't want to use the network unnecessarily.
97 # We should still update local information (from PackageKit).
98 # (force = False, update_local = True)
99 # 3. The user explicitly asked us to refresh everything.
100 # (force = True)
102 try_quick_exit = not (force or update_local)
104 while True:
105 self.solver.solve(self.requirements.interface_uri, host_arch, command_name = self.requirements.command)
106 for w in self.watchers: w()
108 if try_quick_exit and self.solver.ready:
109 break
110 try_quick_exit = False
112 if not self.solver.ready:
113 force = True
115 for f in self.solver.feeds_used:
116 if f in downloads_finished or f in downloads_in_progress:
117 continue
118 if os.path.isabs(f):
119 if force:
120 self.config.iface_cache.get_feed(f, force = True)
121 downloads_in_progress[f] = tasks.IdleBlocker('Refresh local feed')
122 continue
123 elif f.startswith('distribution:'):
124 if force or update_local:
125 downloads_in_progress[f] = self.config.fetcher.download_and_import_feed(f, self.config.iface_cache)
126 elif force and self.config.network_use != network_offline:
127 downloads_in_progress[f] = self.config.fetcher.download_and_import_feed(f, self.config.iface_cache)
128 # Once we've starting downloading some things,
129 # we might as well get them all.
130 force = True
132 if not downloads_in_progress:
133 if self.config.network_use == network_offline:
134 info(_("Can't choose versions and in off-line mode, so aborting"))
135 break
137 # Wait for at least one download to finish
138 blockers = downloads_in_progress.values()
139 try:
140 yield blockers
141 except:
142 pass
143 tasks.check(blockers, self.config.handler.report_error)
145 for f in downloads_in_progress.keys():
146 if f in downloads_in_progress and downloads_in_progress[f].happened:
147 del downloads_in_progress[f]
148 downloads_finished.add(f)
150 # Need to refetch any "distribution" feed that
151 # depends on this one
152 distro_feed_url = 'distribution:' + f
153 if distro_feed_url in downloads_finished:
154 downloads_finished.remove(distro_feed_url)
155 if distro_feed_url in downloads_in_progress:
156 del downloads_in_progress[distro_feed_url]
158 @tasks.async
159 def solve_and_download_impls(self, refresh = False, select_only = False):
160 """Run L{solve_with_downloads} and then get the selected implementations too.
161 @raise SafeException: if we couldn't select a set of implementations
162 @since: 0.40"""
163 refreshed = self.solve_with_downloads(refresh)
164 if refreshed:
165 yield refreshed
167 if not self.solver.ready:
168 raise self.solver.get_failure_reason()
170 if not select_only:
171 downloaded = self.download_uncached_implementations()
172 if downloaded:
173 yield downloaded
175 def need_download(self):
176 """Decide whether we need to download anything (but don't do it!)
177 @return: true if we MUST download something (feeds or implementations)
178 @rtype: bool"""
179 host_arch = self.target_arch
180 if self.requirements.source:
181 host_arch = arch.SourceArchitecture(host_arch)
182 self.solver.solve(self.requirements.interface_uri, host_arch, command_name = self.requirements.command)
183 for w in self.watchers: w()
185 if not self.solver.ready:
186 return True # Maybe a newer version will work?
188 if self.get_uncached_implementations():
189 return True
191 return False
193 def download_uncached_implementations(self):
194 """Download all implementations chosen by the solver that are missing from the cache."""
195 assert self.solver.ready, "Solver is not ready!\n%s" % self.solver.selections
196 stores = self.config.stores
197 return self.config.fetcher.download_impls([impl for impl in self.solver.selections.values() if not impl.is_available(stores)],
198 stores)