Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove direct imports of pkg_resources #359

Merged
merged 3 commits into from Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions noxfile.py
Expand Up @@ -65,12 +65,13 @@ def test_crossenv(session: nox.Session):
git config --global --add safe.directory /io

cd examples/rust_with_cffi/
python3.9 -m pip install crossenv
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
# Using crossenv master to workaround https://github.com/benfogle/crossenv/issues/108, will need 1.5.0 when released
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 ../../
Expand All @@ -80,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
"""
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_crossenv(session: nox.Session):
"/io",
"--platform",
docker_platform,
"python:3.9",
"python:3.11",
"bash",
"-c",
script_check,
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Expand Up @@ -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"]
Expand Down
50 changes: 20 additions & 30 deletions setuptools_rust/build.py
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions setuptools_rust/extension.py
Expand Up @@ -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)))
Expand Down
19 changes: 9 additions & 10 deletions setuptools_rust/setuptools_ext.py
Expand Up @@ -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

Expand All @@ -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)
Expand Down