|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +from typing import Literal |
| 5 | + |
| 6 | +from .._compat import tomllib |
| 7 | +from .._logging import rich_warning |
| 8 | +from ..builder._load_provider import process_dynamic_metadata |
| 9 | +from . import ( |
| 10 | + get_requires_for_build_editable, |
| 11 | + get_requires_for_build_sdist, |
| 12 | + get_requires_for_build_wheel, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def main_metadata(_args: argparse.Namespace, /) -> None: |
| 17 | + """Get the full metadata, including dynamic metadata.""" |
| 18 | + with Path("pyproject.toml").open("rb") as f: |
| 19 | + pyproject = tomllib.load(f) |
| 20 | + |
| 21 | + project = pyproject.get("project", {}) |
| 22 | + metadata = pyproject.get("tool", {}).get("scikit-build", {}).get("metadata", {}) |
| 23 | + new_project = process_dynamic_metadata(project, metadata) |
| 24 | + print(json.dumps(new_project, indent=2)) |
| 25 | + |
| 26 | + |
| 27 | +def main_requires(args: argparse.Namespace, /) -> None: |
| 28 | + get_requires(args.mode) |
| 29 | + |
| 30 | + |
| 31 | +def get_requires(mode: Literal["sdist", "wheel", "editable"]) -> None: |
| 32 | + """Get the build requirements.""" |
| 33 | + |
| 34 | + with Path("pyproject.toml").open("rb") as f: |
| 35 | + pyproject = tomllib.load(f) |
| 36 | + |
| 37 | + requires = pyproject.get("build-system", {}).get("requires", []) |
| 38 | + backend = pyproject.get("build-system", {}).get("build-backend", "") |
| 39 | + if backend != "scikit_build_core.build": |
| 40 | + rich_warning("Might not be a scikit-build-core project.") |
| 41 | + if mode == "sdist": |
| 42 | + requires += get_requires_for_build_sdist({}) |
| 43 | + elif mode == "wheel": |
| 44 | + requires += get_requires_for_build_wheel({}) |
| 45 | + elif mode == "editable": |
| 46 | + requires += get_requires_for_build_editable({}) |
| 47 | + print(json.dumps(sorted(set(requires)), indent=2)) |
| 48 | + |
| 49 | + |
| 50 | +def main() -> None: |
| 51 | + parser = argparse.ArgumentParser( |
| 52 | + description="Build backend utilities", |
| 53 | + ) |
| 54 | + |
| 55 | + subparsers = parser.add_subparsers(help="Commands") |
| 56 | + requires = subparsers.add_parser( |
| 57 | + "requires", |
| 58 | + help="Get the build requirements", |
| 59 | + description="Includes the static build requirements, the dynamically generated ones, and dynamic-metadata ones.", |
| 60 | + ) |
| 61 | + requires.set_defaults(func=main_requires) |
| 62 | + requires.add_argument( |
| 63 | + "--mode", |
| 64 | + choices=["sdist", "wheel", "editable"], |
| 65 | + default="wheel", |
| 66 | + help="The build mode to get the requirements for", |
| 67 | + ) |
| 68 | + metadata = subparsers.add_parser( |
| 69 | + "metadata", |
| 70 | + help="Get the full metadata, including dynamic metadata", |
| 71 | + description="Processes static and dynamic metadata without triggering the backend, only handles scikit-build-core's dynamic metadata.", |
| 72 | + ) |
| 73 | + metadata.set_defaults(func=main_metadata) |
| 74 | + |
| 75 | + args = parser.parse_args() |
| 76 | + args.func(args) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments