From 7ced8d2a8f36e1b4fc41b5544636defb7bd44bdf Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:46:44 +0100 Subject: [PATCH 1/3] remove direct imports of `pkg_resources` --- CHANGELOG.md | 1 + pyproject.toml | 7 +++-- setuptools_rust/build.py | 50 +++++++++++++------------------ setuptools_rust/extension.py | 1 + setuptools_rust/setuptools_ext.py | 19 ++++++------ 5 files changed, 35 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c664c044..a1663cb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Packaging - Drop support for Python 3.7. [#357](https://github.com/PyO3/setuptools-rust/pull/357) +- Remove direct imports from `pkg_resources`. [#359](https://github.com/PyO3/setuptools-rust/pull/359) ## 1.7.0 (2023-08-22) ### Packaging diff --git a/pyproject.toml b/pyproject.toml index ef4a8b95..c6d9d4ca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,9 +40,10 @@ rust_extensions = "setuptools_rust.setuptools_ext:rust_extensions" setuptools_rust = "setuptools_rust.setuptools_ext:pyprojecttoml_config" [project.urls] -repository = "https://github.com/PyO3/setuptools-rust" -documentation = "https://setuptools-rust.readthedocs.io" -changelog = "https://github.com/PyO3/setuptools-rust/blob/main/CHANGELOG.md" +Homepage = "https://github.com/PyO3/setuptools-rust" +Repository = "https://github.com/PyO3/setuptools-rust" +Documentation = "https://setuptools-rust.readthedocs.io" +Changelog = "https://github.com/PyO3/setuptools-rust/blob/main/CHANGELOG.md" [build-system] requires = ["setuptools>=62.4", "setuptools_scm"] diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index 911b6b79..859c3ad0 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -18,12 +18,11 @@ from pathlib import Path from typing import Dict, List, Literal, NamedTuple, Optional, Set, Tuple, cast -import pkg_resources -from semantic_version import Version from setuptools import Distribution from setuptools.command.build import build as CommandBuild from setuptools.command.build_ext import build_ext as CommandBuildExt from setuptools.command.build_ext import get_abi3_suffix +from setuptools.command.install_scripts import install_scripts as CommandInstallScripts from ._utils import format_called_process_error from .command import RustCommand @@ -93,8 +92,6 @@ def initialize_options(self) -> None: def finalize_options(self) -> None: super().finalize_options() - self.data_dir = self.get_data_dir() - if self.plat_name is None: self.plat_name = cast( CommandBuild, self.get_finalized_command("build") @@ -112,17 +109,6 @@ def finalize_options(self) -> None: if self.build_number is not None and not self.build_number[:1].isdigit(): raise ValueError("Build tag (build-number) must start with a digit.") - def get_data_dir(self) -> str: - components = ( - pkg_resources.safe_name(self.distribution.get_name()).replace("-", "_"), - pkg_resources.safe_version(self.distribution.get_version()).replace( - "-", "_" - ), - ) - if self.build_number: - components += (self.build_number,) - return "-".join(components) + ".data" - def run_for_extension(self, ext: RustExtension) -> None: assert self.plat_name is not None @@ -358,30 +344,33 @@ def install_extension( ) if ext._uses_exec_binding(): - ext_path = build_ext.get_ext_fullpath(module_name) - # remove extensions - ext_path, _, _ = _split_platform_and_extension(ext_path) - - # Add expected extension exe = sysconfig.get_config_var("EXE") - if exe is not None: - ext_path += exe - os.makedirs(os.path.dirname(ext_path), exist_ok=True) if isinstance(ext, RustBin): - executable_name = module_name + # will install the rust binary into the scripts directory + bin_name = module_name if exe is not None: - executable_name += exe - scripts_dir = os.path.join( - build_ext.build_lib, self.data_dir, "scripts" + bin_name += exe + + install_scripts = cast( + CommandInstallScripts, + self.get_finalized_command("install_scripts"), ) - os.makedirs(scripts_dir, exist_ok=True) - ext_path = os.path.join(scripts_dir, executable_name) + ext_path = os.path.join(install_scripts.build_dir, bin_name) else: + # will install the rust binary into the module directory + ext_path = build_ext.get_ext_fullpath(module_name) + + # add expected extension + ext_path, _, _ = _split_platform_and_extension(ext_path) + if exe is not None: + ext_path += exe + + # if required, also generate a console script entry point ext.install_script(module_name.split(".")[-1], ext_path) else: + # will install the rust library into the module directory ext_path = self.get_dylib_ext_path(ext, module_name) - os.makedirs(os.path.dirname(ext_path), exist_ok=True) # Make filenames relative to cwd where possible, to make logs and # errors below a little neater @@ -392,6 +381,7 @@ def install_extension( if ext_path.startswith(cwd): ext_path = os.path.relpath(ext_path, cwd) + os.makedirs(os.path.dirname(ext_path), exist_ok=True) logger.info("Copying rust artifact from %s to %s", dylib_path, ext_path) # We want to atomically replace any existing library file. We can't diff --git a/setuptools_rust/extension.py b/setuptools_rust/extension.py index b6f03e32..698188fb 100644 --- a/setuptools_rust/extension.py +++ b/setuptools_rust/extension.py @@ -232,6 +232,7 @@ def install_script(self, module_name: str, exe_path: str) -> None: if self.script and self.binding == Binding.Exec: dirname, executable = os.path.split(exe_path) script_name = _script_name(module_name) + os.makedirs(dirname, exist_ok=True) file = os.path.join(dirname, f"{script_name}.py") with open(file, "w") as f: f.write(_SCRIPT_TEMPLATE.format(executable=repr(executable))) diff --git a/setuptools_rust/setuptools_ext.py b/setuptools_rust/setuptools_ext.py index e0ddee22..d4a02049 100644 --- a/setuptools_rust/setuptools_ext.py +++ b/setuptools_rust/setuptools_ext.py @@ -210,17 +210,17 @@ def run(self) -> None: class install_lib_rust_extension(install_lib_base_class): # type: ignore[misc,valid-type] def get_exclusions(self) -> Set[str]: exclusions: Set[str] = super().get_exclusions() - build_rust = self.get_finalized_command("build_rust") - scripts_path = os.path.join( - self.install_dir, build_rust.data_dir, "scripts" + install_scripts_obj = cast( + install_scripts, self.get_finalized_command("install_scripts") ) + scripts_path = install_scripts_obj.build_dir if self.distribution.rust_extensions: exe = sysconfig.get_config_var("EXE") for ext in self.distribution.rust_extensions: - executable_name = ext.name - if exe is not None: - executable_name += exe if isinstance(ext, RustBin): + executable_name = ext.name + if exe is not None: + executable_name += exe exclusions.add(os.path.join(scripts_path, executable_name)) return exclusions @@ -234,11 +234,10 @@ def get_exclusions(self) -> Set[str]: class install_scripts_rust_extension(install_scripts_base_class): # type: ignore[misc,valid-type] def run(self) -> None: super().run() - 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" + install_scripts_obj = cast( + install_scripts, self.get_finalized_command("install_scripts") ) + scripts_path = install_scripts_obj.build_dir if os.path.isdir(scripts_path): for file in os.listdir(scripts_path): script_path = os.path.join(scripts_path, file) From 7428f8aa95fa2891209785a935b76f2e2955caf1 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:01:53 +0100 Subject: [PATCH 2/3] workaround crossenv need for pkg_resources --- noxfile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index fb8a5b55..cb440417 100644 --- a/noxfile.py +++ b/noxfile.py @@ -65,7 +65,8 @@ def test_crossenv(session: nox.Session): git config --global --add safe.directory /io cd examples/rust_with_cffi/ -python3.9 -m pip install crossenv +# Using crossenv master to workaround https://github.com/benfogle/crossenv/issues/108, will need 1.5.0 when released +python3.9 -m pip install https://github.com/benfogle/crossenv/archive/refs/heads/master.zip python3.9 -m crossenv "/opt/python/cp39-cp39/bin/python3" --cc $TARGET_CC --cxx $TARGET_CXX --sysroot $TARGET_SYSROOT --env LIBRARY_PATH= --manylinux manylinux1 /venv . /venv/bin/activate From de8b7c0aab942493be410e492fa53fdc3956b811 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:16:44 +0100 Subject: [PATCH 3/3] use python 3.11 to build for test-crossenv --- noxfile.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/noxfile.py b/noxfile.py index cb440417..64ac8a2e 100644 --- a/noxfile.py +++ b/noxfile.py @@ -66,12 +66,12 @@ def test_crossenv(session: nox.Session): cd examples/rust_with_cffi/ # Using crossenv master to workaround https://github.com/benfogle/crossenv/issues/108, will need 1.5.0 when released -python3.9 -m pip install https://github.com/benfogle/crossenv/archive/refs/heads/master.zip -python3.9 -m crossenv "/opt/python/cp39-cp39/bin/python3" --cc $TARGET_CC --cxx $TARGET_CXX --sysroot $TARGET_SYSROOT --env LIBRARY_PATH= --manylinux manylinux1 /venv +python3.11 -m pip install https://github.com/benfogle/crossenv/archive/refs/heads/master.zip +python3.11 -m crossenv "/opt/python/cp311-cp311/bin/python3" --cc $TARGET_CC --cxx $TARGET_CXX --sysroot $TARGET_SYSROOT --env LIBRARY_PATH= --manylinux manylinux1 /venv . /venv/bin/activate -build-pip install -U 'pip>=23.2.1' 'setuptools>=68.0.0' 'wheel>=0.41.1' -cross-pip install -U 'pip>=23.2.1' 'setuptools>=68.0.0' 'wheel>=0.41.1' +build-pip install -U 'pip>=23.2.1' 'setuptools>=68.0.0' 'wheel>=0.41.1' 'build>=1' +cross-pip install -U 'pip>=23.2.1' 'setuptools>=68.0.0' 'wheel>=0.41.1' 'build>=1' build-pip install cffi cross-expose cffi cross-pip install -e ../../ @@ -81,7 +81,7 @@ def test_crossenv(session: nox.Session): echo -e "[bdist_wheel]\npy_limited_api=cp37" > $DIST_EXTRA_CONFIG rm -rf dist/* -cross-pip wheel --no-build-isolation --no-deps --wheel-dir dist . -vv +cross-python -m build --no-isolation ls -la dist/ python -m zipfile -l dist/*.whl # debug all files inside wheel file """ @@ -120,7 +120,7 @@ def test_crossenv(session: nox.Session): "/io", "--platform", docker_platform, - "python:3.9", + "python:3.11", "bash", "-c", script_check,