2 A simple non-interactive policy.
4 This module provides a simple policy that will select, download and run a suitable set of
5 implementations. It is not interactive. This is the policy used when you run B{0launch -c}, and
6 is also the policy used to run the injector's GUI.
9 # Copyright (C) 2006, Thomas Leonard
10 # See the README file for details, or visit http://0install.net.
13 from logging
import debug
, info
15 from zeroinstall
.support
import tasks
16 from zeroinstall
.injector
import model
, policy
, run
17 from zeroinstall
.injector
.handler
import Handler
18 from zeroinstall
import NeedDownload
20 class AutoPolicy(policy
.Policy
):
21 __slots__
= ['allow_downloads', 'download_only', 'dry_run']
23 def __init__(self
, interface_uri
, download_only
= False, dry_run
= False, src
= False, handler
= None):
24 """@param handler: (new in 0.30) handler to use, or None to create a L{Handler}"""
25 policy
.Policy
.__init
__(self
, interface_uri
, handler
or Handler(), src
= src
)
26 self
.dry_run
= dry_run
27 self
.allow_downloads
= not dry_run
28 self
.download_only
= download_only
29 self
.dry_run
= dry_run
31 def download_and_import_feed(self
, feed_url
, force
= False):
32 if self
.dry_run
or not self
.allow_downloads
:
33 raise NeedDownload(feed_url
)
35 return policy
.Policy
.download_and_import_feed(self
, feed_url
, force
)
37 def download_archive(self
, download_source
, force
= False):
38 if self
.dry_run
or not self
.allow_downloads
:
39 raise NeedDownload(download_source
.url
)
40 return policy
.Policy
.download_archive(self
, download_source
, force
= force
)
42 def execute(self
, prog_args
, main
= None, wrapper
= None):
43 task
= tasks
.Task(self
.download_impls(), "download_impls")
44 self
.handler
.wait_for_blocker(task
.finished
)
45 if not self
.download_only
:
46 run
.execute(self
, prog_args
, dry_run
= self
.dry_run
, main
= main
, wrapper
= wrapper
)
48 info("Downloads done (download-only mode)")
50 def download_and_execute(self
, prog_args
, refresh
= False, main
= None):
51 task
= tasks
.Task(self
.solve_with_downloads(refresh
), "solve_with_downloads")
53 errors
= self
.handler
.wait_for_blocker(task
.finished
)
55 raise model
.SafeException("Errors during download: " + '\n'.join(errors
))
57 if not self
.solver
.ready
:
58 raise model
.SafeException("Can't find all required implementations:\n" +
59 '\n'.join(["- %s -> %s" % (iface
, self
.solver
.selections
[iface
])
60 for iface
in self
.solver
.selections
]))
61 self
.execute(prog_args
, main
= main
)