Skip to content

Commit 254e2bf

Browse files
tiranclaude
andcommitted
refactor(build_environment): accept sdist_root_dir and req in BuildEnvironment
Replace the generic `parent_dir` parameter with `sdist_root_dir` and `req` so that `BuildEnvironment` has the context it needs for future sandboxing and environment filtering. All arguments are now keyword-only and follow the common `ctx, req, sdist_root_dir` order. Part of the sandboxing proposal. See: #1019 Co-Authored-By: Claude <claude@anthropic.com> Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 973c9e4 commit 254e2bf

7 files changed

Lines changed: 57 additions & 14 deletions

File tree

src/fromager/bootstrapper/_prepare_source.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ def run(self, bt: Bootstrapper) -> list[Phase]:
155155

156156
wi.build_env = build_environment.BuildEnvironment(
157157
ctx=bt.ctx,
158-
parent_dir=sdist_root_dir.parent,
158+
req=wi.req,
159+
sdist_root_dir=sdist_root_dir,
159160
)
160161

161162
# Get build system dependencies

src/fromager/build_environment.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,36 @@ class BuildEnvironment:
7878

7979
def __init__(
8080
self,
81+
*,
8182
ctx: context.WorkContext,
82-
parent_dir: pathlib.Path,
83+
req: Requirement,
84+
sdist_root_dir: pathlib.Path,
8385
):
86+
"""Create a build environment for an sdist.
87+
88+
Args:
89+
ctx: the work context
90+
req: the requirement being built
91+
sdist_root_dir: root directory of the unpacked sdist
92+
"""
8493
self._ctx = ctx
85-
self.path = parent_dir.absolute() / f"build-{platform.python_version()}"
94+
self._req = req
95+
self._sdist_root_dir = sdist_root_dir
96+
self.path = sdist_root_dir.parent.absolute().joinpath(
97+
f"build-{platform.python_version()}"
98+
)
8699
self._createenv()
87100

101+
@property
102+
def sdist_root_dir(self) -> pathlib.Path:
103+
"""Root directory of the unpacked sdist."""
104+
return self._sdist_root_dir
105+
106+
@property
107+
def req(self) -> Requirement:
108+
"""The requirement being built."""
109+
return self._req
110+
88111
@property
89112
def python(self) -> pathlib.Path:
90113
"""Path to Python interpreter in virtual env"""
@@ -257,7 +280,8 @@ def prepare_build_environment(
257280

258281
build_env = BuildEnvironment(
259282
ctx=ctx,
260-
parent_dir=sdist_root_dir.parent,
283+
req=req,
284+
sdist_root_dir=sdist_root_dir,
261285
)
262286

263287
build_system_dependencies = dependencies.get_build_system_dependencies(

src/fromager/commands/step.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def build_sdist(
123123
source_root_dir = _find_source_root_dir(wkctx, wkctx.work_dir, req, dist_version)
124124
build_env = build_environment.BuildEnvironment(
125125
ctx=wkctx,
126-
parent_dir=source_root_dir.parent,
126+
req=req,
127+
sdist_root_dir=source_root_dir,
127128
)
128129
sdist_filename = sources.build_sdist(
129130
ctx=wkctx,
@@ -218,7 +219,8 @@ def build_wheel(
218219
server.start_wheel_server(wkctx)
219220
build_env = build_environment.BuildEnvironment(
220221
ctx=wkctx,
221-
parent_dir=source_root_dir.parent,
222+
req=req,
223+
sdist_root_dir=source_root_dir,
222224
)
223225
wheel_filename = wheels.build_wheel(
224226
ctx=wkctx,

tests/test_bootstrapper_iterative.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,8 @@ def test_source_no_cache_uses_background_result(
14311431
assert result[1] is mock_dep_item
14321432
mock_build_env_cls.assert_called_once_with(
14331433
ctx=bt.ctx,
1434-
parent_dir=sdist_root.parent,
1434+
req=wi.req,
1435+
sdist_root_dir=sdist_root,
14351436
)
14361437
mock_create_items.assert_called_once_with(
14371438
wi.build_system_deps,

tests/test_dependencies.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,12 @@ def test_get_build_backend_dependencies(
191191
tmp_context.wheel_server_url = "https://pypi.org/simple"
192192

193193
req = Requirement("fromager")
194+
sdist_root = tmp_path / "fromager-1.0.0"
195+
sdist_root.mkdir()
194196
build_env = build_environment.BuildEnvironment(
195197
ctx=tmp_context,
196-
parent_dir=tmp_path,
198+
req=req,
199+
sdist_root_dir=sdist_root,
197200
)
198201
build_system_dependencies = dependencies.get_build_system_dependencies(
199202
ctx=tmp_context,
@@ -223,9 +226,11 @@ def test_get_build_backend_dependencies_cached(
223226
req_file = tmp_path / "build-backend-requirements.txt"
224227
req_file.write_text("foo==1.0")
225228

229+
req = Requirement("fromager")
226230
build_env = build_environment.BuildEnvironment(
227231
ctx=tmp_context,
228-
parent_dir=tmp_path,
232+
req=req,
233+
sdist_root_dir=sdist_root_dir,
229234
)
230235
results = dependencies.get_build_backend_dependencies(
231236
ctx=tmp_context,
@@ -249,9 +254,12 @@ def test_get_build_sdist_dependencies(
249254
tmp_context.wheel_server_url = "https://pypi.org/simple"
250255

251256
req = Requirement("fromager")
257+
sdist_root = tmp_path / "fromager-1.0.0"
258+
sdist_root.mkdir()
252259
build_env = build_environment.BuildEnvironment(
253260
ctx=tmp_context,
254-
parent_dir=tmp_path,
261+
req=req,
262+
sdist_root_dir=sdist_root,
255263
)
256264
build_system_dependencies = dependencies.get_build_system_dependencies(
257265
ctx=tmp_context,
@@ -284,7 +292,8 @@ def test_get_build_sdist_dependencies_cached(
284292
req = Requirement("fromager")
285293
build_env = build_environment.BuildEnvironment(
286294
ctx=tmp_context,
287-
parent_dir=tmp_path,
295+
req=req,
296+
sdist_root_dir=sdist_root_dir,
288297
)
289298
results = dependencies.get_build_sdist_dependencies(
290299
ctx=tmp_context,

tests/test_packagesettings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,12 @@ def test_pbi_test_pkg_extra_environ(
322322
)
323323
assert "__version__" not in result
324324

325+
sdist_root = tmp_path / "test-pkg-1.0.0"
326+
sdist_root.mkdir()
325327
build_env = build_environment.BuildEnvironment(
326-
testdata_context,
327-
parent_dir=tmp_path,
328+
ctx=testdata_context,
329+
req=Requirement("test-pkg"),
330+
sdist_root_dir=sdist_root,
328331
)
329332
result = pbi.get_extra_environ(
330333
template_env={"EXTRA": "spam", "PATH": "/sbin:/bin"},

tests/test_wheels.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ def test_default_build_wheel(
1818
testdata_context: context.WorkContext,
1919
) -> None:
2020
req = Requirement("test_pkg")
21+
sdist_root = tmp_path / "test_pkg-1.0"
22+
sdist_root.mkdir()
2123
build_env = build_environment.BuildEnvironment(
2224
ctx=testdata_context,
23-
parent_dir=tmp_path,
25+
req=req,
26+
sdist_root_dir=sdist_root,
2427
)
2528
pbi = testdata_context.package_build_info(req)
2629
assert pbi.config_settings

0 commit comments

Comments
 (0)