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
7 # Copyright (C) 2009, Thomas Leonard
8 # See the README file for details, or visit http://0install.net.
10 from zeroinstall
import _
12 from logging
import info
, debug
, warn
14 from zeroinstall
import SafeException
15 from zeroinstall
.injector
import arch
, model
16 from zeroinstall
.injector
.model
import Interface
, Implementation
, network_levels
, network_offline
, network_full
17 from zeroinstall
.injector
.namespaces
import config_site
, config_prog
18 from zeroinstall
.injector
.config
import load_config
19 from zeroinstall
.support
import tasks
22 """Chooses a set of implementations based on a policy.
24 1. Create a Policy object, giving it the URI of the program to be run and a handler.
25 2. Call L{solve_with_downloads}. If more information is needed, a L{fetch.Fetcher} will be used to download it.
26 3. When all downloads are complete, the L{solver} contains the chosen versions.
27 4. Use L{get_uncached_implementations} to find where to get these versions and download them
28 using L{download_uncached_implementations}.
30 @ivar target_arch: target architecture for binaries
31 @type target_arch: L{arch.Architecture}
32 @ivar root: URI of the root interface
33 @ivar solver: solver used to choose a set of implementations
34 @type solver: L{solve.Solver}
35 @ivar watchers: callbacks to invoke after recalculating
36 @ivar help_with_testing: default stability policy
37 @type help_with_testing: bool
38 @ivar network_use: one of the model.network_* values
39 @ivar freshness: seconds allowed since last update
41 @ivar stale_feeds: set of feeds which are present but haven't been checked for a long time
42 @type stale_feeds: set
44 __slots__
= ['root', 'watchers', 'requirements', 'config', '_warned_offline',
45 'command', 'target_arch',
46 'stale_feeds', 'solver']
48 help_with_testing
= property(lambda self
: self
.config
.help_with_testing
,
49 lambda self
, value
: setattr(self
.config
, 'help_with_testing', bool(value
)))
51 network_use
= property(lambda self
: self
.config
.network_use
,
52 lambda self
, value
: setattr(self
.config
, 'network_use', value
))
54 freshness
= property(lambda self
: self
.config
.freshness
,
55 lambda self
, value
: setattr(self
.config
, 'freshness', str(value
)))
57 implementation
= property(lambda self
: self
.solver
.selections
)
59 ready
= property(lambda self
: self
.solver
.ready
)
62 handler
= property(lambda self
: self
.config
.handler
,
63 lambda self
, value
: setattr(self
.config
, 'handler', value
))
66 def __init__(self
, root
= None, handler
= None, src
= None, command
= -1, config
= None, requirements
= None):
68 @param requirements: Details about the program we want to run
69 @type requirements: L{requirements.Requirements}
70 @param config: The configuration settings to use, or None to load from disk.
71 @type config: L{config.Config}
72 Note: all other arguments are deprecated (since 0launch 0.52)
75 if requirements
is None:
76 from zeroinstall
.injector
.requirements
import Requirements
77 requirements
= Requirements(root
)
78 requirements
.source
= bool(src
) # Root impl must be a "src" machine type
84 requirements
.command
= command
85 self
.target_arch
= arch
.get_host_architecture()
87 assert root
== src
== None
89 self
.target_arch
= arch
.get_architecture(requirements
.os
, requirements
.cpu
)
90 self
.requirements
= requirements
92 self
.stale_feeds
= set()
95 self
.config
= load_config(handler
)
97 assert handler
is None, "can't pass a handler and a config"
100 from zeroinstall
.injector
.solver
import DefaultSolver
101 self
.solver
= DefaultSolver(self
.config
)
103 # If we need to download something but can't because we are offline,
104 # warn the user. But only the first time.
105 self
._warned
_offline
= False
107 debug(_("Supported systems: '%s'"), arch
.os_ranks
)
108 debug(_("Supported processors: '%s'"), arch
.machine_ranks
)
110 if requirements
.before
or requirements
.not_before
:
111 self
.solver
.extra_restrictions
[config
.iface_cache
.get_interface(requirements
.interface_uri
)] = [
112 model
.VersionRangeRestriction(model
.parse_version(requirements
.before
),
113 model
.parse_version(requirements
.not_before
))]
117 return self
.config
.fetcher
119 def save_config(self
):
120 self
.config
.save_globals()
122 def recalculate(self
, fetch_stale_interfaces
= True):
123 """@deprecated: see L{solve_with_downloads} """
125 warnings
.warn("Policy.recalculate is deprecated!", DeprecationWarning, stacklevel
= 2)
127 self
.stale_feeds
= set()
129 host_arch
= self
.target_arch
130 if self
.requirements
.source
:
131 host_arch
= arch
.SourceArchitecture(host_arch
)
132 self
.solver
.solve(self
.root
, host_arch
, command_name
= self
.command
)
134 if self
.network_use
== network_offline
:
135 fetch_stale_interfaces
= False
138 for f
in self
.solver
.feeds_used
:
139 if os
.path
.isabs(f
): continue
140 feed
= self
.config
.iface_cache
.get_feed(f
)
141 if feed
is None or feed
.last_modified
is None:
142 self
.download_and_import_feed_if_online(f
) # Will start a download
143 elif self
.is_stale(feed
):
144 debug(_("Adding %s to stale set"), f
)
145 self
.stale_feeds
.add(self
.config
.iface_cache
.get_interface(f
)) # Legacy API
146 if fetch_stale_interfaces
:
147 self
.download_and_import_feed_if_online(f
) # Will start a download
149 for w
in self
.watchers
: w()
153 def usable_feeds(self
, iface
):
154 """Generator for C{iface.feeds} that are valid for our architecture.
157 if self
.requirements
.source
and iface
.uri
== self
.root
:
158 # Note: when feeds are recursive, we'll need a better test for root here
159 machine_ranks
= {'src': 1}
161 machine_ranks
= arch
.machine_ranks
163 for f
in self
.config
.iface_cache
.get_feed_imports(iface
):
164 if f
.os
in arch
.os_ranks
and f
.machine
in machine_ranks
:
167 debug(_("Skipping '%(feed)s'; unsupported architecture %(os)s-%(machine)s"),
168 {'feed': f
, 'os': f
.os
, 'machine': f
.machine
})
170 def is_stale(self
, feed
):
171 """@deprecated: use IfaceCache.is_stale"""
172 return self
.config
.iface_cache
.is_stale(feed
, self
.config
.freshness
)
174 def download_and_import_feed_if_online(self
, feed_url
):
175 """If we're online, call L{fetch.Fetcher.download_and_import_feed}. Otherwise, log a suitable warning."""
176 if self
.network_use
!= network_offline
:
177 debug(_("Feed %s not cached and not off-line. Downloading..."), feed_url
)
178 return self
.fetcher
.download_and_import_feed(feed_url
, self
.config
.iface_cache
)
180 if self
._warned
_offline
:
181 debug(_("Not downloading feed '%s' because we are off-line."), feed_url
)
183 warn(_("Not downloading feed '%s' because we are in off-line mode."), feed_url
)
184 self
._warned
_offline
= True
186 def get_implementation_path(self
, impl
):
187 """Return the local path of impl.
189 @raise zeroinstall.zerostore.NotStored: if it needs to be added to the cache first."""
190 assert isinstance(impl
, Implementation
)
191 return impl
.local_path
or self
.config
.stores
.lookup_any(impl
.digests
)
193 def get_implementation(self
, interface
):
194 """Get the chosen implementation.
195 @type interface: Interface
196 @rtype: L{model.Implementation}
197 @raise SafeException: if interface has not been fetched or no implementation could be
199 assert isinstance(interface
, Interface
)
202 return self
.implementation
[interface
]
204 raise SafeException(_("No usable implementation found for '%s'.") % interface
.uri
)
206 def get_cached(self
, impl
):
207 """Check whether an implementation is available locally.
208 @type impl: model.Implementation
211 return impl
.is_available(self
.config
.stores
)
213 def get_uncached_implementations(self
):
214 """List all chosen implementations which aren't yet available locally.
215 @rtype: [(L{model.Interface}, L{model.Implementation})]"""
216 iface_cache
= self
.config
.iface_cache
218 for uri
, selection
in self
.solver
.selections
.selections
.iteritems():
219 impl
= selection
.impl
220 assert impl
, self
.solver
.selections
221 if not self
.get_cached(impl
):
222 uncached
.append((iface_cache
.get_interface(uri
), impl
))
225 def refresh_all(self
, force
= True):
226 """Start downloading all feeds for all selected interfaces.
227 @param force: Whether to restart existing downloads."""
228 return self
.solve_with_downloads(force
= True)
230 def get_feed_targets(self
, feed
):
231 """@deprecated: use IfaceCache.get_feed_targets"""
232 return self
.config
.iface_cache
.get_feed_targets(feed
)
235 def solve_with_downloads(self
, force
= False, update_local
= False):
236 """Run the solver, then download any feeds that are missing or
237 that need to be updated. Each time a new feed is imported into
238 the cache, the solver is run again, possibly adding new downloads.
239 @param force: whether to download even if we're already ready to run.
240 @param update_local: fetch PackageKit feeds even if we're ready to run."""
242 downloads_finished
= set() # Successful or otherwise
243 downloads_in_progress
= {} # URL -> Download
245 host_arch
= self
.target_arch
246 if self
.requirements
.source
:
247 host_arch
= arch
.SourceArchitecture(host_arch
)
249 # There are three cases:
250 # 1. We want to run immediately if possible. If not, download all the information we can.
251 # (force = False, update_local = False)
252 # 2. We're in no hurry, but don't want to use the network unnecessarily.
253 # We should still update local information (from PackageKit).
254 # (force = False, update_local = True)
255 # 3. The user explicitly asked us to refresh everything.
258 try_quick_exit
= not (force
or update_local
)
261 self
.solver
.solve(self
.root
, host_arch
, command_name
= self
.command
)
262 for w
in self
.watchers
: w()
264 if try_quick_exit
and self
.solver
.ready
:
266 try_quick_exit
= False
268 if not self
.solver
.ready
:
271 for f
in self
.solver
.feeds_used
:
272 if f
in downloads_finished
or f
in downloads_in_progress
:
276 self
.config
.iface_cache
.get_feed(f
, force
= True)
277 downloads_in_progress
[f
] = tasks
.IdleBlocker('Refresh local feed')
279 elif f
.startswith('distribution:'):
280 if force
or update_local
:
281 downloads_in_progress
[f
] = self
.fetcher
.download_and_import_feed(f
, self
.config
.iface_cache
)
282 elif force
and self
.network_use
!= network_offline
:
283 downloads_in_progress
[f
] = self
.fetcher
.download_and_import_feed(f
, self
.config
.iface_cache
)
284 # Once we've starting downloading some things,
285 # we might as well get them all.
288 if not downloads_in_progress
:
289 if self
.network_use
== network_offline
:
290 info(_("Can't choose versions and in off-line mode, so aborting"))
293 # Wait for at least one download to finish
294 blockers
= downloads_in_progress
.values()
296 tasks
.check(blockers
, self
.handler
.report_error
)
298 for f
in downloads_in_progress
.keys():
299 if f
in downloads_in_progress
and downloads_in_progress
[f
].happened
:
300 del downloads_in_progress
[f
]
301 downloads_finished
.add(f
)
303 # Need to refetch any "distribution" feed that
304 # depends on this one
305 distro_feed_url
= 'distribution:' + f
306 if distro_feed_url
in downloads_finished
:
307 downloads_finished
.remove(distro_feed_url
)
308 if distro_feed_url
in downloads_in_progress
:
309 del downloads_in_progress
[distro_feed_url
]
312 def solve_and_download_impls(self
, refresh
= False, select_only
= False):
313 """Run L{solve_with_downloads} and then get the selected implementations too.
314 @raise SafeException: if we couldn't select a set of implementations
316 refreshed
= self
.solve_with_downloads(refresh
)
319 tasks
.check(refreshed
)
321 if not self
.solver
.ready
:
322 raise self
.solver
.get_failure_reason()
325 downloaded
= self
.download_uncached_implementations()
328 tasks
.check(downloaded
)
330 def need_download(self
):
331 """Decide whether we need to download anything (but don't do it!)
332 @return: true if we MUST download something (feeds or implementations)
334 host_arch
= self
.target_arch
335 if self
.requirements
.source
:
336 host_arch
= arch
.SourceArchitecture(host_arch
)
337 self
.solver
.solve(self
.root
, host_arch
, command_name
= self
.command
)
338 for w
in self
.watchers
: w()
340 if not self
.solver
.ready
:
341 return True # Maybe a newer version will work?
343 if self
.get_uncached_implementations():
348 def download_uncached_implementations(self
):
349 """Download all implementations chosen by the solver that are missing from the cache."""
350 assert self
.solver
.ready
, "Solver is not ready!\n%s" % self
.solver
.selections
351 return self
.fetcher
.download_impls([impl
for impl
in self
.solver
.selections
.values() if not self
.get_cached(impl
)],
354 def download_icon(self
, interface
, force
= False):
355 """Download an icon for this interface and add it to the
356 icon cache. If the interface has no icon or we are offline, do nothing.
357 @return: the task doing the import, or None
358 @rtype: L{tasks.Task}"""
359 if self
.network_use
== network_offline
:
360 info("Not downloading icon for %s as we are off-line", interface
)
363 modification_time
= None
365 existing_icon
= self
.config
.iface_cache
.get_icon_path(interface
)
367 file_mtime
= os
.stat(existing_icon
).st_mtime
368 from email
.utils
import formatdate
369 modification_time
= formatdate(timeval
= file_mtime
, localtime
= False, usegmt
= True)
371 return self
.fetcher
.download_icon(interface
, force
, modification_time
)
373 def get_interface(self
, uri
):
374 """@deprecated: use L{iface_cache.IfaceCache.get_interface} instead"""
376 warnings
.warn("Policy.get_interface is deprecated!", DeprecationWarning, stacklevel
= 2)
377 return self
.config
.iface_cache
.get_interface(uri
)
381 return self
.requirements
.command
385 return self
.requirements
.interface_uri
388 def get_deprecated_singleton_config():
391 _config
= load_config()