Release 0.14.2
[0release.git] / compile.py
blob573f86d4e2c869ece67b9d2ee8d99ec38f10f07f
1 # Copyright (C) 2009, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import tempfile, shutil, os
5 import ConfigParser
6 from logging import info
7 from zeroinstall.support import basedir
9 import support
11 COMPILE = 'http://0install.net/2006/interfaces/0compile.xml'
13 class Compiler:
14 def __init__(self, options, src_feed_name):
15 self.src_feed_name = src_feed_name
16 self.src_feed = support.load_feed(src_feed_name)
17 self.archive_dir_public_url = options.archive_dir_public_url
18 assert options.archive_dir_public_url
20 self.config = ConfigParser.RawConfigParser()
22 # Start with a default configuration
23 self.config.add_section('global')
24 self.config.set('global', 'builders', 'host')
26 self.config.add_section('builder-host')
27 self.config.set('builder-host', 'build', '0launch --not-before 0.10 http://0install.net/2007/interfaces/0release.xml --build-slave "$@"')
29 self.src_impl = support.get_singleton_impl(self.src_feed)
30 if self.src_impl.arch and self.src_impl.arch.endswith('-src'):
31 path = basedir.load_first_config('0install.net', '0release', 'builders.conf')
32 if path:
33 info("Loading configuration file '%s'", path)
34 self.config.read(path)
35 else:
36 info("No builders.conf configuration; will build a binary for this host only")
38 if options.builders is not None:
39 builders = options.builders
40 else:
41 builders = self.config.get('global', 'builders').strip()
42 if builders:
43 self.targets = [x.strip() for x in builders.split(',')]
44 info("%d build targets configured: %s", len(self.targets), self.targets)
45 else:
46 self.targets = []
47 info("No builders set; no binaries will be built")
48 else:
49 self.targets = []
51 # We run the build in a sub-process. The idea is that the build may need to run
52 # on a different machine.
53 def build_binaries(self):
54 if not self.targets: return
56 print "Source package, so generating binaries..."
58 archive_file = support.get_archive_basename(self.src_impl)
60 for target in self.targets:
61 start = self.get('builder-' + target, 'start', None)
62 command = self.config.get('builder-' + target, 'build')
63 stop = self.get('builder-' + target, 'stop', None)
65 binary_feed = 'binary-' + target + '.xml'
66 if os.path.exists(binary_feed):
67 print "Feed %s already exists; not rebuilding" % binary_feed
68 else:
69 print "\nBuilding binary with builder '%s' ...\n" % target
71 if start: support.show_and_run(start, [])
72 try:
73 support.show_and_run(command, [os.path.basename(self.src_feed_name), archive_file, self.archive_dir_public_url, binary_feed + '.new'])
74 finally:
75 if stop: support.show_and_run(stop, [])
77 bin_feed = support.load_feed(binary_feed + '.new')
78 bin_impl = support.get_singleton_impl(bin_feed)
79 bin_archive_file = support.get_archive_basename(bin_impl)
80 bin_size = bin_impl.download_sources[0].size
82 assert os.path.exists(bin_archive_file), "Compiled binary '%s' not found!" % os.path.abspath(bin_archive_file)
83 assert os.path.getsize(bin_archive_file) == bin_size, "Compiled binary '%s' has wrong size!" % os.path.abspath(bin_archive_file)
85 os.rename(binary_feed + '.new', binary_feed)
87 def get_binary_feeds(self):
88 return ['binary-%s.xml' % target for target in self.targets]
90 def get(self, section, option, default):
91 try:
92 return self.config.get(section, option)
93 except ConfigParser.NoOptionError:
94 return default
96 # This is the actual build process, running on the build machine
97 def build_slave(src_feed, archive_file, archive_dir_public_url, target_feed):
98 feed = support.load_feed(src_feed)
100 src_feed = os.path.abspath(src_feed)
101 archive_file = os.path.abspath(archive_file)
102 target_feed = os.path.abspath(target_feed)
104 impl, = feed.implementations.values()
106 tmpdir = tempfile.mkdtemp(prefix = '0release-')
107 try:
108 os.chdir(tmpdir)
109 depdir = os.path.join(tmpdir, 'dependencies')
110 os.mkdir(depdir)
112 support.unpack_tarball(archive_file)
113 os.rename(impl.download_sources[0].extract, os.path.join(depdir, impl.id))
115 config = ConfigParser.RawConfigParser()
116 config.add_section('compile')
117 config.set('compile', 'download-base-url', archive_dir_public_url)
118 config.set('compile', 'version-modifier', '')
119 config.set('compile', 'interface', src_feed)
120 config.set('compile', 'selections', '')
121 config.set('compile', 'metadir', '0install')
122 stream = open(os.path.join(tmpdir, '0compile.properties'), 'w')
123 try:
124 config.write(stream)
125 finally:
126 stream.close()
128 support.check_call(['0launch', '--not-before=0.30', COMPILE, 'build'], cwd = tmpdir)
129 support.check_call(['0launch', '--not-before=0.30', COMPILE, 'publish', '--target-feed', target_feed], cwd = tmpdir)
131 # TODO: run unit-tests
133 feed = support.load_feed(target_feed)
134 impl = support.get_singleton_impl(feed)
135 archive_file = support.get_archive_basename(impl)
137 shutil.move(archive_file, os.path.join(os.path.dirname(target_feed), archive_file))
138 except:
139 print "\nLeaving temporary directory %s for inspection...\n" % tmpdir
140 raise
141 else:
142 shutil.rmtree(tmpdir)