forked from temporalio/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
112 lines (89 loc) · 2.87 KB
/
setup.py
File metadata and controls
112 lines (89 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""Setuptools entry points for environments that invoke ``setup.py`` directly.
The canonical build definition remains ``pyproject.toml`` / ``maturin``. This
module delegates ``sdist`` and ``bdist_wheel`` to ``python -m maturin`` so
legacy ``python setup.py sdist`` / ``bdist_wheel`` workflows produce the same
artifacts as a PEP 517 build.
Bootstrap tradeoff: if ``maturin`` is not importable as a module, this script
installs ``maturin`` into the *current* interpreter with ``pip`` (requires
network on cold builders). Builders that pre-install ``maturin`` avoid that
step. Alternatives such as ``setup_requires`` are avoided because they interact
poorly with modern pip and isolated metadata resolution.
``maturin sdist`` / ``maturin build`` still invoke Cargo metadata and require a
Rust toolchain (``cargo`` on ``PATH``), same as a normal PEP 517 build.
"""
from __future__ import annotations
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import setup
from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel
from setuptools.command.sdist import sdist as _sdist
ROOT = Path(__file__).resolve().parent
DIST_DIR = "dist"
MATURIN_REQ = "maturin>=1.0,<2.0"
def _output_dir(cmd: object) -> str:
"""Directory for artifacts; honors setuptools ``--dist-dir`` / ``-d`` when set."""
dist_dir = getattr(cmd, "dist_dir", None)
if dist_dir:
return str(dist_dir)
return DIST_DIR
def _have_maturin() -> bool:
exe = shutil.which("maturin")
if exe:
return True
try:
subprocess.run(
[sys.executable, "-m", "maturin", "--version"],
check=True,
capture_output=True,
cwd=ROOT,
)
return True
except (subprocess.CalledProcessError, OSError):
return False
def _ensure_maturin() -> None:
if _have_maturin():
return
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
MATURIN_REQ,
],
cwd=ROOT,
)
def _maturin_cmd() -> list[str]:
_ensure_maturin()
if shutil.which("maturin"):
return ["maturin"]
return [sys.executable, "-m", "maturin"]
def _run_maturin(args: list[str]) -> None:
cmd = _maturin_cmd() + args
sys.stderr.write("Running: " + " ".join(cmd) + "\n")
subprocess.check_call(cmd, cwd=ROOT)
class sdist(_sdist):
def run(self) -> None:
out = _output_dir(self)
self.mkpath(out)
_run_maturin(["sdist", "-o", out])
class bdist_wheel(_bdist_wheel):
def run(self) -> None:
out = _output_dir(self)
self.mkpath(out)
_run_maturin(
[
"build",
"--release",
"-o",
out,
]
)
setup(
cmdclass={
"sdist": sdist,
"bdist_wheel": bdist_wheel,
},
)