123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- from __future__ import absolute_import, division, print_function
- import collections
- import itertools
- import re
- from ._structures import Infinity, NegativeInfinity
- from ._typing import TYPE_CHECKING
- if TYPE_CHECKING:
- from typing import Callable, Iterator, List, Optional, SupportsInt, Tuple, Union
- from ._structures import InfinityType, NegativeInfinityType
- 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
- ]
- __all__ = ["parse", "Version", "LegacyVersion", "InvalidVersion", "VERSION_PATTERN"]
- _Version = collections.namedtuple(
- "_Version", ["epoch", "release", "dev", "pre", "post", "local"]
- )
- def parse(version):
-
- """
- 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.
- """
- class _BaseVersion(object):
- _key = None
- def __hash__(self):
-
- return hash(self._key)
- def __lt__(self, other):
-
- return self._compare(other, lambda s, o: s < o)
- def __le__(self, other):
-
- return self._compare(other, lambda s, o: s <= o)
- def __eq__(self, other):
-
- return self._compare(other, lambda s, o: s == o)
- def __ge__(self, other):
-
- return self._compare(other, lambda s, o: s >= o)
- def __gt__(self, other):
-
- return self._compare(other, lambda s, o: s > o)
- def __ne__(self, other):
-
- return self._compare(other, lambda s, o: s != o)
- def _compare(self, other, method):
-
- if not isinstance(other, _BaseVersion):
- return NotImplemented
- return method(self._key, other._key)
- class LegacyVersion(_BaseVersion):
- def __init__(self, version):
-
- self._version = str(version)
- self._key = _legacy_cmpkey(self._version)
- def __str__(self):
-
- return self._version
- def __repr__(self):
-
- return "<LegacyVersion({0})>".format(repr(str(self)))
- @property
- def public(self):
-
- return self._version
- @property
- def base_version(self):
-
- return self._version
- @property
- def epoch(self):
-
- return -1
- @property
- def release(self):
-
- return None
- @property
- def pre(self):
-
- return None
- @property
- def post(self):
-
- return None
- @property
- def dev(self):
-
- return None
- @property
- def local(self):
-
- return None
- @property
- def is_prerelease(self):
-
- return False
- @property
- def is_postrelease(self):
-
- return False
- @property
- def is_devrelease(self):
-
- 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": "@",
- }
- def _parse_version_parts(s):
-
- 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":
-
- yield part.zfill(8)
- else:
- yield "*" + part
-
- yield "*final"
- def _legacy_cmpkey(version):
-
-
-
-
-
- epoch = -1
-
-
- parts = []
- for part in _parse_version_parts(version.lower()):
- if part.startswith("*"):
-
- if part < "*final":
- while parts and parts[-1] == "*final-":
- parts.pop()
-
- while parts and parts[-1] == "00000000":
- parts.pop()
- parts.append(part)
- return epoch, tuple(parts)
- 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)
- def __init__(self, version):
-
-
- match = self._regex.search(version)
- if not match:
- raise InvalidVersion("Invalid version: '{0}'".format(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")),
- )
-
- self._key = _cmpkey(
- self._version.epoch,
- self._version.release,
- self._version.pre,
- self._version.post,
- self._version.dev,
- self._version.local,
- )
- def __repr__(self):
-
- return "<Version({0})>".format(repr(str(self)))
- def __str__(self):
-
- parts = []
-
- if self.epoch != 0:
- parts.append("{0}!".format(self.epoch))
-
- parts.append(".".join(str(x) for x in self.release))
-
- if self.pre is not None:
- parts.append("".join(str(x) for x in self.pre))
-
- if self.post is not None:
- parts.append(".post{0}".format(self.post))
-
- if self.dev is not None:
- parts.append(".dev{0}".format(self.dev))
-
- if self.local is not None:
- parts.append("+{0}".format(self.local))
- return "".join(parts)
- @property
- def epoch(self):
-
- _epoch = self._version.epoch
- return _epoch
- @property
- def release(self):
-
- _release = self._version.release
- return _release
- @property
- def pre(self):
-
- _pre = self._version.pre
- return _pre
- @property
- def post(self):
-
- return self._version.post[1] if self._version.post else None
- @property
- def dev(self):
-
- return self._version.dev[1] if self._version.dev else None
- @property
- def local(self):
-
- if self._version.local:
- return ".".join(str(x) for x in self._version.local)
- else:
- return None
- @property
- def public(self):
-
- return str(self).split("+", 1)[0]
- @property
- def base_version(self):
-
- parts = []
-
- if self.epoch != 0:
- parts.append("{0}!".format(self.epoch))
-
- parts.append(".".join(str(x) for x in self.release))
- return "".join(parts)
- @property
- def is_prerelease(self):
-
- return self.dev is not None or self.pre is not None
- @property
- def is_postrelease(self):
-
- return self.post is not None
- @property
- def is_devrelease(self):
-
- return self.dev is not None
- @property
- def major(self):
-
- return self.release[0] if len(self.release) >= 1 else 0
- @property
- def minor(self):
-
- return self.release[1] if len(self.release) >= 2 else 0
- @property
- def micro(self):
-
- return self.release[2] if len(self.release) >= 3 else 0
- def _parse_letter_version(
- letter, # type: str
- number, # type: Union[str, bytes, SupportsInt]
- ):
-
- if letter:
-
-
- if number is None:
- number = 0
-
- letter = letter.lower()
-
-
-
- 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:
-
-
- letter = "post"
- return letter, int(number)
- return None
- _local_version_separators = re.compile(r"[\._-]")
- def _parse_local_version(local):
-
- """
- 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(
- epoch, # type: int
- release, # type: Tuple[int, ...]
- pre, # type: Optional[Tuple[str, int]]
- post, # type: Optional[Tuple[str, int]]
- dev, # type: Optional[Tuple[str, int]]
- local, # type: Optional[Tuple[SubLocalType]]
- ):
-
-
-
-
-
-
- _release = tuple(
- reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release))))
- )
-
-
-
-
- if pre is None and post is None and dev is not None:
- _pre = NegativeInfinity
-
-
- elif pre is None:
- _pre = Infinity
- else:
- _pre = pre
-
- if post is None:
- _post = NegativeInfinity
- else:
- _post = post
-
- if dev is None:
- _dev = Infinity
- else:
- _dev = dev
- if local is None:
-
- _local = NegativeInfinity
- else:
-
-
-
-
-
-
-
- _local = tuple(
- (i, "") if isinstance(i, int) else (NegativeInfinity, i) for i in local
- )
- return epoch, _release, _pre, _post, _dev, _local
|