Skip to content

Commit 5191971

Browse files
committed
unix: create a new API for retrieving output files
This is all we used run_output() for.
1 parent 279df3a commit 5191971

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

cpython-unix/build.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,8 @@ def python_build_info(
323323
core_objs = set()
324324
modules_objs = set()
325325

326-
res = build_env.run_capture(
327-
["/usr/bin/find", "/build/out/python/build", "-name", "*.o"], user="build"
328-
)
329-
330-
for line in res[1].splitlines():
331-
if not line.strip():
332-
continue
333-
334-
p = pathlib.Path(os.fsdecode(line))
335-
rel_path = p.relative_to("/build/out/python")
326+
for f in build_env.find_output_files("python/build", "*.o"):
327+
rel_path = pathlib.Path(f)
336328

337329
if rel_path.parts[1] in ("Objects", "Parser", "Python"):
338330
core_objs.add(rel_path)
@@ -346,14 +338,8 @@ def python_build_info(
346338

347339
libraries = set()
348340

349-
for line in build_env.run_capture(
350-
["/usr/bin/find", "/build/out/python/build/lib", "-name", "*.a"], user="build"
351-
)[1].splitlines():
352-
353-
if not line.strip():
354-
continue
355-
356-
f = line[len("/build/out/python/build/lib/") :].decode("ascii")
341+
for f in build_env.find_output_files("python/build/lib", "*.a"):
342+
f = f[len("python/build/lib/") :]
357343

358344
# Strip "lib" prefix and ".a" suffix.
359345
libname = f[3:-2]

pythonbuild/buildenv.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
import contextlib
6+
import fnmatch
67
import io
8+
import os
79
import pathlib
810
import shutil
911
import tarfile
@@ -61,9 +63,6 @@ def run(self, program, user="build", environment=None):
6163

6264
container_exec(self.container, program, user=user, environment=environment)
6365

64-
def run_capture(self, command, user=None):
65-
return self.container.exec_run(command, user=user)
66-
6766
def get_tools_archive(self, dest, name):
6867
log("copying container files to %s" % dest)
6968
data = container_get_archive(self.container, "/build/out/tools/%s" % name)
@@ -80,6 +79,15 @@ def get_archive(self, path, as_tar=False):
8079
else:
8180
return data.getvalue()
8281

82+
def find_output_files(self, base_path, pattern):
83+
command = ["/usr/bin/find", "/build/out/%s" % base_path, "-name", pattern]
84+
85+
for line in self.container.exec_run(command, user="build")[1].splitlines():
86+
if not line.strip():
87+
continue
88+
89+
yield line[len("/build/out/%s/" % base_path) :].decode("ascii")
90+
8391

8492
class TempdirContext(object):
8593
def __init__(self, td):
@@ -143,6 +151,17 @@ def get_tools_archive(self, dest, name):
143151
with dest.open("wb") as fh:
144152
create_tar_from_directory(fh, self.td / "out" / "tools")
145153

154+
def find_output_files(self, base_path, pattern):
155+
base = str(self.td / "out" / base_path)
156+
157+
for root, dirs, files in os.walk(base):
158+
dirs.sort()
159+
160+
for f in sorted(files):
161+
if fnmatch.fnmatch(f, pattern):
162+
full = os.path.join(root, f)
163+
yield full[len(base) + 1 :]
164+
146165

147166
@contextlib.contextmanager
148167
def build_environment(client, image):

0 commit comments

Comments
 (0)