Skip to content

Commit

Permalink
Fix RustBin setuptools install
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Jul 30, 2022
1 parent 5be25fe commit 7fea4a0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Changed
- Locate cdylib artifacts by handling messages from cargo instead of searching target dir (fixes build on MSYS2). [#267](https://github.com/PyO3/setuptools-rust/pull/267)
- Fix RustBin build without wheel. [#273](https://github.com/PyO3/setuptools-rust/pull/273)
- Fix RustBin setuptools install. [#275](https://github.com/PyO3/setuptools-rust/pull/275)


## 1.4.1 (2022-07-05)
Expand Down
9 changes: 9 additions & 0 deletions examples/hello-world/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ def test(session: nox.Session):
session.install(SETUPTOOLS_RUST)
session.install("--no-build-isolation", ".")
session.run("hello-world", *session.posargs)


@nox.session()
def setuptools_install(session: nox.Session):
session.install("setuptools")
with session.chdir(SETUPTOOLS_RUST):
session.run("python", "setup.py", "install")
session.run("python", "setup.py", "install")
session.run("hello-world", *session.posargs)
56 changes: 55 additions & 1 deletion setuptools_rust/setuptools_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.install_scripts import install_scripts
from setuptools.command.sdist import sdist
from setuptools.dist import Distribution
from typing_extensions import Literal

from .extension import RustExtension
from .extension import RustBin, RustExtension

try:
from wheel.bdist_wheel import bdist_wheel
Expand Down Expand Up @@ -189,8 +191,60 @@ def finalize_options(self) -> None:
# restore ext_modules
self.distribution.ext_modules = ext_modules

def run(self) -> None:
install_base_class.run(self)
install_rustbin = False
if self.distribution.rust_extensions:
for ext in self.distribution.rust_extensions:
if isinstance(ext, RustBin):
install_rustbin = True
if install_rustbin:
self.run_command("install_scripts")

dist.cmdclass["install"] = install_rust_extension

install_lib_base_class = cast(
Type[install_lib], dist.cmdclass.get("install_lib", install_lib)
)

# prevent RustBin from being installed to data_dir
class install_lib_rust_extension(install_lib_base_class): # type: ignore[misc,valid-type]
def get_exclusions(self) -> Set[str]:
exclusions: Set[str] = install_lib_base_class.get_exclusions(self)
build_rust = self.get_finalized_command("build_rust")
scripts_path = os.path.join(
self.install_dir, build_rust.data_dir, "scripts"
)
if self.distribution.rust_extensions:
for ext in self.distribution.rust_extensions:
if isinstance(ext, RustBin):
exclusions.add(os.path.join(scripts_path, ext.name))
return exclusions

dist.cmdclass["install_lib"] = install_lib_rust_extension

install_scripts_base_class = cast(
Type[install_scripts], dist.cmdclass.get("install_scripts", install_scripts)
)

# this is required to make install_scripts compatible with RustBin
class install_scripts_rust_extension(install_scripts_base_class): # type: ignore[misc,valid-type]
def run(self) -> None:
install_scripts_base_class.run(self)
build_ext = self.get_finalized_command("build_ext")
build_rust = self.get_finalized_command("build_rust")
scripts_path = os.path.join(
build_ext.build_lib, build_rust.data_dir, "scripts"
)
if os.path.isdir(scripts_path):
for file in os.listdir(scripts_path):
script_path = os.path.join(scripts_path, file)
if os.path.isfile(script_path):
with open(os.path.join(script_path), "rb") as script_reader:
self.write_script(file, script_reader.read(), mode="b")

dist.cmdclass["install_scripts"] = install_scripts_rust_extension

if bdist_wheel is not None:
bdist_wheel_base_class = cast( # type: ignore[no-any-unimported]
Type[bdist_wheel], dist.cmdclass.get("bdist_wheel", bdist_wheel)
Expand Down

0 comments on commit 7fea4a0

Please sign in to comment.