Only show two decimal places for download progress percentages.
[zeroinstall.git] / zeroinstall / 0launch-gui / compile.py
blob9fb0d64bbbad9fa2e817c716a7da9d5dbed19f05
1 import os, popen2
2 import gtk, gobject
3 import dialog
4 from logging import info
6 from zeroinstall.injector import reader, iface_cache, model
7 from zeroinstall.injector.policy import Policy
9 XMLNS_0COMPILE = 'http://zero-install.sourceforge.net/2006/namespaces/0compile'
11 class Command:
12 def __init__(self):
13 self.child = None
14 self.error = ""
16 def run(self, command, success):
17 assert self.child is None
18 self.success = success
19 self.child = popen2.Popen4(command)
20 self.child.tochild.close()
21 gobject.io_add_watch(self.child.fromchild, gobject.IO_IN | gobject.IO_HUP, self.got_data)
23 def got_data(self, src, cond):
24 data = os.read(src.fileno(), 100)
25 if data:
26 self.error += data
27 return True
28 else:
29 status = self.child.wait()
30 self.child = None
32 if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
33 self.success()
34 else:
35 if os.WIFEXITED(status):
36 status = os.WEXITSTATUS(status)
37 if status == 1 and not self.error:
38 return False # Cancelled
39 dialog.alert(None, "Command failed with exit code %d:\n%s\n" %
40 (status, self.error))
41 else:
42 dialog.alert(None, "Command failed:\n%s\n" % self.error)
43 return False
45 def compile(policy, interface):
46 def add_feed():
47 # A new local feed may have been registered, so update the interface from the cache
48 info("0compile command completed successfully. Reloading interface details.")
49 reader.update_from_cache(interface)
50 policy.recalculate()
52 def build():
53 # Get the chosen versions
54 src_policy = Policy(interface.uri, src = True)
55 src_policy.freshness = 0
57 src_policy.recalculate()
58 if not src_policy.ready:
59 raise Exception('Internal error: required source components not found!')
61 root_iface = iface_cache.iface_cache.get_interface(src_policy.root)
62 impl = src_policy.implementation[root_iface]
63 min_version = impl.metadata.get(XMLNS_0COMPILE + ' min-version', None)
64 if not min_version: min_version = '0.4'
65 # Check the syntax is valid and the version is high enough
66 if model.parse_version(min_version) < model.parse_version('0.4'):
67 min_version = '0.4'
69 # Do the whole build-and-register-feed
70 c = Command()
71 c.run(("0launch",
72 '--not-before=' + min_version,
73 "http://0install.net/2006/interfaces/0compile.xml",
74 'gui',
75 '--no-prompt',
76 interface.uri), add_feed)
78 # Prompt user to choose source version
79 c = Command()
80 c.run(['0launch', '--gui', '--source', '--download-only', interface.uri], build)