forked from astral-sh/python-build-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildenv.py
280 lines (220 loc) · 7.92 KB
/
buildenv.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import contextlib
import fnmatch
import io
import os
import pathlib
import shutil
import tarfile
import tempfile
from .docker import container_exec, container_get_archive, copy_file_to_container
from .downloads import DOWNLOADS
from .logging import log
from .utils import (
clang_toolchain,
create_tar_from_directory,
exec_and_log,
extract_tar_to_directory,
normalize_tar_archive,
)
class ContainerContext(object):
def __init__(self, container):
self.container = container
self.tools_path = "/tools"
@property
def is_isolated(self):
return True
def copy_file(self, source: pathlib.Path, dest_path=None, dest_name=None):
dest_name = dest_name or source.name
dest_path = dest_path or "/build"
copy_file_to_container(source, self.container, dest_path, dest_name)
def install_toolchain_archive(
self, build_dir, package_name, host_platform, version=None
):
entry = DOWNLOADS[package_name]
basename = "%s-%s-%s.tar" % (
package_name,
version or entry["version"],
host_platform,
)
p = build_dir / basename
self.copy_file(p)
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name])
def install_artifact_archive(
self, build_dir, package_name, target_triple, build_options
):
entry = DOWNLOADS[package_name]
basename = "%s-%s-%s-%s.tar" % (
package_name,
entry["version"],
target_triple,
build_options,
)
p = build_dir / basename
self.copy_file(p)
self.run(["/bin/tar", "-C", "/tools", "-xf", "/build/%s" % p.name])
def install_toolchain(
self,
build_dir,
host_platform,
target_triple: str,
binutils=False,
musl=False,
clang=False,
):
if binutils:
self.install_toolchain_archive(build_dir, "binutils", host_platform)
if clang:
self.install_toolchain_archive(
build_dir, clang_toolchain(host_platform, target_triple), host_platform
)
if musl:
self.install_toolchain_archive(build_dir, "musl", host_platform)
def run(self, program, user="build", environment=None):
if isinstance(program, str) and not program.startswith("/"):
program = "/build/%s" % program
container_exec(self.container, program, user=user, environment=environment)
def get_tools_archive(self, dest, name):
log("copying container files to %s" % dest)
data = container_get_archive(self.container, "/build/out/tools/%s" % name)
with open(dest, "wb") as fh:
fh.write(data)
def get_file(self, path):
log("retrieving container file %s" % path)
data = io.BytesIO(container_get_archive(self.container, "/build/%s" % path))
with tarfile.open(fileobj=data) as tf:
for ti in tf:
return tf.extractfile(ti).read()
raise Exception("file not found")
def get_output_archive(self, path=None, as_tar=False):
p = "/build/out"
if path:
p += "/%s" % path
data = container_get_archive(self.container, p)
data = io.BytesIO(data)
data = normalize_tar_archive(data)
if as_tar:
return tarfile.open(fileobj=data)
else:
return data.getvalue()
def find_output_files(self, base_path, pattern):
command = ["/usr/bin/find", "/build/out/%s" % base_path, "-name", pattern]
for line in self.container.exec_run(command, user="build")[1].splitlines():
if not line.strip():
continue
yield line[len("/build/out/%s/" % base_path) :].decode("ascii")
class TempdirContext(object):
def __init__(self, td):
self.td = pathlib.Path(td)
self.tools_path = str(self.td / "tools")
@property
def is_isolated(self):
return False
def copy_file(self, source: pathlib.Path, dest_path=None, dest_name=None):
if dest_path:
dest_dir = self.td / dest_path
else:
dest_dir = self.td
dest_dir.mkdir(exist_ok=True)
dest_name = dest_name or source.name
log("copying %s to %s/%s" % (source, dest_dir, dest_name))
shutil.copy(source, dest_dir / dest_name)
def install_toolchain_archive(
self, build_dir, package_name, host_platform, version=None
):
entry = DOWNLOADS[package_name]
basename = "%s-%s-%s.tar" % (
package_name,
version or entry["version"],
host_platform,
)
p = build_dir / basename
dest_path = self.td / "tools"
log("extracting %s to %s" % (p, dest_path))
extract_tar_to_directory(p, dest_path)
def install_artifact_archive(
self, build_dir, package_name, target_triple, build_options
):
entry = DOWNLOADS[package_name]
basename = "%s-%s-%s-%s.tar" % (
package_name,
entry["version"],
target_triple,
build_options,
)
p = build_dir / basename
dest_path = self.td / "tools"
log("extracting %s to %s" % (p, dest_path))
extract_tar_to_directory(p, dest_path)
def install_toolchain(
self,
build_dir,
platform,
target_triple,
binutils=False,
musl=False,
clang=False,
):
if binutils:
self.install_toolchain_archive(build_dir, "binutils", platform)
if clang:
self.install_toolchain_archive(
build_dir, clang_toolchain(platform, target_triple), platform
)
if musl:
self.install_toolchain_archive(build_dir, "musl", platform)
def run(self, program, user="build", environment=None):
if user != "build":
raise Exception("cannot change user in temp directory builds")
if isinstance(program, str) and not program.startswith("/"):
program = str(self.td / program)
exec_and_log(program, cwd=self.td, env=environment)
def get_tools_archive(self, dest, name):
log("copying built files to %s" % dest)
with dest.open("wb") as fh:
create_tar_from_directory(fh, self.td / "out" / "tools")
def get_file(self, path):
log("retrieving file %s" % path)
p = self.td / path
with p.open("rb") as fh:
return fh.read()
def get_output_archive(self, path, as_tar=False):
p = self.td / "out" / path
data = io.BytesIO()
create_tar_from_directory(data, p, path_prefix=p.parts[-1])
data.seek(0)
data = normalize_tar_archive(data)
if as_tar:
return tarfile.open(fileobj=data)
else:
return data.getvalue()
def find_output_files(self, base_path, pattern):
base = str(self.td / "out" / base_path)
for root, dirs, files in os.walk(base):
dirs.sort()
for f in sorted(files):
if fnmatch.fnmatch(f, pattern):
full = os.path.join(root, f)
yield full[len(base) + 1 :]
@contextlib.contextmanager
def build_environment(client, image):
if client is not None:
container = client.containers.run(
image, command=["/bin/sleep", "86400"], detach=True
)
td = None
context = ContainerContext(container)
else:
container = None
td = tempfile.TemporaryDirectory()
context = TempdirContext(td.name)
try:
yield context
finally:
if container:
container.stop(timeout=0)
container.remove()
else:
td.cleanup()