Check size of downloaded archive.
[zeroinstall.git] / injector-auto
blob123f55ae830b81a56209ac4dc81eb82e53d842f8
1 #!/usr/bin/env python
2 import os, sys
4 __builtins__._ = lambda x: x
6 if len(sys.argv) < 2:
7 print "Usage: injector INTERFACE [ARGS]"
8 print "Eg:"
9 print "injector http://example.com/myprog.xml --help"
10 sys.exit(1)
11 interface_uri = sys.argv[1]
12 if not interface_uri.startswith('http:'):
13 interface_uri = os.path.realpath(interface_uri) # For testing
14 prog_args = sys.argv[2:]
16 import model
17 from policy import Policy
18 import download
20 import shutil
21 import sys
22 import gpg
24 class AutoPolicy(Policy):
25 monitored_downloads = None
27 def __init__(self, interface_uri):
28 Policy.__init__(self, interface_uri)
29 self.monitored_downloads = []
31 def monitor_download(self, dl):
32 error_stream = dl.start()
33 self.monitored_downloads.append((error_stream, dl))
35 def start_downloading_impls(self):
36 for impl in self.get_uncached_implementations():
37 if not impl.download_sources:
38 raise SafeException("Implementation " + impl + " of "
39 "interface " + impl.interface + " cannot be "
40 "downloaded (no download locations given in "
41 "interface!")
42 dl = download.begin_impl_download(impl.download_sources[0])
43 self.monitor_download(dl)
45 # Singleton instance used everywhere...
46 policy = AutoPolicy(interface_uri)
47 policy.recalculate()
49 def wait_for_downloads():
50 while policy.monitored_downloads:
51 print "Currently downloading:"
52 for e, dl in policy.monitored_downloads:
53 print "- " + dl.url
55 for e, dl in policy.monitored_downloads[:]:
56 errors = e.read()
57 if errors:
58 print "Got errors:", errors
59 e.close()
60 policy.monitored_downloads.remove((e, dl))
61 data = dl.error_stream_closed()
62 if isinstance(dl, download.InterfaceDownload):
63 policy.check_signed_data(dl, data)
64 elif isinstance(dl, download.ImplementationDownload):
65 policy.add_to_cache(dl.source, data)
66 else:
67 raise Exception("Unknown download type %s" % dl)
69 import run
71 def execute():
72 policy.start_downloading_impls()
74 wait_for_downloads()
76 run.execute(policy, prog_args)
78 # Get interfaces...
79 wait_for_downloads()
81 try:
82 execute()
83 except model.SafeException, ex:
84 if policy.network_use != model.network_full:
85 print >>sys.stderr, "Error. Retrying with network use = full"
86 policy.network_use = model.network_full
87 policy.recalculate()
88 try:
89 execute()
90 except model.SafeException, ex:
91 print >>sys.stderr, ex
92 else:
93 print >>sys.stderr, ex