1 # Copyright (C) 2006, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 from logging
import debug
, info
7 from zeroinstall
.injector
import model
, download
8 from zeroinstall
.injector
import policy
, run
, handler
9 from zeroinstall
import NeedDownload
11 class AutoPolicy(policy
.Policy
):
12 __slots__
= ['allow_downloads', 'download_only', 'dry_run']
14 def __init__(self
, interface_uri
, download_only
= False, dry_run
= False):
15 policy
.Policy
.__init
__(self
, interface_uri
, handler
.Handler())
16 self
.dry_run
= dry_run
17 self
.allow_downloads
= not dry_run
18 self
.download_only
= download_only
19 self
.dry_run
= dry_run
21 def need_download(self
):
22 """Decide whether we need to download anything (but don't do it!)"""
23 old
= self
.allow_downloads
24 self
.allow_downloads
= False
28 debug("Recalculated: ready = %s", self
.ready
)
29 if not self
.ready
: return False
30 self
.start_downloading_impls()
35 self
.allow_downloads
= old
37 def begin_iface_download(self
, interface
, force
= False):
38 if self
.dry_run
or not self
.allow_downloads
:
39 raise NeedDownload(interface
.uri
)
41 policy
.Policy
.begin_iface_download(self
, interface
, force
)
43 def start_downloading_impls(self
):
44 for iface
, impl
in self
.get_uncached_implementations():
45 debug("start_downloading_impls: for %s get %s", iface
, impl
)
46 if not impl
.download_sources
:
47 raise model
.SafeException("Implementation " + impl
.id + " of "
48 "interface " + iface
.get_name() + " cannot be "
49 "downloaded (no download locations given in "
51 source
= impl
.download_sources
[0]
52 if self
.dry_run
or not self
.allow_downloads
:
53 raise NeedDownload(source
.url
)
55 dl
= download
.begin_impl_download(source
)
56 self
.handler
.monitor_download(dl
)
58 def execute(self
, prog_args
, main
= None):
59 self
.start_downloading_impls()
60 self
.handler
.wait_for_downloads()
61 if not self
.download_only
:
62 run
.execute(self
, prog_args
, dry_run
= self
.dry_run
, main
= main
)
64 info("Downloads done (download-only mode)")
66 def recalculate_with_dl(self
):
68 if self
.handler
.monitored_downloads
:
69 self
.handler
.wait_for_downloads()
72 def download_and_execute(self
, prog_args
, refresh
= False, main
= None):
73 self
.recalculate_with_dl()
75 self
.refresh_all(False)
76 self
.recalculate_with_dl()
78 raise model
.SafeException("Can't find all required implementations:\n" +
79 '\n'.join(["- %s -> %s" % (iface
, self
.implementation
[iface
])
80 for iface
in self
.implementation
]))
81 self
.execute(prog_args
, main
= main
)