Skip to content

Commit 0ed6e75

Browse files
authored
Merge pull request #574 from tiran/refactor-hook-caller
Refactor get_build_backend_hook_caller() API
2 parents 66903ff + d295758 commit 0ed6e75

4 files changed

Lines changed: 46 additions & 36 deletions

File tree

src/fromager/build_environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def prepare_build_environment(
217217
ctx: context.WorkContext,
218218
req: Requirement,
219219
sdist_root_dir: pathlib.Path,
220-
) -> pathlib.Path:
220+
) -> BuildEnvironment:
221221
logger.info(f"{req.name}: preparing build environment")
222222

223223
next_req_type = RequirementType.BUILD_SYSTEM
@@ -314,7 +314,7 @@ def prepare_build_environment(
314314
| build_sdist_dependencies,
315315
) from err
316316
raise
317-
return build_env.path
317+
return build_env
318318

319319

320320
def maybe_install(

src/fromager/commands/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,9 @@ def _build(
370370
)
371371

372372
# Build environment
373-
build_environment.prepare_build_environment(
373+
build_env = build_environment.prepare_build_environment(
374374
ctx=wkctx, req=req, sdist_root_dir=source_root_dir
375375
)
376-
build_env = build_environment.BuildEnvironment(wkctx, source_root_dir.parent, None)
377376

378377
# Make a new source distribution, in case we patched the code.
379378
sdist_filename = sources.build_sdist(

src/fromager/dependencies.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ def default_get_build_backend_dependencies(
140140
Defaults to result of hook call
141141
:meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_wheel`
142142
"""
143-
pyproject_toml = get_pyproject_contents(build_dir)
144143
hook_caller = get_build_backend_hook_caller(
145-
build_dir, pyproject_toml, override_environ=extra_environ
144+
ctx=ctx,
145+
req=req,
146+
build_dir=build_dir,
147+
override_environ=extra_environ,
146148
)
147149
return hook_caller.get_requires_for_build_wheel()
148150

@@ -197,9 +199,11 @@ def default_get_build_sdist_dependencies(
197199
Defaults to result of hook call
198200
:meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_wheel`
199201
"""
200-
pyproject_toml = get_pyproject_contents(build_dir)
201202
hook_caller = get_build_backend_hook_caller(
202-
build_dir, pyproject_toml, override_environ=extra_environ
203+
ctx=ctx,
204+
req=req,
205+
build_dir=build_dir,
206+
override_environ=extra_environ,
203207
)
204208
return hook_caller.get_requires_for_build_wheel()
205209

@@ -220,13 +224,12 @@ def get_install_dependencies_of_sdist(
220224
logger.info(
221225
f"{req.name}: getting install requirements for {req} from sdist in {build_dir}"
222226
)
223-
pyproject_toml = get_pyproject_contents(build_dir)
224227
extra_environ = pbi.get_extra_environ()
225228
hook_caller = get_build_backend_hook_caller(
226-
build_dir,
227-
pyproject_toml,
229+
ctx=ctx,
230+
req=req,
231+
build_dir=build_dir,
228232
override_environ=extra_environ,
229-
network_isolation=ctx.network_isolation,
230233
build_env=build_env,
231234
)
232235
with tempfile.TemporaryDirectory() as tmp_dir:
@@ -296,39 +299,44 @@ def get_build_backend(pyproject_toml: dict[str, typing.Any]) -> dict[str, typing
296299

297300

298301
def get_build_backend_hook_caller(
299-
sdist_root_dir: pathlib.Path,
300-
pyproject_toml: dict[str, typing.Any],
301-
override_environ: dict[str, typing.Any],
302302
*,
303-
network_isolation: bool = False,
303+
ctx: context.WorkContext,
304+
req: Requirement,
305+
build_dir: pathlib.Path,
306+
override_environ: dict[str, typing.Any],
304307
build_env: build_environment.BuildEnvironment | None = None,
305308
) -> pyproject_hooks.BuildBackendHookCaller:
306-
backend = get_build_backend(pyproject_toml)
309+
"""Create pyproject_hooks build backend caller"""
307310

308311
def _run_hook_with_extra_environ(
309312
cmd: typing.Sequence[str],
310313
cwd: str | None = None,
311314
extra_environ: typing.Mapping[str, str] | None = None,
312315
) -> None:
313316
"""The BuildBackendHookCaller is going to pass extra_environ
314-
and our build system may want to set some values, too. Merge
315-
the 2 sets of values before calling the actual runner function.
317+
and our build system may want to set some values, too. The hook
318+
also needs env vars from the build environment's virtualenv. Merge
319+
the 3 sets of values before calling the actual runner function.
316320
"""
317-
full_environ: dict[str, typing.Any] = {}
318-
if extra_environ is not None:
319-
full_environ.update(extra_environ)
320-
full_environ.update(override_environ)
321+
if typing.TYPE_CHECKING:
322+
assert build_env is not None
323+
extra_environ = dict(extra_environ) if extra_environ else {}
324+
extra_environ.update(override_environ)
325+
if build_env is not None:
326+
extra_environ.update(build_env.get_venv_environ(template_env=extra_environ))
321327
external_commands.run(
322328
cmd,
323329
cwd=cwd,
324-
extra_environ=full_environ,
325-
network_isolation=network_isolation,
330+
extra_environ=extra_environ,
331+
network_isolation=ctx.network_isolation,
326332
)
327333

334+
pyproject_toml = get_pyproject_contents(build_dir)
335+
backend = get_build_backend(pyproject_toml)
328336
python_executable = str(build_env.python) if build_env is not None else None
329337

330338
return pyproject_hooks.BuildBackendHookCaller(
331-
source_dir=str(sdist_root_dir),
339+
source_dir=str(build_dir),
332340
build_backend=backend["build-backend"],
333341
backend_path=backend["backend-path"],
334342
runner=_run_hook_with_extra_environ,

src/fromager/sources.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,15 @@ def _get_version_from_package_metadata(
255255
req: Requirement,
256256
source_dir: str,
257257
) -> Version:
258+
pbi = ctx.package_build_info(req)
259+
build_dir = pbi.build_dir(source_dir)
258260
logger.info(f"{req.name}: generating metadata to get version")
259-
pyproject_toml = dependencies.get_pyproject_contents(source_dir)
261+
260262
hook_caller = dependencies.get_build_backend_hook_caller(
261-
source_dir,
262-
pyproject_toml,
263-
{},
264-
network_isolation=ctx.network_isolation,
263+
ctx=ctx,
264+
req=req,
265+
build_dir=build_dir,
266+
override_environ={},
265267
)
266268
metadata_dir_base = hook_caller.prepare_metadata_for_build_wheel(
267269
metadata_directory=source_dir.parent,
@@ -679,12 +681,13 @@ def pep517_build_sdist(
679681
version: Version,
680682
) -> pathlib.Path:
681683
"""Use the PEP 517 API to build a source distribution from a modified source tree."""
682-
pyproject_toml = dependencies.get_pyproject_contents(sdist_root_dir)
684+
pbi = ctx.package_build_info(req)
685+
build_dir = pbi.build_dir(sdist_root_dir)
683686
hook_caller = dependencies.get_build_backend_hook_caller(
684-
sdist_root_dir,
685-
pyproject_toml,
686-
extra_environ,
687-
network_isolation=ctx.network_isolation,
687+
ctx=ctx,
688+
req=req,
689+
build_dir=build_dir,
690+
override_environ=extra_environ,
688691
)
689692
sdist_filename = hook_caller.build_sdist(ctx.sdists_builds)
690693
return ctx.sdists_builds / sdist_filename

0 commit comments

Comments
 (0)