2023-01-12 01:04:47 +00:00
|
|
|
# This file is dual licensed under the terms of the Apache License, Version
|
|
|
|
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
|
|
|
|
# for complete details.
|
|
|
|
|
|
|
|
import re
|
2023-01-14 02:55:00 +00:00
|
|
|
from typing import FrozenSet, NewType, Tuple, Union, cast
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
from .tags import Tag, parse_tag
|
2023-01-12 01:04:47 +00:00
|
|
|
from .version import InvalidVersion, Version
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
BuildTag = Union[Tuple[()], Tuple[int, str]]
|
|
|
|
NormalizedName = NewType("NormalizedName", str)
|
|
|
|
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
class InvalidName(ValueError):
|
|
|
|
"""
|
|
|
|
An invalid distribution name; users should refer to the packaging user guide.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
class InvalidWheelFilename(ValueError):
|
|
|
|
"""
|
|
|
|
An invalid wheel filename was found, users should refer to PEP 427.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidSdistFilename(ValueError):
|
|
|
|
"""
|
|
|
|
An invalid sdist filename was found, users should refer to the packaging user guide.
|
|
|
|
"""
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
# Core metadata spec for `Name`
|
|
|
|
_validate_regex = re.compile(
|
|
|
|
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE
|
|
|
|
)
|
2023-01-12 01:04:47 +00:00
|
|
|
_canonicalize_regex = re.compile(r"[-_.]+")
|
2024-06-07 16:18:00 +00:00
|
|
|
_normalized_regex = re.compile(r"^([a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9])$")
|
2023-01-14 02:55:00 +00:00
|
|
|
# PEP 427: The build number must start with a digit.
|
|
|
|
_build_tag_regex = re.compile(r"(\d+)(.*)")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
|
|
|
|
if validate and not _validate_regex.match(name):
|
|
|
|
raise InvalidName(f"name is invalid: {name!r}")
|
2023-01-12 01:04:47 +00:00
|
|
|
# This is taken from PEP 503.
|
|
|
|
value = _canonicalize_regex.sub("-", name).lower()
|
2023-01-14 02:55:00 +00:00
|
|
|
return cast(NormalizedName, value)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
2024-06-07 16:18:00 +00:00
|
|
|
def is_normalized_name(name: str) -> bool:
|
|
|
|
return _normalized_regex.match(name) is not None
|
|
|
|
|
|
|
|
|
2023-02-17 01:17:58 +00:00
|
|
|
def canonicalize_version(
|
|
|
|
version: Union[Version, str], *, strip_trailing_zero: bool = True
|
|
|
|
) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
This is very similar to Version.__str__, but has one subtle difference
|
|
|
|
with the way it handles the release segment.
|
|
|
|
"""
|
2023-01-14 02:55:00 +00:00
|
|
|
if isinstance(version, str):
|
|
|
|
try:
|
|
|
|
parsed = Version(version)
|
|
|
|
except InvalidVersion:
|
|
|
|
# Legacy versions cannot be normalized
|
|
|
|
return version
|
|
|
|
else:
|
|
|
|
parsed = version
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
parts = []
|
|
|
|
|
|
|
|
# Epoch
|
2023-01-14 02:55:00 +00:00
|
|
|
if parsed.epoch != 0:
|
|
|
|
parts.append(f"{parsed.epoch}!")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Release segment
|
2023-02-17 01:17:58 +00:00
|
|
|
release_segment = ".".join(str(x) for x in parsed.release)
|
|
|
|
if strip_trailing_zero:
|
|
|
|
# NB: This strips trailing '.0's to normalize
|
|
|
|
release_segment = re.sub(r"(\.0)+$", "", release_segment)
|
|
|
|
parts.append(release_segment)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Pre-release
|
2023-01-14 02:55:00 +00:00
|
|
|
if parsed.pre is not None:
|
|
|
|
parts.append("".join(str(x) for x in parsed.pre))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Post-release
|
2023-01-14 02:55:00 +00:00
|
|
|
if parsed.post is not None:
|
|
|
|
parts.append(f".post{parsed.post}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Development release
|
2023-01-14 02:55:00 +00:00
|
|
|
if parsed.dev is not None:
|
|
|
|
parts.append(f".dev{parsed.dev}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Local version segment
|
2023-01-14 02:55:00 +00:00
|
|
|
if parsed.local is not None:
|
|
|
|
parts.append(f"+{parsed.local}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return "".join(parts)
|
2023-01-14 02:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_wheel_filename(
|
|
|
|
filename: str,
|
|
|
|
) -> Tuple[NormalizedName, Version, BuildTag, FrozenSet[Tag]]:
|
|
|
|
if not filename.endswith(".whl"):
|
|
|
|
raise InvalidWheelFilename(
|
|
|
|
f"Invalid wheel filename (extension must be '.whl'): {filename}"
|
|
|
|
)
|
|
|
|
|
|
|
|
filename = filename[:-4]
|
|
|
|
dashes = filename.count("-")
|
|
|
|
if dashes not in (4, 5):
|
|
|
|
raise InvalidWheelFilename(
|
|
|
|
f"Invalid wheel filename (wrong number of parts): {filename}"
|
|
|
|
)
|
|
|
|
|
|
|
|
parts = filename.split("-", dashes - 2)
|
|
|
|
name_part = parts[0]
|
2024-06-07 16:18:00 +00:00
|
|
|
# See PEP 427 for the rules on escaping the project name.
|
2023-01-14 02:55:00 +00:00
|
|
|
if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
|
|
|
|
raise InvalidWheelFilename(f"Invalid project name: {filename}")
|
|
|
|
name = canonicalize_name(name_part)
|
2024-06-07 16:18:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
version = Version(parts[1])
|
|
|
|
except InvalidVersion as e:
|
|
|
|
raise InvalidWheelFilename(
|
|
|
|
f"Invalid wheel filename (invalid version): {filename}"
|
|
|
|
) from e
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
if dashes == 5:
|
|
|
|
build_part = parts[2]
|
|
|
|
build_match = _build_tag_regex.match(build_part)
|
|
|
|
if build_match is None:
|
|
|
|
raise InvalidWheelFilename(
|
|
|
|
f"Invalid build number: {build_part} in '{filename}'"
|
|
|
|
)
|
|
|
|
build = cast(BuildTag, (int(build_match.group(1)), build_match.group(2)))
|
|
|
|
else:
|
|
|
|
build = ()
|
|
|
|
tags = parse_tag(parts[-1])
|
|
|
|
return (name, version, build, tags)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
|
|
|
|
if filename.endswith(".tar.gz"):
|
|
|
|
file_stem = filename[: -len(".tar.gz")]
|
|
|
|
elif filename.endswith(".zip"):
|
|
|
|
file_stem = filename[: -len(".zip")]
|
|
|
|
else:
|
|
|
|
raise InvalidSdistFilename(
|
|
|
|
f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
|
|
|
|
f" {filename}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# We are requiring a PEP 440 version, which cannot contain dashes,
|
|
|
|
# so we split on the last dash.
|
|
|
|
name_part, sep, version_part = file_stem.rpartition("-")
|
|
|
|
if not sep:
|
|
|
|
raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")
|
|
|
|
|
|
|
|
name = canonicalize_name(name_part)
|
2024-06-07 16:18:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
version = Version(version_part)
|
|
|
|
except InvalidVersion as e:
|
|
|
|
raise InvalidSdistFilename(
|
|
|
|
f"Invalid sdist filename (invalid version): {filename}"
|
|
|
|
) from e
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
return (name, version)
|