Tell the user when a background update completes, not when it starts
[zeroinstall/solver.git] / zeroinstall / injector / driver.py
blobd18e9a8128af2593620f4840d59a33cefd4578a6
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 (deprecated)
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.items():
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 # There are three cases:
90 # 1. We want to run immediately if possible. If not, download all the information we can.
91 # (force = False, update_local = False)
92 # 2. We're in no hurry, but don't want to use the network unnecessarily.
93 # We should still update local information (from PackageKit).
94 # (force = False, update_local = True)
95 # 3. The user explicitly asked us to refresh everything.
96 # (force = True)
98 try_quick_exit = not (force or update_local)
100 while True:
101 self.solver.solve_for(self.requirements)
102 for w in self.watchers: w()
104 if try_quick_exit and self.solver.ready:
105 break
106 try_quick_exit = False
108 if not self.solver.ready:
109 force = True
111 for f in self.solver.feeds_used:
112 if f in downloads_finished or f in downloads_in_progress:
113 continue
114 if os.path.isabs(f):
115 if force:
116 self.config.iface_cache.get_feed(f, force = True)
117 downloads_in_progress[f] = tasks.IdleBlocker('Refresh local feed')
118 continue
119 elif f.startswith('distribution:'):
120 if force or update_local:
121 downloads_in_progress[f] = self.config.fetcher.download_and_import_feed(f, self.config.iface_cache)
122 elif force and self.config.network_use != network_offline:
123 downloads_in_progress[f] = self.config.fetcher.download_and_import_feed(f, self.config.iface_cache)
124 # Once we've starting downloading some things,
125 # we might as well get them all.
126 force = True
128 if not downloads_in_progress:
129 if self.config.network_use == network_offline:
130 info(_("Can't choose versions and in off-line mode, so aborting"))
131 break
133 # Wait for at least one download to finish
134 blockers = downloads_in_progress.values()
135 yield blockers
136 tasks.check(blockers, self.config.handler.report_error)
138 for f in list(downloads_in_progress.keys()):
139 if f in downloads_in_progress and downloads_in_progress[f].happened:
140 del downloads_in_progress[f]
141 downloads_finished.add(f)
143 # Need to refetch any "distribution" feed that
144 # depends on this one
145 distro_feed_url = 'distribution:' + f
146 if distro_feed_url in downloads_finished:
147 downloads_finished.remove(distro_feed_url)
148 if distro_feed_url in downloads_in_progress:
149 del downloads_in_progress[distro_feed_url]
151 @tasks.async
152 def solve_and_download_impls(self, refresh = False, select_only = False):
153 """Run L{solve_with_downloads} and then get the selected implementations too.
154 @raise SafeException: if we couldn't select a set of implementations
155 @since: 0.40"""
156 refreshed = self.solve_with_downloads(refresh)
157 if refreshed:
158 yield refreshed
159 tasks.check(refreshed)
161 if not self.solver.ready:
162 raise self.solver.get_failure_reason()
164 if not select_only:
165 downloaded = self.download_uncached_implementations()
166 if downloaded:
167 yield downloaded
168 tasks.check(downloaded)
170 def need_download(self):
171 """Decide whether we need to download anything (but don't do it!)
172 @return: true if we MUST download something (feeds or implementations)
173 @rtype: bool"""
174 self.solver.solve_for(self.requirements)
175 for w in self.watchers: w()
177 if not self.solver.ready:
178 return True # Maybe a newer version will work?
180 if self.get_uncached_implementations():
181 return True
183 return False
185 def download_uncached_implementations(self):
186 """Download all implementations chosen by the solver that are missing from the cache."""
187 assert self.solver.ready, "Solver is not ready!\n%s" % self.solver.selections
188 stores = self.config.stores
189 return self.config.fetcher.download_impls([impl for impl in self.solver.selections.values() if not impl.is_available(stores)],
190 stores)