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