Skip to content

Commit

Permalink
remove direct imports of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Sep 1, 2023
1 parent 322d5f8 commit de095d4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
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
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 @@ -211,17 +211,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] = 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"
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 @@ -235,11 +235,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:
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"
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

0 comments on commit de095d4

Please sign in to comment.