New icon for Zero Install
[zeroinstall.git] / zeroinstall / 0launch-gui / compile.py
blobbeeff1d59ed2fc4365321839411ec8b8ac174b97
1 # Copyright (C) 2009, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, subprocess
5 import gobject
6 import dialog
7 from logging import info
9 from zeroinstall.injector import reader, iface_cache, model
10 from zeroinstall.injector.policy import Policy
12 XMLNS_0COMPILE = 'http://zero-install.sourceforge.net/2006/namespaces/0compile'
14 class Command:
15 def __init__(self):
16 self.child = None
17 self.error = ""
19 def run(self, command, success):
20 assert self.child is None
21 self.success = success
22 self.child = subprocess.Popen(command,
23 stdout = subprocess.PIPE,
24 stderr = subprocess.STDOUT)
25 gobject.io_add_watch(self.child.stdout, gobject.IO_IN | gobject.IO_HUP, self.got_data)
27 def got_data(self, src, cond):
28 data = os.read(src.fileno(), 100)
29 if data:
30 self.error += data
31 return True
32 else:
33 status = self.child.wait()
34 self.child = None
36 if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
37 self.success()
38 else:
39 if os.WIFEXITED(status):
40 status = os.WEXITSTATUS(status)
41 if status == 1 and not self.error:
42 return False # Cancelled
43 dialog.alert(None, _("Command failed with exit code %(status)d:\n%(error)s\n") %
44 {'status': status, 'error': self.error})
45 else:
46 dialog.alert(None, _("Command failed:\n%s\n") % self.error)
47 return False
49 def compile(policy, interface, autocompile = False):
50 our_min_version = '0.18' # The oldest version of 0compile we support
52 def add_feed():
53 # A new local feed may have been registered, so update the interface from the cache
54 info(_("0compile command completed successfully. Reloading interface details."))
55 reader.update_from_cache(interface)
56 policy.recalculate()
58 def build():
59 # Get the chosen versions
60 src_policy = Policy(interface.uri, src = True)
61 src_policy.freshness = 0
63 src_policy.recalculate()
64 if not src_policy.ready:
65 raise Exception(_('Internal error: required source components not found!'))
67 root_iface = iface_cache.iface_cache.get_interface(src_policy.root)
68 impl = src_policy.implementation[root_iface]
70 min_version = impl.metadata.get(XMLNS_0COMPILE + ' min-version', our_min_version)
71 # Check the syntax is valid and the version is high enough
72 if model.parse_version(min_version) < model.parse_version(our_min_version):
73 min_version = our_min_version
75 # Do the whole build-and-register-feed
76 c = Command()
77 c.run(("0launch",
78 '--message', _('Download the 0compile tool, to compile the source code'),
79 '--not-before=' + min_version,
80 "http://0install.net/2006/interfaces/0compile.xml",
81 'gui',
82 '--no-prompt',
83 interface.uri), add_feed)
85 if autocompile:
86 c = Command()
87 c.run(("0launch",
88 '--message', 'Download the 0compile tool, to compile the source code',
89 '--not-before=' + our_min_version,
90 "http://0install.net/2006/interfaces/0compile.xml",
91 'autocompile',
92 '--gui',
93 interface.uri), add_feed)
94 else:
95 # Prompt user to choose source version
96 c = Command()
97 c.run(['0launch',
98 '--message', _('Download the source code to be compiled'),
99 '--gui', '--source', '--download-only', interface.uri], build)