Skip to content

Refactor get_build_backend_hook_caller() API #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/fromager/build_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def prepare_build_environment(
ctx: context.WorkContext,
req: Requirement,
sdist_root_dir: pathlib.Path,
) -> pathlib.Path:
) -> BuildEnvironment:
logger.info(f"{req.name}: preparing build environment")

next_req_type = RequirementType.BUILD_SYSTEM
Expand Down Expand Up @@ -300,7 +300,7 @@ def prepare_build_environment(
| build_sdist_dependencies,
) from err
raise
return build_env.path
return build_env


def maybe_install(
Expand Down
3 changes: 1 addition & 2 deletions src/fromager/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,9 @@ def _build(
)

# Build environment
build_environment.prepare_build_environment(
build_env = build_environment.prepare_build_environment(
ctx=wkctx, req=req, sdist_root_dir=source_root_dir
)
build_env = build_environment.BuildEnvironment(wkctx, source_root_dir.parent, None)

# Make a new source distribution, in case we patched the code.
sdist_filename = sources.build_sdist(
Expand Down
52 changes: 30 additions & 22 deletions src/fromager/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ def default_get_build_backend_dependencies(
Defaults to result of hook call
:meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_wheel`
"""
pyproject_toml = get_pyproject_contents(build_dir)
hook_caller = get_build_backend_hook_caller(
build_dir, pyproject_toml, override_environ=extra_environ
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ=extra_environ,
)
return hook_caller.get_requires_for_build_wheel()

Expand Down Expand Up @@ -197,9 +199,11 @@ def default_get_build_sdist_dependencies(
Defaults to result of hook call
:meth:`~pyproject_hooks.BuildBackendHookCaller.get_requires_for_build_wheel`
"""
pyproject_toml = get_pyproject_contents(build_dir)
hook_caller = get_build_backend_hook_caller(
build_dir, pyproject_toml, override_environ=extra_environ
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ=extra_environ,
)
return hook_caller.get_requires_for_build_wheel()

Expand All @@ -220,13 +224,12 @@ def get_install_dependencies_of_sdist(
logger.info(
f"{req.name}: getting install requirements for {req} from sdist in {build_dir}"
)
pyproject_toml = get_pyproject_contents(build_dir)
extra_environ = pbi.get_extra_environ()
hook_caller = get_build_backend_hook_caller(
build_dir,
pyproject_toml,
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ=extra_environ,
network_isolation=ctx.network_isolation,
build_env=build_env,
)
with tempfile.TemporaryDirectory() as tmp_dir:
Expand Down Expand Up @@ -296,39 +299,44 @@ def get_build_backend(pyproject_toml: dict[str, typing.Any]) -> dict[str, typing


def get_build_backend_hook_caller(
sdist_root_dir: pathlib.Path,
pyproject_toml: dict[str, typing.Any],
override_environ: dict[str, typing.Any],
*,
network_isolation: bool = False,
ctx: context.WorkContext,
req: Requirement,
build_dir: pathlib.Path,
override_environ: dict[str, typing.Any],
build_env: build_environment.BuildEnvironment | None = None,
) -> pyproject_hooks.BuildBackendHookCaller:
backend = get_build_backend(pyproject_toml)
"""Create pyproject_hooks build backend caller"""

def _run_hook_with_extra_environ(
cmd: typing.Sequence[str],
cwd: str | None = None,
extra_environ: typing.Mapping[str, str] | None = None,
) -> None:
"""The BuildBackendHookCaller is going to pass extra_environ
and our build system may want to set some values, too. Merge
the 2 sets of values before calling the actual runner function.
and our build system may want to set some values, too. The hook
also needs env vars from the build environment's virtualenv. Merge
the 3 sets of values before calling the actual runner function.
"""
full_environ: dict[str, typing.Any] = {}
if extra_environ is not None:
full_environ.update(extra_environ)
full_environ.update(override_environ)
if typing.TYPE_CHECKING:
assert build_env is not None
extra_environ = dict(extra_environ) if extra_environ else {}
extra_environ.update(override_environ)
if build_env is not None:
extra_environ.update(build_env.get_venv_environ(template_env=extra_environ))
external_commands.run(
cmd,
cwd=cwd,
extra_environ=full_environ,
network_isolation=network_isolation,
extra_environ=extra_environ,
network_isolation=ctx.network_isolation,
)

pyproject_toml = get_pyproject_contents(build_dir)
backend = get_build_backend(pyproject_toml)
python_executable = str(build_env.python) if build_env is not None else None

return pyproject_hooks.BuildBackendHookCaller(
source_dir=str(sdist_root_dir),
source_dir=str(build_dir),
build_backend=backend["build-backend"],
backend_path=backend["backend-path"],
runner=_run_hook_with_extra_environ,
Expand Down
23 changes: 13 additions & 10 deletions src/fromager/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,15 @@ def _get_version_from_package_metadata(
req: Requirement,
source_dir: str,
) -> Version:
pbi = ctx.package_build_info(req)
build_dir = pbi.build_dir(source_dir)
logger.info(f"{req.name}: generating metadata to get version")
pyproject_toml = dependencies.get_pyproject_contents(source_dir)

hook_caller = dependencies.get_build_backend_hook_caller(
source_dir,
pyproject_toml,
{},
network_isolation=ctx.network_isolation,
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ={},
)
metadata_dir_base = hook_caller.prepare_metadata_for_build_wheel(
metadata_directory=source_dir.parent,
Expand Down Expand Up @@ -672,12 +674,13 @@ def pep517_build_sdist(
version: Version,
) -> pathlib.Path:
"""Use the PEP 517 API to build a source distribution from a modified source tree."""
pyproject_toml = dependencies.get_pyproject_contents(sdist_root_dir)
pbi = ctx.package_build_info(req)
build_dir = pbi.build_dir(sdist_root_dir)
hook_caller = dependencies.get_build_backend_hook_caller(
sdist_root_dir,
pyproject_toml,
extra_environ,
network_isolation=ctx.network_isolation,
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ=extra_environ,
)
sdist_filename = hook_caller.build_sdist(ctx.sdists_builds)
return ctx.sdists_builds / sdist_filename
Loading