Skip to content

Commit f41c856

Browse files
committed
Further simplify calling hooks
1 parent 23962c2 commit f41c856

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

setuptools/_bootstrap.py

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
from __future__ import annotations
22

33
import argparse
4-
import importlib
54
import subprocess
65
import sys
76
import tempfile
87
from pathlib import Path
98

109
__all__: list[str] = [] # No public function, only CLI is provided.
1110

12-
_PRIVATE = "_private._dont_call_directly"
13-
1411

1512
def _build(output_dir: Path) -> None:
1613
"""Emulate as close as possible the way a build frontend would work."""
17-
cmd = [sys.executable, "-m", "setuptools._bootstrap"]
18-
store_dir = str(output_dir.absolute())
19-
20-
# Call build_sdist hook
21-
subprocess.run([*cmd, _PRIVATE, "build_sdist", store_dir])
14+
# Call build_wheel hook from CWD
15+
_hook("build_sdist", Path.cwd(), output_dir)
2216
sdist = _find_or_halt(output_dir, "setuptools*.tar.gz", "Error building sdist")
2317
print(f"**** sdist created in `{sdist}` ****")
2418

@@ -27,7 +21,7 @@ def _build(output_dir: Path) -> None:
2721
subprocess.run([sys.executable, "-m", "tarfile", "-e", str(sdist), tmp])
2822

2923
root = _find_or_halt(Path(tmp), "setuptools-*", "Error finding sdist root")
30-
subprocess.run([*cmd, _PRIVATE, "build_wheel", store_dir], cwd=str(root))
24+
_hook("build_wheel", root, output_dir)
3125

3226
wheel = _find_or_halt(output_dir, "setuptools*.whl", "Error building wheel")
3327
print(f"**** wheel created in `{wheel}` ****")
@@ -39,6 +33,13 @@ def _find_or_halt(parent: Path, pattern: str, error: str) -> Path:
3933
raise SystemExit(f"{error}. Cannot find `{parent / pattern}`")
4034

4135

36+
def _hook(name: str, source_dir: Path, output_dir: Path) -> None:
37+
# Call each hook in a fresh subprocess as required by PEP 517
38+
out = str(output_dir.absolute())
39+
script = f"from setuptools.build_meta import {name}; {name}({out!r})"
40+
subprocess.run([sys.executable, "-c", script], cwd=source_dir)
41+
42+
4243
def _cli() -> None:
4344
parser = argparse.ArgumentParser(
4445
description="**EXPERIMENTAL** bootstrapping script for setuptools. "
@@ -60,16 +61,5 @@ def _cli() -> None:
6061
_build(params.output_dir)
6162

6263

63-
def _private(guard: str = _PRIVATE) -> None:
64-
"""Private CLI that only calls a build hook in the simplest way possible."""
65-
parser = argparse.ArgumentParser()
66-
private = parser.add_subparsers().add_parser(guard)
67-
private.add_argument("hook", choices=["build_sdist", "build_wheel"])
68-
private.add_argument("output_dir", type=Path)
69-
params = parser.parse_args()
70-
hook = getattr(importlib.import_module("setuptools.build_meta"), params.hook)
71-
hook(params.output_dir)
72-
73-
7464
if __name__ == "__main__":
75-
_private() if _PRIVATE in sys.argv else _cli()
65+
_cli()

0 commit comments

Comments
 (0)