|
1 | 1 | import os
|
2 | 2 | import subprocess
|
| 3 | +import sysconfig |
3 | 4 | from distutils import log
|
4 | 5 | from distutils.command.clean import clean
|
5 | 6 | from typing import List, Set, Tuple, Type, cast
|
6 | 7 |
|
7 | 8 | from setuptools.command.build_ext import build_ext
|
8 | 9 | from setuptools.command.install import install
|
| 10 | +from setuptools.command.install_lib import install_lib |
| 11 | +from setuptools.command.install_scripts import install_scripts |
9 | 12 | from setuptools.command.sdist import sdist
|
10 | 13 | from setuptools.dist import Distribution
|
11 | 14 | from typing_extensions import Literal
|
12 | 15 |
|
13 |
| -from .extension import RustExtension |
| 16 | +from .extension import RustBin, RustExtension |
14 | 17 |
|
15 | 18 | try:
|
16 | 19 | from wheel.bdist_wheel import bdist_wheel
|
@@ -189,8 +192,64 @@ def finalize_options(self) -> None:
|
189 | 192 | # restore ext_modules
|
190 | 193 | self.distribution.ext_modules = ext_modules
|
191 | 194 |
|
| 195 | + def run(self) -> None: |
| 196 | + install_base_class.run(self) |
| 197 | + install_rustbin = False |
| 198 | + if self.distribution.rust_extensions: |
| 199 | + for ext in self.distribution.rust_extensions: |
| 200 | + if isinstance(ext, RustBin): |
| 201 | + install_rustbin = True |
| 202 | + if install_rustbin: |
| 203 | + self.run_command("install_scripts") |
| 204 | + |
192 | 205 | dist.cmdclass["install"] = install_rust_extension
|
193 | 206 |
|
| 207 | + install_lib_base_class = cast( |
| 208 | + Type[install_lib], dist.cmdclass.get("install_lib", install_lib) |
| 209 | + ) |
| 210 | + |
| 211 | + # prevent RustBin from being installed to data_dir |
| 212 | + class install_lib_rust_extension(install_lib_base_class): # type: ignore[misc,valid-type] |
| 213 | + def get_exclusions(self) -> Set[str]: |
| 214 | + exclusions: Set[str] = install_lib_base_class.get_exclusions(self) |
| 215 | + build_rust = self.get_finalized_command("build_rust") |
| 216 | + scripts_path = os.path.join( |
| 217 | + self.install_dir, build_rust.data_dir, "scripts" |
| 218 | + ) |
| 219 | + if self.distribution.rust_extensions: |
| 220 | + exe = sysconfig.get_config_var("EXE") |
| 221 | + for ext in self.distribution.rust_extensions: |
| 222 | + executable_name = ext.name |
| 223 | + if exe is not None: |
| 224 | + executable_name += exe |
| 225 | + if isinstance(ext, RustBin): |
| 226 | + exclusions.add(os.path.join(scripts_path, executable_name)) |
| 227 | + return exclusions |
| 228 | + |
| 229 | + dist.cmdclass["install_lib"] = install_lib_rust_extension |
| 230 | + |
| 231 | + install_scripts_base_class = cast( |
| 232 | + Type[install_scripts], dist.cmdclass.get("install_scripts", install_scripts) |
| 233 | + ) |
| 234 | + |
| 235 | + # this is required to make install_scripts compatible with RustBin |
| 236 | + class install_scripts_rust_extension(install_scripts_base_class): # type: ignore[misc,valid-type] |
| 237 | + def run(self) -> None: |
| 238 | + install_scripts_base_class.run(self) |
| 239 | + build_ext = self.get_finalized_command("build_ext") |
| 240 | + build_rust = self.get_finalized_command("build_rust") |
| 241 | + scripts_path = os.path.join( |
| 242 | + build_ext.build_lib, build_rust.data_dir, "scripts" |
| 243 | + ) |
| 244 | + if os.path.isdir(scripts_path): |
| 245 | + for file in os.listdir(scripts_path): |
| 246 | + script_path = os.path.join(scripts_path, file) |
| 247 | + if os.path.isfile(script_path): |
| 248 | + with open(os.path.join(script_path), "rb") as script_reader: |
| 249 | + self.write_script(file, script_reader.read(), mode="b") |
| 250 | + |
| 251 | + dist.cmdclass["install_scripts"] = install_scripts_rust_extension |
| 252 | + |
194 | 253 | if bdist_wheel is not None:
|
195 | 254 | bdist_wheel_base_class = cast( # type: ignore[no-any-unimported]
|
196 | 255 | Type[bdist_wheel], dist.cmdclass.get("bdist_wheel", bdist_wheel)
|
|
0 commit comments