diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:20:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:20:41 +0000 |
commit | b49f1524e250764592ff132af8fb0d39182620f7 (patch) | |
tree | a2c4da0c1bfc3be79c9b80180d8958804e91a07d /src/build/util.py | |
parent | Initial commit. (diff) | |
download | python-build-b49f1524e250764592ff132af8fb0d39182620f7.tar.xz python-build-b49f1524e250764592ff132af8fb0d39182620f7.zip |
Adding upstream version 0.9.0.upstream/0.9.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/build/util.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/build/util.py b/src/build/util.py new file mode 100644 index 0000000..6c918ce --- /dev/null +++ b/src/build/util.py @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: MIT + +import os +import pathlib +import sys +import tempfile + +import pep517 + +import build +import build.env + + +if sys.version_info >= (3, 8): + import importlib.metadata as importlib_metadata +else: + import importlib_metadata + + +def _project_wheel_metadata(builder: build.ProjectBuilder) -> 'importlib_metadata.PackageMetadata': + with tempfile.TemporaryDirectory() as tmpdir: + path = pathlib.Path(builder.metadata_path(tmpdir)) + # https://github.com/python/importlib_metadata/pull/343 + return importlib_metadata.PathDistribution(path).metadata # type: ignore[arg-type] + + +def project_wheel_metadata( + srcdir: build.PathType, + isolated: bool = True, +) -> 'importlib_metadata.PackageMetadata': + """ + Return the wheel metadata for a project. + + Uses the ``prepare_metadata_for_build_wheel`` hook if available, + otherwise ``build_wheel``. + + :param srcdir: Project source directory + :param isolated: Whether or not to run invoke the backend in the current + environment or to create an isolated one and invoke it + there. + """ + builder = build.ProjectBuilder( + os.fspath(srcdir), + runner=pep517.quiet_subprocess_runner, + ) + + if not isolated: + return _project_wheel_metadata(builder) + + with build.env.IsolatedEnvBuilder() as env: + builder.python_executable = env.executable + builder.scripts_dir = env.scripts_dir + env.install(builder.build_system_requires) + env.install(builder.get_requires_for_build('wheel')) + return _project_wheel_metadata(builder) + + +__all__ = [ + 'project_wheel_metadata', +] |