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