Better check for Debian-style distribution
[zeroinstall.git] / zeroinstall / 0launch-gui / compile.py
bloba12a912e99be8aae42df829be45a25e2139c29aa
1 # Copyright (C) 2008, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os, popen2
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 = popen2.Popen4(command)
23 self.child.tochild.close()
24 gobject.io_add_watch(self.child.fromchild, gobject.IO_IN | gobject.IO_HUP, self.got_data)
26 def got_data(self, src, cond):
27 data = os.read(src.fileno(), 100)
28 if data:
29 self.error += data
30 return True
31 else:
32 status = self.child.wait()
33 self.child = None
35 if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
36 self.success()
37 else:
38 if os.WIFEXITED(status):
39 status = os.WEXITSTATUS(status)
40 if status == 1 and not self.error:
41 return False # Cancelled
42 dialog.alert(None, "Command failed with exit code %d:\n%s\n" %
43 (status, self.error))
44 else:
45 dialog.alert(None, "Command failed:\n%s\n" % self.error)
46 return False
48 def compile(policy, interface):
49 def add_feed():
50 # A new local feed may have been registered, so update the interface from the cache
51 info("0compile command completed successfully. Reloading interface details.")
52 reader.update_from_cache(interface)
53 policy.recalculate()
55 def build():
56 # Get the chosen versions
57 src_policy = Policy(interface.uri, src = True)
58 src_policy.freshness = 0
60 src_policy.recalculate()
61 if not src_policy.ready:
62 raise Exception('Internal error: required source components not found!')
64 root_iface = iface_cache.iface_cache.get_interface(src_policy.root)
65 impl = src_policy.implementation[root_iface]
66 min_version = impl.metadata.get(XMLNS_0COMPILE + ' min-version', None)
67 if not min_version: min_version = '0.4'
68 # Check the syntax is valid and the version is high enough
69 if model.parse_version(min_version) < model.parse_version('0.4'):
70 min_version = '0.4'
72 # Do the whole build-and-register-feed
73 c = Command()
74 c.run(("0launch",
75 '--not-before=' + min_version,
76 "http://0install.net/2006/interfaces/0compile.xml",
77 'gui',
78 '--no-prompt',
79 interface.uri), add_feed)
81 # Prompt user to choose source version
82 c = Command()
83 c.run(['0launch', '--gui', '--source', '--download-only', interface.uri], build)