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 collections
|
|
|
|
import itertools
|
|
|
|
import re
|
2023-01-14 02:55:00 +00:00
|
|
|
import warnings
|
|
|
|
from typing import Callable, Iterator, List, Optional, SupportsInt, Tuple, Union
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
from ._structures import Infinity, InfinityType, NegativeInfinity, NegativeInfinityType
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
__all__ = ["parse", "Version", "LegacyVersion", "InvalidVersion", "VERSION_PATTERN"]
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
InfiniteTypes = Union[InfinityType, NegativeInfinityType]
|
|
|
|
PrePostDevType = Union[InfiniteTypes, Tuple[str, int]]
|
|
|
|
SubLocalType = Union[InfiniteTypes, int, str]
|
|
|
|
LocalType = Union[
|
|
|
|
NegativeInfinityType,
|
|
|
|
Tuple[
|
|
|
|
Union[
|
|
|
|
SubLocalType,
|
|
|
|
Tuple[SubLocalType, str],
|
|
|
|
Tuple[NegativeInfinityType, SubLocalType],
|
|
|
|
],
|
|
|
|
...,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
CmpKey = Tuple[
|
|
|
|
int, Tuple[int, ...], PrePostDevType, PrePostDevType, PrePostDevType, LocalType
|
|
|
|
]
|
|
|
|
LegacyCmpKey = Tuple[int, Tuple[str, ...]]
|
|
|
|
VersionComparisonMethod = Callable[
|
|
|
|
[Union[CmpKey, LegacyCmpKey], Union[CmpKey, LegacyCmpKey]], bool
|
|
|
|
]
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
_Version = collections.namedtuple(
|
|
|
|
"_Version", ["epoch", "release", "dev", "pre", "post", "local"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def parse(version: str) -> Union["LegacyVersion", "Version"]:
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
Parse the given version string and return either a :class:`Version` object
|
|
|
|
or a :class:`LegacyVersion` object depending on if the given version is
|
|
|
|
a valid PEP 440 version or a legacy version.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return Version(version)
|
|
|
|
except InvalidVersion:
|
|
|
|
return LegacyVersion(version)
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidVersion(ValueError):
|
|
|
|
"""
|
|
|
|
An invalid version was found, users should refer to PEP 440.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
class _BaseVersion:
|
|
|
|
_key: Union[CmpKey, LegacyCmpKey]
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __hash__(self) -> int:
|
2023-01-12 01:04:47 +00:00
|
|
|
return hash(self._key)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
# Please keep the duplicated `isinstance` check
|
|
|
|
# in the six comparisons hereunder
|
|
|
|
# unless you find a way to avoid adding overhead function calls.
|
|
|
|
def __lt__(self, other: "_BaseVersion") -> bool:
|
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
return self._key < other._key
|
|
|
|
|
|
|
|
def __le__(self, other: "_BaseVersion") -> bool:
|
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
return self._key <= other._key
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __eq__(self, other: object) -> bool:
|
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
return self._key == other._key
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __ge__(self, other: "_BaseVersion") -> bool:
|
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
return self._key >= other._key
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __gt__(self, other: "_BaseVersion") -> bool:
|
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
return self._key > other._key
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __ne__(self, other: object) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
if not isinstance(other, _BaseVersion):
|
|
|
|
return NotImplemented
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
return self._key != other._key
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LegacyVersion(_BaseVersion):
|
2023-01-14 02:55:00 +00:00
|
|
|
def __init__(self, version: str) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
self._version = str(version)
|
|
|
|
self._key = _legacy_cmpkey(self._version)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
warnings.warn(
|
|
|
|
"Creating a LegacyVersion has been deprecated and will be "
|
|
|
|
"removed in the next major release",
|
|
|
|
DeprecationWarning,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self._version
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<LegacyVersion('{self}')>"
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def public(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self._version
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def base_version(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self._version
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def epoch(self) -> int:
|
2023-01-12 01:04:47 +00:00
|
|
|
return -1
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def release(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def pre(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def post(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def dev(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def local(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_prerelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_postrelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_devrelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
_legacy_version_component_re = re.compile(r"(\d+ | [a-z]+ | \.| -)", re.VERBOSE)
|
|
|
|
|
|
|
|
_legacy_version_replacement_map = {
|
|
|
|
"pre": "c",
|
|
|
|
"preview": "c",
|
|
|
|
"-": "final-",
|
|
|
|
"rc": "c",
|
|
|
|
"dev": "@",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def _parse_version_parts(s: str) -> Iterator[str]:
|
2023-01-12 01:04:47 +00:00
|
|
|
for part in _legacy_version_component_re.split(s):
|
|
|
|
part = _legacy_version_replacement_map.get(part, part)
|
|
|
|
|
|
|
|
if not part or part == ".":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if part[:1] in "0123456789":
|
|
|
|
# pad for numeric comparison
|
|
|
|
yield part.zfill(8)
|
|
|
|
else:
|
|
|
|
yield "*" + part
|
|
|
|
|
|
|
|
# ensure that alpha/beta/candidate are before final
|
|
|
|
yield "*final"
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def _legacy_cmpkey(version: str) -> LegacyCmpKey:
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# We hardcode an epoch of -1 here. A PEP 440 version can only have a epoch
|
|
|
|
# greater than or equal to 0. This will effectively put the LegacyVersion,
|
|
|
|
# which uses the defacto standard originally implemented by setuptools,
|
|
|
|
# as before all PEP 440 versions.
|
|
|
|
epoch = -1
|
|
|
|
|
|
|
|
# This scheme is taken from pkg_resources.parse_version setuptools prior to
|
|
|
|
# it's adoption of the packaging library.
|
2023-01-14 02:55:00 +00:00
|
|
|
parts: List[str] = []
|
2023-01-12 01:04:47 +00:00
|
|
|
for part in _parse_version_parts(version.lower()):
|
|
|
|
if part.startswith("*"):
|
|
|
|
# remove "-" before a prerelease tag
|
|
|
|
if part < "*final":
|
|
|
|
while parts and parts[-1] == "*final-":
|
|
|
|
parts.pop()
|
|
|
|
|
|
|
|
# remove trailing zeros from each series of numeric parts
|
|
|
|
while parts and parts[-1] == "00000000":
|
|
|
|
parts.pop()
|
|
|
|
|
|
|
|
parts.append(part)
|
|
|
|
|
|
|
|
return epoch, tuple(parts)
|
|
|
|
|
|
|
|
|
|
|
|
# Deliberately not anchored to the start and end of the string, to make it
|
|
|
|
# easier for 3rd party code to reuse
|
|
|
|
VERSION_PATTERN = r"""
|
|
|
|
v?
|
|
|
|
(?:
|
|
|
|
(?:(?P<epoch>[0-9]+)!)? # epoch
|
|
|
|
(?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
|
|
|
|
(?P<pre> # pre-release
|
|
|
|
[-_\.]?
|
|
|
|
(?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
|
|
|
|
[-_\.]?
|
|
|
|
(?P<pre_n>[0-9]+)?
|
|
|
|
)?
|
|
|
|
(?P<post> # post release
|
|
|
|
(?:-(?P<post_n1>[0-9]+))
|
|
|
|
|
|
|
|
|
(?:
|
|
|
|
[-_\.]?
|
|
|
|
(?P<post_l>post|rev|r)
|
|
|
|
[-_\.]?
|
|
|
|
(?P<post_n2>[0-9]+)?
|
|
|
|
)
|
|
|
|
)?
|
|
|
|
(?P<dev> # dev release
|
|
|
|
[-_\.]?
|
|
|
|
(?P<dev_l>dev)
|
|
|
|
[-_\.]?
|
|
|
|
(?P<dev_n>[0-9]+)?
|
|
|
|
)?
|
|
|
|
)
|
|
|
|
(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Version(_BaseVersion):
|
|
|
|
|
|
|
|
_regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __init__(self, version: str) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Validate the version and parse it into pieces
|
|
|
|
match = self._regex.search(version)
|
|
|
|
if not match:
|
2023-01-14 02:55:00 +00:00
|
|
|
raise InvalidVersion(f"Invalid version: '{version}'")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Store the parsed out pieces of the version
|
|
|
|
self._version = _Version(
|
|
|
|
epoch=int(match.group("epoch")) if match.group("epoch") else 0,
|
|
|
|
release=tuple(int(i) for i in match.group("release").split(".")),
|
|
|
|
pre=_parse_letter_version(match.group("pre_l"), match.group("pre_n")),
|
|
|
|
post=_parse_letter_version(
|
|
|
|
match.group("post_l"), match.group("post_n1") or match.group("post_n2")
|
|
|
|
),
|
|
|
|
dev=_parse_letter_version(match.group("dev_l"), match.group("dev_n")),
|
|
|
|
local=_parse_local_version(match.group("local")),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Generate a key which will be used for sorting
|
|
|
|
self._key = _cmpkey(
|
|
|
|
self._version.epoch,
|
|
|
|
self._version.release,
|
|
|
|
self._version.pre,
|
|
|
|
self._version.post,
|
|
|
|
self._version.dev,
|
|
|
|
self._version.local,
|
|
|
|
)
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<Version('{self}')>"
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def __str__(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
parts = []
|
|
|
|
|
|
|
|
# Epoch
|
|
|
|
if self.epoch != 0:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f"{self.epoch}!")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Release segment
|
|
|
|
parts.append(".".join(str(x) for x in self.release))
|
|
|
|
|
|
|
|
# Pre-release
|
|
|
|
if self.pre is not None:
|
|
|
|
parts.append("".join(str(x) for x in self.pre))
|
|
|
|
|
|
|
|
# Post-release
|
|
|
|
if self.post is not None:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f".post{self.post}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Development release
|
|
|
|
if self.dev is not None:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f".dev{self.dev}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Local version segment
|
|
|
|
if self.local is not None:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f"+{self.local}")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def epoch(self) -> int:
|
|
|
|
_epoch: int = self._version.epoch
|
2023-01-12 01:04:47 +00:00
|
|
|
return _epoch
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def release(self) -> Tuple[int, ...]:
|
|
|
|
_release: Tuple[int, ...] = self._version.release
|
2023-01-12 01:04:47 +00:00
|
|
|
return _release
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def pre(self) -> Optional[Tuple[str, int]]:
|
|
|
|
_pre: Optional[Tuple[str, int]] = self._version.pre
|
2023-01-12 01:04:47 +00:00
|
|
|
return _pre
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def post(self) -> Optional[int]:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self._version.post[1] if self._version.post else None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def dev(self) -> Optional[int]:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self._version.dev[1] if self._version.dev else None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def local(self) -> Optional[str]:
|
2023-01-12 01:04:47 +00:00
|
|
|
if self._version.local:
|
|
|
|
return ".".join(str(x) for x in self._version.local)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def public(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
return str(self).split("+", 1)[0]
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def base_version(self) -> str:
|
2023-01-12 01:04:47 +00:00
|
|
|
parts = []
|
|
|
|
|
|
|
|
# Epoch
|
|
|
|
if self.epoch != 0:
|
2023-01-14 02:55:00 +00:00
|
|
|
parts.append(f"{self.epoch}!")
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Release segment
|
|
|
|
parts.append(".".join(str(x) for x in self.release))
|
|
|
|
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_prerelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.dev is not None or self.pre is not None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_postrelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.post is not None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def is_devrelease(self) -> bool:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.dev is not None
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def major(self) -> int:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.release[0] if len(self.release) >= 1 else 0
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def minor(self) -> int:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.release[1] if len(self.release) >= 2 else 0
|
|
|
|
|
|
|
|
@property
|
2023-01-14 02:55:00 +00:00
|
|
|
def micro(self) -> int:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.release[2] if len(self.release) >= 3 else 0
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_letter_version(
|
2023-01-14 02:55:00 +00:00
|
|
|
letter: str, number: Union[str, bytes, SupportsInt]
|
|
|
|
) -> Optional[Tuple[str, int]]:
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
if letter:
|
|
|
|
# We consider there to be an implicit 0 in a pre-release if there is
|
|
|
|
# not a numeral associated with it.
|
|
|
|
if number is None:
|
|
|
|
number = 0
|
|
|
|
|
|
|
|
# We normalize any letters to their lower case form
|
|
|
|
letter = letter.lower()
|
|
|
|
|
|
|
|
# We consider some words to be alternate spellings of other words and
|
|
|
|
# in those cases we want to normalize the spellings to our preferred
|
|
|
|
# spelling.
|
|
|
|
if letter == "alpha":
|
|
|
|
letter = "a"
|
|
|
|
elif letter == "beta":
|
|
|
|
letter = "b"
|
|
|
|
elif letter in ["c", "pre", "preview"]:
|
|
|
|
letter = "rc"
|
|
|
|
elif letter in ["rev", "r"]:
|
|
|
|
letter = "post"
|
|
|
|
|
|
|
|
return letter, int(number)
|
|
|
|
if not letter and number:
|
|
|
|
# We assume if we are given a number, but we are not given a letter
|
|
|
|
# then this is using the implicit post release syntax (e.g. 1.0-1)
|
|
|
|
letter = "post"
|
|
|
|
|
|
|
|
return letter, int(number)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
_local_version_separators = re.compile(r"[\._-]")
|
|
|
|
|
|
|
|
|
2023-01-14 02:55:00 +00:00
|
|
|
def _parse_local_version(local: str) -> Optional[LocalType]:
|
2023-01-12 01:04:47 +00:00
|
|
|
"""
|
|
|
|
Takes a string like abc.1.twelve and turns it into ("abc", 1, "twelve").
|
|
|
|
"""
|
|
|
|
if local is not None:
|
|
|
|
return tuple(
|
|
|
|
part.lower() if not part.isdigit() else int(part)
|
|
|
|
for part in _local_version_separators.split(local)
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _cmpkey(
|
2023-01-14 02:55:00 +00:00
|
|
|
epoch: int,
|
|
|
|
release: Tuple[int, ...],
|
|
|
|
pre: Optional[Tuple[str, int]],
|
|
|
|
post: Optional[Tuple[str, int]],
|
|
|
|
dev: Optional[Tuple[str, int]],
|
|
|
|
local: Optional[Tuple[SubLocalType]],
|
|
|
|
) -> CmpKey:
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# When we compare a release version, we want to compare it with all of the
|
|
|
|
# trailing zeros removed. So we'll use a reverse the list, drop all the now
|
|
|
|
# leading zeros until we come to something non zero, then take the rest
|
|
|
|
# re-reverse it back into the correct order and make it a tuple and use
|
|
|
|
# that for our sorting key.
|
|
|
|
_release = tuple(
|
|
|
|
reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release))))
|
|
|
|
)
|
|
|
|
|
|
|
|
# We need to "trick" the sorting algorithm to put 1.0.dev0 before 1.0a0.
|
|
|
|
# We'll do this by abusing the pre segment, but we _only_ want to do this
|
|
|
|
# if there is not a pre or a post segment. If we have one of those then
|
|
|
|
# the normal sorting rules will handle this case correctly.
|
|
|
|
if pre is None and post is None and dev is not None:
|
2023-01-14 02:55:00 +00:00
|
|
|
_pre: PrePostDevType = NegativeInfinity
|
2023-01-12 01:04:47 +00:00
|
|
|
# Versions without a pre-release (except as noted above) should sort after
|
|
|
|
# those with one.
|
|
|
|
elif pre is None:
|
|
|
|
_pre = Infinity
|
|
|
|
else:
|
|
|
|
_pre = pre
|
|
|
|
|
|
|
|
# Versions without a post segment should sort before those with one.
|
|
|
|
if post is None:
|
2023-01-14 02:55:00 +00:00
|
|
|
_post: PrePostDevType = NegativeInfinity
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
_post = post
|
|
|
|
|
|
|
|
# Versions without a development segment should sort after those with one.
|
|
|
|
if dev is None:
|
2023-01-14 02:55:00 +00:00
|
|
|
_dev: PrePostDevType = Infinity
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
_dev = dev
|
|
|
|
|
|
|
|
if local is None:
|
|
|
|
# Versions without a local segment should sort before those with one.
|
2023-01-14 02:55:00 +00:00
|
|
|
_local: LocalType = NegativeInfinity
|
2023-01-12 01:04:47 +00:00
|
|
|
else:
|
|
|
|
# Versions with a local segment need that segment parsed to implement
|
|
|
|
# the sorting rules in PEP440.
|
|
|
|
# - Alpha numeric segments sort before numeric segments
|
|
|
|
# - Alpha numeric segments sort lexicographically
|
|
|
|
# - Numeric segments sort numerically
|
|
|
|
# - Shorter versions sort before longer versions when the prefixes
|
|
|
|
# match exactly
|
|
|
|
_local = tuple(
|
|
|
|
(i, "") if isinstance(i, int) else (NegativeInfinity, i) for i in local
|
|
|
|
)
|
|
|
|
|
|
|
|
return epoch, _release, _pre, _post, _dev, _local
|