|
@ -9,8 +9,27 @@ import itertools |
|
|
import re |
|
|
import re |
|
|
|
|
|
|
|
|
from ._compat import string_types, with_metaclass |
|
|
from ._compat import string_types, with_metaclass |
|
|
|
|
|
from ._typing import TYPE_CHECKING |
|
|
|
|
|
from .utils import canonicalize_version |
|
|
from .version import Version, LegacyVersion, parse |
|
|
from .version import Version, LegacyVersion, parse |
|
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING: # pragma: no cover |
|
|
|
|
|
from typing import ( |
|
|
|
|
|
List, |
|
|
|
|
|
Dict, |
|
|
|
|
|
Union, |
|
|
|
|
|
Iterable, |
|
|
|
|
|
Iterator, |
|
|
|
|
|
Optional, |
|
|
|
|
|
Callable, |
|
|
|
|
|
Tuple, |
|
|
|
|
|
FrozenSet, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
ParsedVersion = Union[Version, LegacyVersion] |
|
|
|
|
|
UnparsedVersion = Union[Version, LegacyVersion, str] |
|
|
|
|
|
CallableOperator = Callable[[ParsedVersion, str], bool] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidSpecifier(ValueError): |
|
|
class InvalidSpecifier(ValueError): |
|
|
""" |
|
|
""" |
|
@ -18,10 +37,10 @@ class InvalidSpecifier(ValueError): |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): # type: ignore |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def __str__(self): |
|
|
def __str__(self): |
|
|
|
|
|
# type: () -> str |
|
|
""" |
|
|
""" |
|
|
Returns the str representation of this Specifier like object. This |
|
|
Returns the str representation of this Specifier like object. This |
|
|
should be representative of the Specifier itself. |
|
|
should be representative of the Specifier itself. |
|
@ -29,12 +48,14 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def __hash__(self): |
|
|
def __hash__(self): |
|
|
|
|
|
# type: () -> int |
|
|
""" |
|
|
""" |
|
|
Returns a hash value for this Specifier like object. |
|
|
Returns a hash value for this Specifier like object. |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def __eq__(self, other): |
|
|
def __eq__(self, other): |
|
|
|
|
|
# type: (object) -> bool |
|
|
""" |
|
|
""" |
|
|
Returns a boolean representing whether or not the two Specifier like |
|
|
Returns a boolean representing whether or not the two Specifier like |
|
|
objects are equal. |
|
|
objects are equal. |
|
@ -42,6 +63,7 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def __ne__(self, other): |
|
|
def __ne__(self, other): |
|
|
|
|
|
# type: (object) -> bool |
|
|
""" |
|
|
""" |
|
|
Returns a boolean representing whether or not the two Specifier like |
|
|
Returns a boolean representing whether or not the two Specifier like |
|
|
objects are not equal. |
|
|
objects are not equal. |
|
@ -49,6 +71,7 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
@abc.abstractproperty |
|
|
@abc.abstractproperty |
|
|
def prereleases(self): |
|
|
def prereleases(self): |
|
|
|
|
|
# type: () -> Optional[bool] |
|
|
""" |
|
|
""" |
|
|
Returns whether or not pre-releases as a whole are allowed by this |
|
|
Returns whether or not pre-releases as a whole are allowed by this |
|
|
specifier. |
|
|
specifier. |
|
@ -56,6 +79,7 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
@prereleases.setter |
|
|
@prereleases.setter |
|
|
def prereleases(self, value): |
|
|
def prereleases(self, value): |
|
|
|
|
|
# type: (bool) -> None |
|
|
""" |
|
|
""" |
|
|
Sets whether or not pre-releases as a whole are allowed by this |
|
|
Sets whether or not pre-releases as a whole are allowed by this |
|
|
specifier. |
|
|
specifier. |
|
@ -63,12 +87,14 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def contains(self, item, prereleases=None): |
|
|
def contains(self, item, prereleases=None): |
|
|
|
|
|
# type: (str, Optional[bool]) -> bool |
|
|
""" |
|
|
""" |
|
|
Determines if the given item is contained within this specifier. |
|
|
Determines if the given item is contained within this specifier. |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
@abc.abstractmethod |
|
|
@abc.abstractmethod |
|
|
def filter(self, iterable, prereleases=None): |
|
|
def filter(self, iterable, prereleases=None): |
|
|
|
|
|
# type: (Iterable[UnparsedVersion], Optional[bool]) -> Iterable[UnparsedVersion] |
|
|
""" |
|
|
""" |
|
|
Takes an iterable of items and filters them so that only items which |
|
|
Takes an iterable of items and filters them so that only items which |
|
|
are contained within this specifier are allowed in it. |
|
|
are contained within this specifier are allowed in it. |
|
@ -77,9 +103,10 @@ class BaseSpecifier(with_metaclass(abc.ABCMeta, object)): |
|
|
|
|
|
|
|
|
class _IndividualSpecifier(BaseSpecifier): |
|
|
class _IndividualSpecifier(BaseSpecifier): |
|
|
|
|
|
|
|
|
_operators = {} |
|
|
_operators = {} # type: Dict[str, str] |
|
|
|
|
|
|
|
|
def __init__(self, spec="", prereleases=None): |
|
|
def __init__(self, spec="", prereleases=None): |
|
|
|
|
|
# type: (str, Optional[bool]) -> None |
|
|
match = self._regex.search(spec) |
|
|
match = self._regex.search(spec) |
|
|
if not match: |
|
|
if not match: |
|
|
raise InvalidSpecifier("Invalid specifier: '{0}'".format(spec)) |
|
|
raise InvalidSpecifier("Invalid specifier: '{0}'".format(spec)) |
|
@ -87,45 +114,51 @@ class _IndividualSpecifier(BaseSpecifier): |
|
|
self._spec = ( |
|
|
self._spec = ( |
|
|
match.group("operator").strip(), |
|
|
match.group("operator").strip(), |
|
|
match.group("version").strip(), |
|
|
match.group("version").strip(), |
|
|
) |
|
|
) # type: Tuple[str, str] |
|
|
|
|
|
|
|
|
# Store whether or not this Specifier should accept prereleases |
|
|
# Store whether or not this Specifier should accept prereleases |
|
|
self._prereleases = prereleases |
|
|
self._prereleases = prereleases |
|
|
|
|
|
|
|
|
def __repr__(self): |
|
|
def __repr__(self): |
|
|
|
|
|
# type: () -> str |
|
|
pre = ( |
|
|
pre = ( |
|
|
", prereleases={0!r}".format(self.prereleases) |
|
|
", prereleases={0!r}".format(self.prereleases) |
|
|
if self._prereleases is not None |
|
|
if self._prereleases is not None |
|
|
else "" |
|
|
else "" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
return "<{0}({1!r}{2})>".format( |
|
|
return "<{0}({1!r}{2})>".format(self.__class__.__name__, str(self), pre) |
|
|
self.__class__.__name__, |
|
|
|
|
|
str(self), |
|
|
|
|
|
pre, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
def __str__(self): |
|
|
|
|
|
# type: () -> str |
|
|
return "{0}{1}".format(*self._spec) |
|
|
return "{0}{1}".format(*self._spec) |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
|
def _canonical_spec(self): |
|
|
|
|
|
# type: () -> Tuple[str, Union[Version, str]] |
|
|
|
|
|
return self._spec[0], canonicalize_version(self._spec[1]) |
|
|
|
|
|
|
|
|
def __hash__(self): |
|
|
def __hash__(self): |
|
|
return hash(self._spec) |
|
|
# type: () -> int |
|
|
|
|
|
return hash(self._canonical_spec) |
|
|
|
|
|
|
|
|
def __eq__(self, other): |
|
|
def __eq__(self, other): |
|
|
|
|
|
# type: (object) -> bool |
|
|
if isinstance(other, string_types): |
|
|
if isinstance(other, string_types): |
|
|
try: |
|
|
try: |
|
|
other = self.__class__(other) |
|
|
other = self.__class__(str(other)) |
|
|
except InvalidSpecifier: |
|
|
except InvalidSpecifier: |
|
|
return NotImplemented |
|
|
return NotImplemented |
|
|
elif not isinstance(other, self.__class__): |
|
|
elif not isinstance(other, self.__class__): |
|
|
return NotImplemented |
|
|
return NotImplemented |
|
|
|
|
|
|
|
|
return self._spec == other._spec |
|
|
return self._canonical_spec == other._canonical_spec |
|
|
|
|
|
|
|
|
def __ne__(self, other): |
|
|
def __ne__(self, other): |
|
|
|
|
|
# type: (object) -> bool |
|
|
if isinstance(other, string_types): |
|
|
if isinstance(other, string_types): |
|
|
try: |
|
|
try: |
|
|
other = self.__class__(other) |
|
|
other = self.__class__(str(other)) |
|
|
except InvalidSpecifier: |
|
|
except InvalidSpecifier: |
|
|
return NotImplemented |
|
|
return NotImplemented |
|
|
elif not isinstance(other, self.__class__): |
|
|
elif not isinstance(other, self.__class__): |
|
@ -134,52 +167,67 @@ class _IndividualSpecifier(BaseSpecifier): |
|
|
return self._spec != other._spec |
|
|
return self._spec != other._spec |
|
|
|
|
|
|
|
|
def _get_operator(self, op): |
|
|
def _get_operator(self, op): |
|
|
return getattr(self, "_compare_{0}".format(self._operators[op])) |
|
|
# type: (str) -> CallableOperator |
|
|
|
|
|
operator_callable = getattr( |
|
|
|
|
|
self, "_compare_{0}".format(self._operators[op]) |
|
|
|
|
|
) # type: CallableOperator |
|
|
|
|
|
return operator_callable |
|
|
|
|
|
|
|
|
def _coerce_version(self, version): |
|
|
def _coerce_version(self, version): |
|
|
|
|
|
# type: (UnparsedVersion) -> ParsedVersion |
|
|
if not isinstance(version, (LegacyVersion, Version)): |
|
|
if not isinstance(version, (LegacyVersion, Version)): |
|
|
version = parse(version) |
|
|
version = parse(version) |
|
|
return version |
|
|
return version |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def operator(self): |
|
|
def operator(self): |
|
|
|
|
|
# type: () -> str |
|
|
return self._spec[0] |
|
|
return self._spec[0] |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def version(self): |
|
|
def version(self): |
|
|
|
|
|
# type: () -> str |
|
|
return self._spec[1] |
|
|
return self._spec[1] |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def prereleases(self): |
|
|
def prereleases(self): |
|
|
|
|
|
# type: () -> Optional[bool] |
|
|
return self._prereleases |
|
|
return self._prereleases |
|
|
|
|
|
|
|
|
@prereleases.setter |
|
|
@prereleases.setter |
|
|
def prereleases(self, value): |
|
|
def prereleases(self, value): |
|
|
|
|
|
# type: (bool) -> None |
|
|
self._prereleases = value |
|
|
self._prereleases = value |
|
|
|
|
|
|
|
|
def __contains__(self, item): |
|
|
def __contains__(self, item): |
|
|
|
|
|
# type: (str) -> bool |
|
|
return self.contains(item) |
|
|
return self.contains(item) |
|
|
|
|
|
|
|
|
def contains(self, item, prereleases=None): |
|
|
def contains(self, item, prereleases=None): |
|
|
|
|
|
# type: (UnparsedVersion, Optional[bool]) -> bool |
|
|
|
|
|
|
|
|
# Determine if prereleases are to be allowed or not. |
|
|
# Determine if prereleases are to be allowed or not. |
|
|
if prereleases is None: |
|
|
if prereleases is None: |
|
|
prereleases = self.prereleases |
|
|
prereleases = self.prereleases |
|
|
|
|
|
|
|
|
# Normalize item to a Version or LegacyVersion, this allows us to have |
|
|
# Normalize item to a Version or LegacyVersion, this allows us to have |
|
|
# a shortcut for ``"2.0" in Specifier(">=2") |
|
|
# a shortcut for ``"2.0" in Specifier(">=2") |
|
|
item = self._coerce_version(item) |
|
|
normalized_item = self._coerce_version(item) |
|
|
|
|
|
|
|
|
# Determine if we should be supporting prereleases in this specifier |
|
|
# Determine if we should be supporting prereleases in this specifier |
|
|
# or not, if we do not support prereleases than we can short circuit |
|
|
# or not, if we do not support prereleases than we can short circuit |
|
|
# logic if this version is a prereleases. |
|
|
# logic if this version is a prereleases. |
|
|
if item.is_prerelease and not prereleases: |
|
|
if normalized_item.is_prerelease and not prereleases: |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
# Actually do the comparison to determine if this item is contained |
|
|
# Actually do the comparison to determine if this item is contained |
|
|
# within this Specifier or not. |
|
|
# within this Specifier or not. |
|
|
return self._get_operator(self.operator)(item, self.version) |
|
|
operator_callable = self._get_operator(self.operator) # type: CallableOperator |
|
|
|
|
|
return operator_callable(normalized_item, self.version) |
|
|
|
|
|
|
|
|
def filter(self, iterable, prereleases=None): |
|
|
def filter(self, iterable, prereleases=None): |
|
|
|
|
|
# type: (Iterable[UnparsedVersion], Optional[bool]) -> Iterable[UnparsedVersion] |
|
|
|
|
|
|
|
|
yielded = False |
|
|
yielded = False |
|
|
found_prereleases = [] |
|
|
found_prereleases = [] |
|
|
|
|
|
|
|
@ -194,11 +242,12 @@ class _IndividualSpecifier(BaseSpecifier): |
|
|
# If our version is a prerelease, and we were not set to allow |
|
|
# If our version is a prerelease, and we were not set to allow |
|
|
# prereleases, then we'll store it for later incase nothing |
|
|
# prereleases, then we'll store it for later incase nothing |
|
|
# else matches this specifier. |
|
|
# else matches this specifier. |
|
|
if (parsed_version.is_prerelease and not |
|
|
if parsed_version.is_prerelease and not ( |
|
|
(prereleases or self.prereleases)): |
|
|
prereleases or self.prereleases |
|
|
|
|
|
): |
|
|
found_prereleases.append(version) |
|
|
found_prereleases.append(version) |
|
|
# Either this is not a prerelease, or we should have been |
|
|
# Either this is not a prerelease, or we should have been |
|
|
# accepting prereleases from the begining. |
|
|
# accepting prereleases from the beginning. |
|
|
else: |
|
|
else: |
|
|
yielded = True |
|
|
yielded = True |
|
|
yield version |
|
|
yield version |
|
@ -213,8 +262,7 @@ class _IndividualSpecifier(BaseSpecifier): |
|
|
|
|
|
|
|
|
class LegacySpecifier(_IndividualSpecifier): |
|
|
class LegacySpecifier(_IndividualSpecifier): |
|
|
|
|
|
|
|
|
_regex_str = ( |
|
|
_regex_str = r""" |
|
|
r""" |
|
|
|
|
|
(?P<operator>(==|!=|<=|>=|<|>)) |
|
|
(?P<operator>(==|!=|<=|>=|<|>)) |
|
|
\s* |
|
|
\s* |
|
|
(?P<version> |
|
|
(?P<version> |
|
@ -225,10 +273,8 @@ class LegacySpecifier(_IndividualSpecifier): |
|
|
# them, and a comma since it's a version separator. |
|
|
# them, and a comma since it's a version separator. |
|
|
) |
|
|
) |
|
|
""" |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_regex = re.compile( |
|
|
_regex = re.compile(r"^\s*" + _regex_str + r"\s*$", re.VERBOSE | re.IGNORECASE) |
|
|
r"^\s*" + _regex_str + r"\s*$", re.VERBOSE | re.IGNORECASE) |
|
|
|
|
|
|
|
|
|
|
|
_operators = { |
|
|
_operators = { |
|
|
"==": "equal", |
|
|
"==": "equal", |
|
@ -240,42 +286,53 @@ class LegacySpecifier(_IndividualSpecifier): |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
def _coerce_version(self, version): |
|
|
def _coerce_version(self, version): |
|
|
|
|
|
# type: (Union[ParsedVersion, str]) -> LegacyVersion |
|
|
if not isinstance(version, LegacyVersion): |
|
|
if not isinstance(version, LegacyVersion): |
|
|
version = LegacyVersion(str(version)) |
|
|
version = LegacyVersion(str(version)) |
|
|
return version |
|
|
return version |
|
|
|
|
|
|
|
|
def _compare_equal(self, prospective, spec): |
|
|
def _compare_equal(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective == self._coerce_version(spec) |
|
|
return prospective == self._coerce_version(spec) |
|
|
|
|
|
|
|
|
def _compare_not_equal(self, prospective, spec): |
|
|
def _compare_not_equal(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective != self._coerce_version(spec) |
|
|
return prospective != self._coerce_version(spec) |
|
|
|
|
|
|
|
|
def _compare_less_than_equal(self, prospective, spec): |
|
|
def _compare_less_than_equal(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective <= self._coerce_version(spec) |
|
|
return prospective <= self._coerce_version(spec) |
|
|
|
|
|
|
|
|
def _compare_greater_than_equal(self, prospective, spec): |
|
|
def _compare_greater_than_equal(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective >= self._coerce_version(spec) |
|
|
return prospective >= self._coerce_version(spec) |
|
|
|
|
|
|
|
|
def _compare_less_than(self, prospective, spec): |
|
|
def _compare_less_than(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective < self._coerce_version(spec) |
|
|
return prospective < self._coerce_version(spec) |
|
|
|
|
|
|
|
|
def _compare_greater_than(self, prospective, spec): |
|
|
def _compare_greater_than(self, prospective, spec): |
|
|
|
|
|
# type: (LegacyVersion, str) -> bool |
|
|
return prospective > self._coerce_version(spec) |
|
|
return prospective > self._coerce_version(spec) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _require_version_compare(fn): |
|
|
def _require_version_compare( |
|
|
|
|
|
fn # type: (Callable[[Specifier, ParsedVersion, str], bool]) |
|
|
|
|
|
): |
|
|
|
|
|
# type: (...) -> Callable[[Specifier, ParsedVersion, str], bool] |
|
|
@functools.wraps(fn) |
|
|
@functools.wraps(fn) |
|
|
def wrapped(self, prospective, spec): |
|
|
def wrapped(self, prospective, spec): |
|
|
|
|
|
# type: (Specifier, ParsedVersion, str) -> bool |
|
|
if not isinstance(prospective, Version): |
|
|
if not isinstance(prospective, Version): |
|
|
return False |
|
|
return False |
|
|
return fn(self, prospective, spec) |
|
|
return fn(self, prospective, spec) |
|
|
|
|
|
|
|
|
return wrapped |
|
|
return wrapped |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Specifier(_IndividualSpecifier): |
|
|
class Specifier(_IndividualSpecifier): |
|
|
|
|
|
|
|
|
_regex_str = ( |
|
|
_regex_str = r""" |
|
|
r""" |
|
|
|
|
|
(?P<operator>(~=|==|!=|<=|>=|<|>|===)) |
|
|
(?P<operator>(~=|==|!=|<=|>=|<|>|===)) |
|
|
(?P<version> |
|
|
(?P<version> |
|
|
(?: |
|
|
(?: |
|
@ -367,10 +424,8 @@ class Specifier(_IndividualSpecifier): |
|
|
) |
|
|
) |
|
|
) |
|
|
) |
|
|
""" |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_regex = re.compile( |
|
|
_regex = re.compile(r"^\s*" + _regex_str + r"\s*$", re.VERBOSE | re.IGNORECASE) |
|
|
r"^\s*" + _regex_str + r"\s*$", re.VERBOSE | re.IGNORECASE) |
|
|
|
|
|
|
|
|
|
|
|
_operators = { |
|
|
_operators = { |
|
|
"~=": "compatible", |
|
|
"~=": "compatible", |
|
@ -385,6 +440,8 @@ class Specifier(_IndividualSpecifier): |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_compatible(self, prospective, spec): |
|
|
def _compare_compatible(self, prospective, spec): |
|
|
|
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
# Compatible releases have an equivalent combination of >= and ==. That |
|
|
# Compatible releases have an equivalent combination of >= and ==. That |
|
|
# is that ~=2.2 is equivalent to >=2.2,==2.*. This allows us to |
|
|
# is that ~=2.2 is equivalent to >=2.2,==2.*. This allows us to |
|
|
# implement this in terms of the other specifiers instead of |
|
|
# implement this in terms of the other specifiers instead of |
|
@ -397,8 +454,7 @@ class Specifier(_IndividualSpecifier): |
|
|
prefix = ".".join( |
|
|
prefix = ".".join( |
|
|
list( |
|
|
list( |
|
|
itertools.takewhile( |
|
|
itertools.takewhile( |
|
|
lambda x: (not x.startswith("post") and not |
|
|
lambda x: (not x.startswith("post") and not x.startswith("dev")), |
|
|
x.startswith("dev")), |
|
|
|
|
|
_version_split(spec), |
|
|
_version_split(spec), |
|
|
) |
|
|
) |
|
|
)[:-1] |
|
|
)[:-1] |
|
@ -407,61 +463,81 @@ class Specifier(_IndividualSpecifier): |
|
|
# Add the prefix notation to the end of our string |
|
|
# Add the prefix notation to the end of our string |
|
|
prefix += ".*" |
|
|
prefix += ".*" |
|
|
|
|
|
|
|
|
return (self._get_operator(">=")(prospective, spec) and |
|
|
return self._get_operator(">=")(prospective, spec) and self._get_operator("==")( |
|
|
self._get_operator("==")(prospective, prefix)) |
|
|
prospective, prefix |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_equal(self, prospective, spec): |
|
|
def _compare_equal(self, prospective, spec): |
|
|
|
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
# We need special logic to handle prefix matching |
|
|
# We need special logic to handle prefix matching |
|
|
if spec.endswith(".*"): |
|
|
if spec.endswith(".*"): |
|
|
# In the case of prefix matching we want to ignore local segment. |
|
|
# In the case of prefix matching we want to ignore local segment. |
|
|
prospective = Version(prospective.public) |
|
|
prospective = Version(prospective.public) |
|
|
# Split the spec out by dots, and pretend that there is an implicit |
|
|
# Split the spec out by dots, and pretend that there is an implicit |
|
|
# dot in between a release segment and a pre-release segment. |
|
|
# dot in between a release segment and a pre-release segment. |
|
|
spec = _version_split(spec[:-2]) # Remove the trailing .* |
|
|
split_spec = _version_split(spec[:-2]) # Remove the trailing .* |
|
|
|
|
|
|
|
|
# Split the prospective version out by dots, and pretend that there |
|
|
# Split the prospective version out by dots, and pretend that there |
|
|
# is an implicit dot in between a release segment and a pre-release |
|
|
# is an implicit dot in between a release segment and a pre-release |
|
|
# segment. |
|
|
# segment. |
|
|
prospective = _version_split(str(prospective)) |
|
|
split_prospective = _version_split(str(prospective)) |
|
|
|
|
|
|
|
|
# Shorten the prospective version to be the same length as the spec |
|
|
# Shorten the prospective version to be the same length as the spec |
|
|
# so that we can determine if the specifier is a prefix of the |
|
|
# so that we can determine if the specifier is a prefix of the |
|
|
# prospective version or not. |
|
|
# prospective version or not. |
|
|
prospective = prospective[:len(spec)] |
|
|
shortened_prospective = split_prospective[: len(split_spec)] |
|
|
|
|
|
|
|
|
# Pad out our two sides with zeros so that they both equal the same |
|
|
# Pad out our two sides with zeros so that they both equal the same |
|
|
# length. |
|
|
# length. |
|
|
spec, prospective = _pad_version(spec, prospective) |
|
|
padded_spec, padded_prospective = _pad_version( |
|
|
|
|
|
split_spec, shortened_prospective |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
return padded_prospective == padded_spec |
|
|
else: |
|
|
else: |
|
|
# Convert our spec string into a Version |
|
|
# Convert our spec string into a Version |
|
|
spec = Version(spec) |
|
|
spec_version = Version(spec) |
|
|
|
|
|
|
|
|
# If the specifier does not have a local segment, then we want to |
|
|
# If the specifier does not have a local segment, then we want to |
|
|
# act as if the prospective version also does not have a local |
|
|
# act as if the prospective version also does not have a local |
|
|
# segment. |
|
|
# segment. |
|
|
if not spec.local: |
|
|
if not spec_version.local: |
|
|
prospective = Version(prospective.public) |
|
|
prospective = Version(prospective.public) |
|
|
|
|
|
|
|
|
return prospective == spec |
|
|
return prospective == spec_version |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_not_equal(self, prospective, spec): |
|
|
def _compare_not_equal(self, prospective, spec): |
|
|
|
|
|
# type: (ParsedVersion, str) -> bool |
|
|
return not self._compare_equal(prospective, spec) |
|
|
return not self._compare_equal(prospective, spec) |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_less_than_equal(self, prospective, spec): |
|
|
def _compare_less_than_equal(self, prospective, spec): |
|
|
return prospective <= Version(spec) |
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
|
|
|
# NB: Local version identifiers are NOT permitted in the version |
|
|
|
|
|
# specifier, so local version labels can be universally removed from |
|
|
|
|
|
# the prospective version. |
|
|
|
|
|
return Version(prospective.public) <= Version(spec) |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_greater_than_equal(self, prospective, spec): |
|
|
def _compare_greater_than_equal(self, prospective, spec): |
|
|
return prospective >= Version(spec) |
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
|
|
|
# NB: Local version identifiers are NOT permitted in the version |
|
|
|
|
|
# specifier, so local version labels can be universally removed from |
|
|
|
|
|
# the prospective version. |
|
|
|
|
|
return Version(prospective.public) >= Version(spec) |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_less_than(self, prospective, spec): |
|
|
def _compare_less_than(self, prospective, spec_str): |
|
|
|
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
# Convert our spec to a Version instance, since we'll want to work with |
|
|
# Convert our spec to a Version instance, since we'll want to work with |
|
|
# it as a version. |
|
|
# it as a version. |
|
|
spec = Version(spec) |
|
|
spec = Version(spec_str) |
|
|
|
|
|
|
|
|
# Check to see if the prospective version is less than the spec |
|
|
# Check to see if the prospective version is less than the spec |
|
|
# version. If it's not we can short circuit and just return False now |
|
|
# version. If it's not we can short circuit and just return False now |
|
@ -483,10 +559,12 @@ class Specifier(_IndividualSpecifier): |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
@_require_version_compare |
|
|
@_require_version_compare |
|
|
def _compare_greater_than(self, prospective, spec): |
|
|
def _compare_greater_than(self, prospective, spec_str): |
|
|
|
|
|
# type: (ParsedVersion, str) -> bool |
|
|
|
|
|
|
|
|
# Convert our spec to a Version instance, since we'll want to work with |
|
|
# Convert our spec to a Version instance, since we'll want to work with |
|
|
# it as a version. |
|
|
# it as a version. |
|
|
spec = Version(spec) |
|
|
spec = Version(spec_str) |
|
|
|
|
|
|
|
|
# Check to see if the prospective version is greater than the spec |
|
|
# Check to see if the prospective version is greater than the spec |
|
|
# version. If it's not we can short circuit and just return False now |
|
|
# version. If it's not we can short circuit and just return False now |
|
@ -503,7 +581,7 @@ class Specifier(_IndividualSpecifier): |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
# Ensure that we do not allow a local version of the version mentioned |
|
|
# Ensure that we do not allow a local version of the version mentioned |
|
|
# in the specifier, which is techincally greater than, to match. |
|
|
# in the specifier, which is technically greater than, to match. |
|
|
if prospective.local is not None: |
|
|
if prospective.local is not None: |
|
|
if Version(prospective.base_version) == Version(spec.base_version): |
|
|
if Version(prospective.base_version) == Version(spec.base_version): |
|
|
return False |
|
|
return False |
|
@ -514,10 +592,13 @@ class Specifier(_IndividualSpecifier): |
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
def _compare_arbitrary(self, prospective, spec): |
|
|
def _compare_arbitrary(self, prospective, spec): |
|
|
|
|
|
# type: (Version, str) -> bool |
|
|
return str(prospective).lower() == str(spec).lower() |
|
|
return str(prospective).lower() == str(spec).lower() |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def prereleases(self): |
|
|
def prereleases(self): |
|
|
|
|
|
# type: () -> bool |
|
|
|
|
|
|
|
|
# If there is an explicit prereleases set for this, then we'll just |
|
|
# If there is an explicit prereleases set for this, then we'll just |
|
|
# blindly use that. |
|
|
# blindly use that. |
|
|
if self._prereleases is not None: |
|
|
if self._prereleases is not None: |
|
@ -542,6 +623,7 @@ class Specifier(_IndividualSpecifier): |
|
|
|
|
|
|
|
|
@prereleases.setter |
|
|
@prereleases.setter |
|
|
def prereleases(self, value): |
|
|
def prereleases(self, value): |
|
|
|
|
|
# type: (bool) -> None |
|
|
self._prereleases = value |
|
|
self._prereleases = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -549,7 +631,8 @@ _prefix_regex = re.compile(r"^([0-9]+)((?:a|b|c|rc)[0-9]+)$") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _version_split(version): |
|
|
def _version_split(version): |
|
|
result = [] |
|
|
# type: (str) -> List[str] |
|
|
|
|
|
result = [] # type: List[str] |
|
|
for item in version.split("."): |
|
|
for item in version.split("."): |
|
|
match = _prefix_regex.search(item) |
|
|
match = _prefix_regex.search(item) |
|
|
if match: |
|
|
if match: |
|
@ -560,6 +643,7 @@ def _version_split(version): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _pad_version(left, right): |
|
|
def _pad_version(left, right): |
|
|
|
|
|
# type: (List[str], List[str]) -> Tuple[List[str], List[str]] |
|
|
left_split, right_split = [], [] |
|
|
left_split, right_split = [], [] |
|
|
|
|
|
|
|
|
# Get the release segment of our versions |
|
|
# Get the release segment of our versions |
|
@ -567,36 +651,28 @@ def _pad_version(left, right): |
|
|
right_split.append(list(itertools.takewhile(lambda x: x.isdigit(), right))) |
|
|
right_split.append(list(itertools.takewhile(lambda x: x.isdigit(), right))) |
|
|
|
|
|
|
|
|
# Get the rest of our versions |
|
|
# Get the rest of our versions |
|
|
left_split.append(left[len(left_split[0]):]) |
|
|
left_split.append(left[len(left_split[0]) :]) |
|
|
right_split.append(right[len(right_split[0]):]) |
|
|
right_split.append(right[len(right_split[0]) :]) |
|
|
|
|
|
|
|
|
# Insert our padding |
|
|
# Insert our padding |
|
|
left_split.insert( |
|
|
left_split.insert(1, ["0"] * max(0, len(right_split[0]) - len(left_split[0]))) |
|
|
1, |
|
|
right_split.insert(1, ["0"] * max(0, len(left_split[0]) - len(right_split[0]))) |
|
|
["0"] * max(0, len(right_split[0]) - len(left_split[0])), |
|
|
|
|
|
) |
|
|
|
|
|
right_split.insert( |
|
|
|
|
|
1, |
|
|
|
|
|
["0"] * max(0, len(left_split[0]) - len(right_split[0])), |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
return (list(itertools.chain(*left_split)), list(itertools.chain(*right_split))) |
|
|
list(itertools.chain(*left_split)), |
|
|
|
|
|
list(itertools.chain(*right_split)), |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SpecifierSet(BaseSpecifier): |
|
|
class SpecifierSet(BaseSpecifier): |
|
|
|
|
|
|
|
|
def __init__(self, specifiers="", prereleases=None): |
|
|
def __init__(self, specifiers="", prereleases=None): |
|
|
# Split on , to break each indidivual specifier into it's own item, and |
|
|
# type: (str, Optional[bool]) -> None |
|
|
|
|
|
|
|
|
|
|
|
# Split on , to break each individual specifier into it's own item, and |
|
|
# strip each item to remove leading/trailing whitespace. |
|
|
# strip each item to remove leading/trailing whitespace. |
|
|
specifiers = [s.strip() for s in specifiers.split(",") if s.strip()] |
|
|
split_specifiers = [s.strip() for s in specifiers.split(",") if s.strip()] |
|
|
|
|
|
|
|
|
# Parsed each individual specifier, attempting first to make it a |
|
|
# Parsed each individual specifier, attempting first to make it a |
|
|
# Specifier and falling back to a LegacySpecifier. |
|
|
# Specifier and falling back to a LegacySpecifier. |
|
|
parsed = set() |
|
|
parsed = set() |
|
|
for specifier in specifiers: |
|
|
for specifier in split_specifiers: |
|
|
try: |
|
|
try: |
|
|
parsed.add(Specifier(specifier)) |
|
|
parsed.add(Specifier(specifier)) |
|
|
except InvalidSpecifier: |
|
|
except InvalidSpecifier: |
|
@ -610,6 +686,7 @@ class SpecifierSet(BaseSpecifier): |
|
|
self._prereleases = prereleases |
|
|
self._prereleases = prereleases |
|
|
|
|
|
|
|
|
def __repr__(self): |
|
|
def __repr__(self): |
|
|
|
|
|
# type: () -> str |
|
|
pre = ( |
|
|
pre = ( |
|
|
", prereleases={0!r}".format(self.prereleases) |
|
|
", prereleases={0!r}".format(self.prereleases) |
|
|
if self._prereleases is not None |
|
|
if self._prereleases is not None |
|
@ -619,12 +696,15 @@ class SpecifierSet(BaseSpecifier): |
|
|
return "<SpecifierSet({0!r}{1})>".format(str(self), pre) |
|
|
return "<SpecifierSet({0!r}{1})>".format(str(self), pre) |
|
|
|
|
|
|
|
|
def __str__(self): |
|
|
def __str__(self): |
|
|
|
|
|
# type: () -> str |
|
|
return ",".join(sorted(str(s) for s in self._specs)) |
|
|
return ",".join(sorted(str(s) for s in self._specs)) |
|
|
|
|
|
|
|
|
def __hash__(self): |
|
|
def __hash__(self): |
|
|
|
|
|
# type: () -> int |
|
|
return hash(self._specs) |
|
|
return hash(self._specs) |
|
|
|
|
|
|
|
|
def __and__(self, other): |
|
|
def __and__(self, other): |
|
|
|
|
|
# type: (Union[SpecifierSet, str]) -> SpecifierSet |
|
|
if isinstance(other, string_types): |
|
|
if isinstance(other, string_types): |
|
|
other = SpecifierSet(other) |
|
|
other = SpecifierSet(other) |
|
|
elif not isinstance(other, SpecifierSet): |
|
|
elif not isinstance(other, SpecifierSet): |
|
@ -648,9 +728,8 @@ class SpecifierSet(BaseSpecifier): |
|
|
return specifier |
|
|
return specifier |
|
|
|
|
|
|
|
|
def __eq__(self, other): |
|
|
def __eq__(self, other): |
|
|
if isinstance(other, string_types): |
|
|
# type: (object) -> bool |
|
|
other = SpecifierSet(other) |
|
|
if isinstance(other, (string_types, _IndividualSpecifier)): |
|
|
elif isinstance(other, _IndividualSpecifier): |
|
|
|
|
|
other = SpecifierSet(str(other)) |
|
|
other = SpecifierSet(str(other)) |
|
|
elif not isinstance(other, SpecifierSet): |
|
|
elif not isinstance(other, SpecifierSet): |
|
|
return NotImplemented |
|
|
return NotImplemented |
|
@ -658,9 +737,8 @@ class SpecifierSet(BaseSpecifier): |
|
|
return self._specs == other._specs |
|
|
return self._specs == other._specs |
|
|
|
|
|
|
|
|
def __ne__(self, other): |
|
|
def __ne__(self, other): |
|
|
if isinstance(other, string_types): |
|
|
# type: (object) -> bool |
|
|
other = SpecifierSet(other) |
|
|
if isinstance(other, (string_types, _IndividualSpecifier)): |
|
|
elif isinstance(other, _IndividualSpecifier): |
|
|
|
|
|
other = SpecifierSet(str(other)) |
|
|
other = SpecifierSet(str(other)) |
|
|
elif not isinstance(other, SpecifierSet): |
|
|
elif not isinstance(other, SpecifierSet): |
|
|
return NotImplemented |
|
|
return NotImplemented |
|
@ -668,13 +746,17 @@ class SpecifierSet(BaseSpecifier): |
|
|
return self._specs != other._specs |
|
|
return self._specs != other._specs |
|
|
|
|
|
|
|
|
def __len__(self): |
|
|
def __len__(self): |
|
|
|
|
|
# type: () -> int |
|
|
return len(self._specs) |
|
|
return len(self._specs) |
|
|
|
|
|
|
|
|
def __iter__(self): |
|
|
def __iter__(self): |
|
|
|
|
|
# type: () -> Iterator[FrozenSet[_IndividualSpecifier]] |
|
|
return iter(self._specs) |
|
|
return iter(self._specs) |
|
|
|
|
|
|
|
|
@property |
|
|
@property |
|
|
def prereleases(self): |
|
|
def prereleases(self): |
|
|
|
|
|
# type: () -> Optional[bool] |
|
|
|
|
|
|
|
|
# If we have been given an explicit prerelease modifier, then we'll |
|
|
# If we have been given an explicit prerelease modifier, then we'll |
|
|
# pass that through here. |
|
|
# pass that through here. |
|
|
if self._prereleases is not None: |
|
|
if self._prereleases is not None: |
|
@ -692,12 +774,16 @@ class SpecifierSet(BaseSpecifier): |
|
|
|
|
|
|
|
|
@prereleases.setter |
|
|
@prereleases.setter |
|
|
def prereleases(self, value): |
|
|
def prereleases(self, value): |
|
|
|
|
|
# type: (bool) -> None |
|
|
self._prereleases = value |
|
|
self._prereleases = value |
|
|
|
|
|
|
|
|
def __contains__(self, item): |
|
|
def __contains__(self, item): |
|
|
|
|
|
# type: (Union[ParsedVersion, str]) -> bool |
|
|
return self.contains(item) |
|
|
return self.contains(item) |
|
|
|
|
|
|
|
|
def contains(self, item, prereleases=None): |
|
|
def contains(self, item, prereleases=None): |
|
|
|
|
|
# type: (Union[ParsedVersion, str], Optional[bool]) -> bool |
|
|
|
|
|
|
|
|
# Ensure that our item is a Version or LegacyVersion instance. |
|
|
# Ensure that our item is a Version or LegacyVersion instance. |
|
|
if not isinstance(item, (LegacyVersion, Version)): |
|
|
if not isinstance(item, (LegacyVersion, Version)): |
|
|
item = parse(item) |
|
|
item = parse(item) |
|
@ -721,12 +807,15 @@ class SpecifierSet(BaseSpecifier): |
|
|
# given version is contained within all of them. |
|
|
# given version is contained within all of them. |
|
|
# Note: This use of all() here means that an empty set of specifiers |
|
|
# Note: This use of all() here means that an empty set of specifiers |
|
|
# will always return True, this is an explicit design decision. |
|
|
# will always return True, this is an explicit design decision. |
|
|
return all( |
|
|
return all(s.contains(item, prereleases=prereleases) for s in self._specs) |
|
|
s.contains(item, prereleases=prereleases) |
|
|
|
|
|
for s in self._specs |
|
|
def filter( |
|
|
) |
|
|
self, |
|
|
|
|
|
iterable, # type: Iterable[Union[ParsedVersion, str]] |
|
|
|
|
|
prereleases=None, # type: Optional[bool] |
|
|
|
|
|
): |
|
|
|
|
|
# type: (...) -> Iterable[Union[ParsedVersion, str]] |
|
|
|
|
|
|
|
|
def filter(self, iterable, prereleases=None): |
|
|
|
|
|
# Determine if we're forcing a prerelease or not, if we're not forcing |
|
|
# Determine if we're forcing a prerelease or not, if we're not forcing |
|
|
# one for this particular filter call, then we'll use whatever the |
|
|
# one for this particular filter call, then we'll use whatever the |
|
|
# SpecifierSet thinks for whether or not we should support prereleases. |
|
|
# SpecifierSet thinks for whether or not we should support prereleases. |
|
@ -744,8 +833,8 @@ class SpecifierSet(BaseSpecifier): |
|
|
# which will filter out any pre-releases, unless there are no final |
|
|
# which will filter out any pre-releases, unless there are no final |
|
|
# releases, and which will filter out LegacyVersion in general. |
|
|
# releases, and which will filter out LegacyVersion in general. |
|
|
else: |
|
|
else: |
|
|
filtered = [] |
|
|
filtered = [] # type: List[Union[ParsedVersion, str]] |
|
|
found_prereleases = [] |
|
|
found_prereleases = [] # type: List[Union[ParsedVersion, str]] |
|
|
|
|
|
|
|
|
for item in iterable: |
|
|
for item in iterable: |
|
|
# Ensure that we some kind of Version class for this item. |
|
|
# Ensure that we some kind of Version class for this item. |
|
|