pyflakes
[zeroinstall.git] / zeroinstall / 0launch-gui / compile.py
blobcf0d636aefa1a08bbe7fbaf80235efb365eaefe2
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 StringIO import StringIO
9 from zeroinstall.injector import model, selections, qdom
11 XMLNS_0COMPILE = 'http://zero-install.sourceforge.net/2006/namespaces/0compile'
13 class Command:
14 def __init__(self):
15 self.child = None
16 self.error = ""
17 self.stdout = ""
18 self.watched_streams = 0
20 def run(self, command, success, get_stdout = False):
21 assert self.child is None
22 self.success = success
23 if get_stdout:
24 self.child = subprocess.Popen(command,
25 stdout = subprocess.PIPE,
26 stderr = subprocess.PIPE)
27 gobject.io_add_watch(self.child.stdout, gobject.IO_IN | gobject.IO_HUP, self.got_stdout)
28 gobject.io_add_watch(self.child.stderr, gobject.IO_IN | gobject.IO_HUP, self.got_errors)
29 self.watched_streams = 2
30 else:
31 self.child = subprocess.Popen(command,
32 stdout = subprocess.PIPE,
33 stderr = subprocess.STDOUT)
34 gobject.io_add_watch(self.child.stdout, gobject.IO_IN | gobject.IO_HUP, self.got_errors)
35 self.watched_streams = 1
37 def got_stdout(self, src, cond):
38 data = os.read(src.fileno(), 100)
39 if data:
40 self.stdout += data
41 return True
42 else:
43 self.done()
44 return False
46 def done(self):
47 self.watched_streams -= 1
48 if self.watched_streams == 0:
49 status = self.child.wait()
50 self.child = None
52 if status == 0:
53 self.success(self.stdout)
54 else:
55 if status == 1 and not self.error:
56 return False # Cancelled
57 dialog.alert(None, _("Command failed with exit code %(status)d:\n%(error)s\n") %
58 {'status': status, 'error': self.error})
60 def got_errors(self, src, cond):
61 data = os.read(src.fileno(), 100)
62 if data:
63 self.error += data
64 return True
65 else:
66 self.done()
67 return False
69 def compile(on_success, interface_uri, autocompile = False):
70 our_min_version = '0.18' # The oldest version of 0compile we support
72 def build(selections_xml):
73 # Get the chosen versions
74 sels = selections.Selections(qdom.parse(StringIO(selections_xml)))
76 impl = sels.selections[interface_uri]
78 min_version = impl.attrs.get(XMLNS_0COMPILE + ' min-version', our_min_version)
79 # Check the syntax is valid and the version is high enough
80 if model.parse_version(min_version) < model.parse_version(our_min_version):
81 min_version = our_min_version
83 # Do the whole build-and-register-feed
84 c = Command()
85 c.run(("0launch",
86 '--message', _('Download the 0compile tool, to compile the source code'),
87 '--not-before=' + min_version,
88 "http://0install.net/2006/interfaces/0compile.xml",
89 'gui',
90 interface_uri), lambda unused: on_success())
92 if autocompile:
93 c = Command()
94 c.run(("0launch",
95 '--message', 'Download the 0compile tool, to compile the source code',
96 '--not-before=' + our_min_version,
97 "http://0install.net/2006/interfaces/0compile.xml",
98 'autocompile',
99 '--gui',
100 interface_uri), lambda unused: on_success())
101 else:
102 # Prompt user to choose source version
103 c = Command()
104 c.run(['0install', 'download', '--xml',
105 '--message', _('Download the source code to be compiled'),
106 '--gui', '--source', '--', interface_uri], build, get_stdout = True)