Update year to 2009 in various places
[zeroinstall/zeroinstall-rsl.git] / zeroinstall / 0launch-gui / compile.py
blob2399afdef5e293182358431c17613ccbdf4dac37
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 %d:\n%s\n" %
44 (status, self.error))
45 else:
46 dialog.alert(None, "Command failed:\n%s\n" % self.error)
47 return False
49 def compile(policy, interface):
50 def add_feed():
51 # A new local feed may have been registered, so update the interface from the cache
52 info("0compile command completed successfully. Reloading interface details.")
53 reader.update_from_cache(interface)
54 policy.recalculate()
56 def build():
57 # Get the chosen versions
58 src_policy = Policy(interface.uri, src = True)
59 src_policy.freshness = 0
61 src_policy.recalculate()
62 if not src_policy.ready:
63 raise Exception('Internal error: required source components not found!')
65 root_iface = iface_cache.iface_cache.get_interface(src_policy.root)
66 impl = src_policy.implementation[root_iface]
67 min_version = impl.metadata.get(XMLNS_0COMPILE + ' min-version', None)
68 if not min_version: min_version = '0.4'
69 # Check the syntax is valid and the version is high enough
70 if model.parse_version(min_version) < model.parse_version('0.4'):
71 min_version = '0.4'
73 # Do the whole build-and-register-feed
74 c = Command()
75 c.run(("0launch",
76 '--message', 'Download the 0compile tool, to compile the source code',
77 '--not-before=' + min_version,
78 "http://0install.net/2006/interfaces/0compile.xml",
79 'gui',
80 '--no-prompt',
81 interface.uri), add_feed)
83 # Prompt user to choose source version
84 c = Command()
85 c.run(['0launch',
86 '--message', 'Download the source code to be compiled',
87 '--gui', '--source', '--download-only', interface.uri], build)