Skip to content

Fix breakage caused by setuptools recent PEP 491 fix #44

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

Merged
merged 1 commit into from
Apr 11, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 80 additions & 34 deletions metapkg/packages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
)

import copy
import functools
import pathlib
import re
import shlex
import tempfile
import textwrap
Expand All @@ -21,6 +23,9 @@

import build as pypa_build
import build.env as pypa_build_env
import packaging.version
import packaging.utils

import pyproject_hooks

import distlib.database
Expand All @@ -33,8 +38,6 @@
from . import repository
from .utils import python_dependency_from_pep_508

import packaging.utils


if TYPE_CHECKING:
from cleo.io import io as cleo_io
Expand Down Expand Up @@ -300,8 +303,59 @@ def is_build_system_bootstrap_package(
}


#
# The following it the setuptools' dist_info sanitation protocol
# attempting to bring arbitrary version specs into PEP440 compliance.
#
# SPDX-SnippetBegin
# SPDX-License-Identifier: MIT
# SPDX-SnippetCopyrightText: Python Packaging Authority <distutils-sig@python.org>
# SDPX-SnippetName: dist_info version normalization
#
_UNSAFE_NAME_CHARS = re.compile(r"[^A-Z0-9._-]+", re.I)
_PEP440_FALLBACK = re.compile(
r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I
)
_NON_ALPHANUMERIC = re.compile(r"[^A-Z0-9]+", re.I)


def _safe_version(version: str) -> str:
v = version.replace(" ", ".")
try:
return str(packaging.version.Version(v))
except packaging.version.InvalidVersion:
attempt = _UNSAFE_NAME_CHARS.sub("-", v)
return str(packaging.version.Version(attempt))


def _best_effort_version(version: str) -> str:
try:
return _safe_version(version)
except packaging.version.InvalidVersion:
v = version.replace(" ", ".")
match = _PEP440_FALLBACK.search(v)
if match:
safe = match["safe"]
rest = v[len(safe) :]
else:
safe = "0"
rest = version
safe_rest = _NON_ALPHANUMERIC.sub(".", rest).strip(".")
local = f"sanitized.{safe_rest}".strip(".")
return _safe_version(f"{safe}.dev0+{local}")


# SPDX-SnippetEnd


def get_dist_info_dirname(name: base.NormalizedName, version: str) -> str:
version = _best_effort_version(version).replace("-", "_").strip("_")
return f"{name.replace('-', '_')}-{version}.dist-info"


class BasePythonPackage(base.BasePackage):
source: af_sources.BaseSource
dist_name: base.NormalizedName

def sh_get_build_wheel_env(
self,
Expand All @@ -312,6 +366,16 @@ def sh_get_build_wheel_env(
) -> base.Args:
return {}

@functools.cache
def get_dist_name(self) -> base.NormalizedName:
dist_name = getattr(self, "dist_name", None)
if dist_name is None:
dist_name = self.name
dist_name = base.canonicalize_name(
dist_name.removeprefix("pypkg-")
)
return dist_name

def get_build_script(self, build: targets.Build) -> str:
sdir = build.get_source_dir(self, relative_to="pkgbuild")
src_python = build.sh_get_command(
Expand All @@ -338,12 +402,7 @@ def get_build_script(self, build: targets.Build) -> str:
"import pathlib, sys; print(pathlib.Path(sys.argv[1]).resolve())"
)

pkgname = getattr(self, "dist_name", None)
if pkgname is None:
pkgname = self.name
if pkgname.startswith("pypkg-"):
pkgname = pkgname[len("pypkg-") :]

dist_name = self.get_dist_name()
env = build.sh_append_global_flags(
{
"SETUPTOOLS_SCM_PRETEND_VERSION": self.pretty_version,
Expand All @@ -353,11 +412,11 @@ def get_build_script(self, build: targets.Build) -> str:

build_deps = build.get_build_reqs(self)

if is_build_system_bootstrap_package(pkgname):
if is_build_system_bootstrap_package(dist_name):
tarballs = build.get_tarballs(self, relative_to="pkgsource")
assert len(tarballs) == 1, "expected exactly one tarball"
_, tarball = tarballs[0]
build_command = f'cp "{tarball}" ${{_wheeldir}}/{pkgname}-{self.version}.tar.gz'
build_command = f'cp "{tarball}" ${{_wheeldir}}/{dist_name}-{self.version}.tar.gz'
binary = False
else:
args = [
Expand Down Expand Up @@ -441,7 +500,7 @@ def get_build_script(self, build: targets.Build) -> str:
-f "file://${{_wheeldir}}" \\
{'--only-binary' if binary else '--no-binary'} :all: \\
--target "${{_target}}" \\
"{pkgname}"
"{dist_name}"
"""
)

Expand All @@ -458,13 +517,9 @@ def get_build_install_script(self, build: targets.Build) -> str:
root = build.get_build_install_dir(self, relative_to="pkgbuild")
wheeldir_script = 'import pathlib; print(pathlib.Path(".").resolve())'

pkgname = getattr(self, "dist_name", None)
if pkgname is None:
pkgname = self.name
if pkgname.startswith("pypkg-"):
pkgname = pkgname[len("pypkg-") :]
dist_name = self.get_dist_name()

binary = not is_build_system_bootstrap_package(pkgname)
binary = not is_build_system_bootstrap_package(dist_name)

env = {
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
Expand All @@ -486,7 +541,7 @@ def get_build_install_script(self, build: targets.Build) -> str:
--no-warn-script-location -f "file://${{_wheeldir}}" \\
{'--only-binary' if binary else '--no-binary'} :all: \\
--root "$(pwd -P)/{root}" \\
"{pkgname}"
"{dist_name}"
"""
)

Expand All @@ -501,12 +556,10 @@ def get_install_list_script(self, build: targets.Build) -> str:
prefix = build.get_install_prefix(self)
dest = build.get_build_install_dir(self, relative_to="pkgbuild")

pkgname = getattr(self, "dist_name", None)
if pkgname is None:
pkgname = self.pretty_name
if pkgname.startswith("pypkg-"):
pkgname = pkgname[len("pypkg-") :]
dist_name = pkgname.replace("-", "_")
dist_info_dir = get_dist_info_dirname(
self.get_dist_name(),
self.pretty_version,
)

pyscript = textwrap.dedent(
f"""\
Expand All @@ -519,14 +572,9 @@ def get_install_list_script(self, build: targets.Build) -> str:
sitepackages.relative_to('/')
)

record = (
abs_sitepackages /
f'{dist_name}-{self.pretty_version}.dist-info' /
'RECORD'
)

record = abs_sitepackages / "{dist_info_dir}" / "RECORD"
if not record.exists():
raise RuntimeError(f'no wheel RECORD for {pkgname}')
raise RuntimeError(f'no wheel RECORD for {self.name}')

entries = set()

Expand Down Expand Up @@ -579,8 +627,6 @@ def __repr__(self) -> str:


class BundledPythonPackage(BasePythonPackage, base.BundledPackage):
dist_name: str

@classmethod
def get_package_repository(
cls, target: targets.Target, io: cleo_io.IO
Expand Down Expand Up @@ -627,7 +673,7 @@ def resolve(
requires=requires,
source_version=repo.rev_parse("HEAD"),
)
package.dist_name = dist.name
package.dist_name = base.canonicalize_name(dist.name)
repository.set_build_requirements(
package,
get_build_requires_from_srcdir(package, repo_dir),
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'python-magic~=0.4.26; platform_system=="Linux" or (platform_machine!="x86_64" and platform_machine!="AMD64")',
'python-magic-bin~=0.4.14; platform_system!="Linux" and platform_machine!="arm64"',
"wheel>=0.32.3",
"setuptools>=75.3.1",
"setuptools-rust>=0.11.4",
"tomli>=1.2",
],
Expand Down