mirror of https://github.com/ansible/ansible.git
Support pre-releases via new SemanticVersion (#68258)
* Support pre-releases via new SemanticVersion. Fixes #64905 * Don't treat buildmeta as prerelease * Don't inherit from str and int * Add helper method to try and construct a SemanticVersion from a LooseVersion * Don't count major 0 as pre-release, it's different * Guard against invalid or no version in LooseVersion * return a bool * Add integration tests for pre-release * Fix up lingering issues with comparisons * typo fix * Always allow pre-releases in verify * Move pre-release filtering into CollectionRequirement, add messaging when a collection only contains pre-releases * Update changelog * If explicit requirement allow pre releases * Enable pre-releases for tar installs, and collections already installed when they are pre-releases * Drop --pre-release alias, make arg name more clear * Simplify code into a single line * Remove build metadata precedence, add some comments, and is_stable helper * Improve from_loose_version * Increase test coverage * linting fix * Update changelogpull/68417/head
parent
ed9de94ad9
commit
d3ec31f8d5
@ -0,0 +1,4 @@
|
||||
minor_changes:
|
||||
- Add ``--pre`` flag to ``ansible-galaxy collection install``
|
||||
to allow pulling in the most recent pre-release version of a collection
|
||||
(https://github.com/ansible/ansible/issues/64905)
|
@ -0,0 +1,272 @@
|
||||
# Copyright (c) 2020 Matt Martz <matt@sivel.net>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import re
|
||||
|
||||
from distutils.version import LooseVersion, Version
|
||||
|
||||
from ansible.module_utils.six import text_type
|
||||
|
||||
|
||||
# Regular expression taken from
|
||||
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
|
||||
SEMVER_RE = re.compile(
|
||||
r'''
|
||||
^
|
||||
(?P<major>0|[1-9]\d*)
|
||||
\.
|
||||
(?P<minor>0|[1-9]\d*)
|
||||
\.
|
||||
(?P<patch>0|[1-9]\d*)
|
||||
(?:
|
||||
-
|
||||
(?P<prerelease>
|
||||
(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
|
||||
(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*
|
||||
)
|
||||
)?
|
||||
(?:
|
||||
\+
|
||||
(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)
|
||||
)?
|
||||
$
|
||||
''',
|
||||
flags=re.X
|
||||
)
|
||||
|
||||
|
||||
class _Alpha:
|
||||
"""Class to easily allow comparing strings
|
||||
|
||||
Largely this exists to make comparing an integer and a string on py3
|
||||
so that it works like py2.
|
||||
"""
|
||||
def __init__(self, specifier):
|
||||
self.specifier = specifier
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.specifier)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, _Alpha):
|
||||
return self.specifier == other.specifier
|
||||
elif isinstance(other, str):
|
||||
return self.specifier == other
|
||||
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, _Alpha):
|
||||
return self.specifier < other.specifier
|
||||
elif isinstance(other, str):
|
||||
return self.specifier < other
|
||||
elif isinstance(other, _Numeric):
|
||||
return False
|
||||
|
||||
raise ValueError
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__lt__(other)
|
||||
|
||||
def __le__(self, other):
|
||||
return self.__lt__(other) or self.__eq__(other)
|
||||
|
||||
def __ge__(self, other):
|
||||
return self.__gt__(other) or self.__eq__(other)
|
||||
|
||||
|
||||
class _Numeric:
|
||||
"""Class to easily allow comparing numbers
|
||||
|
||||
Largely this exists to make comparing an integer and a string on py3
|
||||
so that it works like py2.
|
||||
"""
|
||||
def __init__(self, specifier):
|
||||
self.specifier = int(specifier)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.specifier)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, _Numeric):
|
||||
return self.specifier == other.specifier
|
||||
elif isinstance(other, int):
|
||||
return self.specifier == other
|
||||
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, _Numeric):
|
||||
return self.specifier < other.specifier
|
||||
elif isinstance(other, int):
|
||||
return self.specifier < other
|
||||
elif isinstance(other, _Alpha):
|
||||
return True
|
||||
|
||||
raise ValueError
|
||||
|
||||
def __gt__(self, other):
|
||||
return not self.__lt__(other)
|
||||
|
||||
def __le__(self, other):
|
||||
return self.__lt__(other) or self.__eq__(other)
|
||||
|
||||
def __ge__(self, other):
|
||||
return self.__gt__(other) or self.__eq__(other)
|
||||
|
||||
|
||||
class SemanticVersion(Version):
|
||||
"""Version comparison class that implements Semantic Versioning 2.0.0
|
||||
|
||||
Based off of ``distutils.version.Version``
|
||||
"""
|
||||
|
||||
version_re = SEMVER_RE
|
||||
|
||||
def __init__(self, vstring=None):
|
||||
self.vstring = vstring
|
||||
self.major = None
|
||||
self.minor = None
|
||||
self.patch = None
|
||||
self.prerelease = ()
|
||||
self.buildmetadata = ()
|
||||
|
||||
if vstring:
|
||||
self.parse(vstring)
|
||||
|
||||
def __repr__(self):
|
||||
return 'SemanticVersion(%r)' % self.vstring
|
||||
|
||||
@staticmethod
|
||||
def from_loose_version(loose_version):
|
||||
"""This method is designed to take a ``LooseVersion``
|
||||
and attempt to construct a ``SemanticVersion`` from it
|
||||
|
||||
This is useful where you want to do simple version math
|
||||
without requiring users to provide a compliant semver.
|
||||
"""
|
||||
if not isinstance(loose_version, LooseVersion):
|
||||
raise ValueError("%r is not a LooseVersion" % loose_version)
|
||||
|
||||
try:
|
||||
version = loose_version.version[:]
|
||||
except AttributeError:
|
||||
raise ValueError("%r is not a LooseVersion" % loose_version)
|
||||
|
||||
extra_idx = 3
|
||||
for marker in ('-', '+'):
|
||||
try:
|
||||
idx = version.index(marker)
|
||||
except ValueError:
|
||||
continue
|
||||
else:
|
||||
if idx < extra_idx:
|
||||
extra_idx = idx
|
||||
version[:] = version[:extra_idx]
|
||||
|
||||
if version and set(type(v) for v in version) != set((int,)):
|
||||
raise ValueError("Non integer values in %r" % loose_version)
|
||||
|
||||
# Extra is everything to the right of the core version
|
||||
extra = re.search('[+-].+$', loose_version.vstring)
|
||||
|
||||
version[:] = version + [0] * (3 - len(version))
|
||||
return SemanticVersion(
|
||||
'%s%s' % (
|
||||
'.'.join(str(v) for v in version),
|
||||
extra.group(0) if extra else ''
|
||||
)
|
||||
)
|
||||
|
||||
def parse(self, vstring):
|
||||
match = SEMVER_RE.match(vstring)
|
||||
if not match:
|
||||
raise ValueError("invalid semantic version '%s'" % vstring)
|
||||
|
||||
(major, minor, patch, prerelease, buildmetadata) = match.group(1, 2, 3, 4, 5)
|
||||
self.major = int(major)
|
||||
self.minor = int(minor)
|
||||
self.patch = int(patch)
|
||||
|
||||
if prerelease:
|
||||
self.prerelease = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in prerelease.split('.'))
|
||||
if buildmetadata:
|
||||
self.buildmetadata = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in buildmetadata.split('.'))
|
||||
|
||||
@property
|
||||
def core(self):
|
||||
return self.major, self.minor, self.patch
|
||||
|
||||
@property
|
||||
def is_prerelease(self):
|
||||
return bool(self.prerelease)
|
||||
|
||||
@property
|
||||
def is_stable(self):
|
||||
# Major version zero (0.y.z) is for initial development. Anything MAY change at any time.
|
||||
# The public API SHOULD NOT be considered stable.
|
||||
# https://semver.org/#spec-item-4
|
||||
return not (self.major == 0 or self.is_prerelease)
|
||||
|
||||
def _cmp(self, other):
|
||||
if isinstance(other, str):
|
||||
other = SemanticVersion(other)
|
||||
|
||||
if self.core != other.core:
|
||||
# if the core version doesn't match
|
||||
# prerelease and buildmetadata doesn't matter
|
||||
if self.core < other.core:
|
||||
return -1
|
||||
else:
|
||||
return 1
|
||||
|
||||
if not any((self.prerelease, other.prerelease)):
|
||||
return 0
|
||||
|
||||
if self.prerelease and not other.prerelease:
|
||||
return -1
|
||||
elif not self.prerelease and other.prerelease:
|
||||
return 1
|
||||
else:
|
||||
if self.prerelease < other.prerelease:
|
||||
return -1
|
||||
elif self.prerelease > other.prerelease:
|
||||
return 1
|
||||
|
||||
# Build metadata MUST be ignored when determining version precedence
|
||||
# https://semver.org/#spec-item-10
|
||||
# With the above in mind it is ignored here
|
||||
|
||||
# If we have made it here, things should be equal
|
||||
return 0
|
||||
|
||||
# The Py2 and Py3 implementations of distutils.version.Version
|
||||
# are quite different, this makes the Py2 and Py3 implementations
|
||||
# the same
|
||||
def __eq__(self, other):
|
||||
return self._cmp(other) == 0
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
return self._cmp(other) < 0
|
||||
|
||||
def __le__(self, other):
|
||||
return self._cmp(other) <= 0
|
||||
|
||||
def __gt__(self, other):
|
||||
return self._cmp(other) > 0
|
||||
|
||||
def __ge__(self, other):
|
||||
return self._cmp(other) >= 0
|
@ -0,0 +1,285 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# (c) 2020 Matt Martz <matt@sivel.net>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from distutils.version import LooseVersion, StrictVersion
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.utils.version import _Alpha, _Numeric, SemanticVersion
|
||||
|
||||
|
||||
EQ = [
|
||||
('1.0.0', '1.0.0', True),
|
||||
('1.0.0', '1.0.0-beta', False),
|
||||
('1.0.0-beta2+build1', '1.0.0-beta.2+build.1', False),
|
||||
('1.0.0-beta+build', '1.0.0-beta+build', True),
|
||||
('1.0.0-beta+build1', '1.0.0-beta+build2', True),
|
||||
('1.0.0-beta+a', '1.0.0-alpha+bar', False),
|
||||
]
|
||||
|
||||
NE = [
|
||||
('1.0.0', '1.0.0', False),
|
||||
('1.0.0', '1.0.0-beta', True),
|
||||
('1.0.0-beta2+build1', '1.0.0-beta.2+build.1', True),
|
||||
('1.0.0-beta+build', '1.0.0-beta+build', False),
|
||||
('1.0.0-beta+a', '1.0.0-alpha+bar', True),
|
||||
]
|
||||
|
||||
LT = [
|
||||
('1.0.0', '2.0.0', True),
|
||||
('1.0.0-beta', '2.0.0-alpha', True),
|
||||
('1.0.0-alpha', '2.0.0-beta', True),
|
||||
('1.0.0-alpha', '1.0.0', True),
|
||||
('1.0.0-beta', '1.0.0-alpha3', False),
|
||||
('1.0.0+foo', '1.0.0-alpha', False),
|
||||
('1.0.0-beta.1', '1.0.0-beta.a', True),
|
||||
('1.0.0-beta+a', '1.0.0-alpha+bar', False),
|
||||
]
|
||||
|
||||
GT = [
|
||||
('1.0.0', '2.0.0', False),
|
||||
('1.0.0-beta', '2.0.0-alpha', False),
|
||||
('1.0.0-alpha', '2.0.0-beta', False),
|
||||
('1.0.0-alpha', '1.0.0', False),
|
||||
('1.0.0-beta', '1.0.0-alpha3', True),
|
||||
('1.0.0+foo', '1.0.0-alpha', True),
|
||||
('1.0.0-beta.1', '1.0.0-beta.a', False),
|
||||
('1.0.0-beta+a', '1.0.0-alpha+bar', True),
|
||||
]
|
||||
|
||||
LE = [
|
||||
('1.0.0', '1.0.0', True),
|
||||
('1.0.0', '2.0.0', True),
|
||||
('1.0.0-alpha', '1.0.0-beta', True),
|
||||
('1.0.0-beta', '1.0.0-alpha', False),
|
||||
]
|
||||
|
||||
GE = [
|
||||
('1.0.0', '1.0.0', True),
|
||||
('1.0.0', '2.0.0', False),
|
||||
('1.0.0-alpha', '1.0.0-beta', False),
|
||||
('1.0.0-beta', '1.0.0-alpha', True),
|
||||
]
|
||||
|
||||
VALID = [
|
||||
"0.0.4",
|
||||
"1.2.3",
|
||||
"10.20.30",
|
||||
"1.1.2-prerelease+meta",
|
||||
"1.1.2+meta",
|
||||
"1.1.2+meta-valid",
|
||||
"1.0.0-alpha",
|
||||
"1.0.0-beta",
|
||||
"1.0.0-alpha.beta",
|
||||
"1.0.0-alpha.beta.1",
|
||||
"1.0.0-alpha.1",
|
||||
"1.0.0-alpha0.valid",
|
||||
"1.0.0-alpha.0valid",
|
||||
"1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
|
||||
"1.0.0-rc.1+build.1",
|
||||
"2.0.0-rc.1+build.123",
|
||||
"1.2.3-beta",
|
||||
"10.2.3-DEV-SNAPSHOT",
|
||||
"1.2.3-SNAPSHOT-123",
|
||||
"1.0.0",
|
||||
"2.0.0",
|
||||
"1.1.7",
|
||||
"2.0.0+build.1848",
|
||||
"2.0.1-alpha.1227",
|
||||
"1.0.0-alpha+beta",
|
||||
"1.2.3----RC-SNAPSHOT.12.9.1--.12+788",
|
||||
"1.2.3----R-S.12.9.1--.12+meta",
|
||||
"1.2.3----RC-SNAPSHOT.12.9.1--.12",
|
||||
"1.0.0+0.build.1-rc.10000aaa-kk-0.1",
|
||||
"99999999999999999999999.999999999999999999.99999999999999999",
|
||||
"1.0.0-0A.is.legal",
|
||||
]
|
||||
|
||||
INVALID = [
|
||||
"1",
|
||||
"1.2",
|
||||
"1.2.3-0123",
|
||||
"1.2.3-0123.0123",
|
||||
"1.1.2+.123",
|
||||
"+invalid",
|
||||
"-invalid",
|
||||
"-invalid+invalid",
|
||||
"-invalid.01",
|
||||
"alpha",
|
||||
"alpha.beta",
|
||||
"alpha.beta.1",
|
||||
"alpha.1",
|
||||
"alpha+beta",
|
||||
"alpha_beta",
|
||||
"alpha.",
|
||||
"alpha..",
|
||||
"beta",
|
||||
"1.0.0-alpha_beta",
|
||||
"-alpha.",
|
||||
"1.0.0-alpha..",
|
||||
"1.0.0-alpha..1",
|
||||
"1.0.0-alpha...1",
|
||||
"1.0.0-alpha....1",
|
||||
"1.0.0-alpha.....1",
|
||||
"1.0.0-alpha......1",
|
||||
"1.0.0-alpha.......1",
|
||||
"01.1.1",
|
||||
"1.01.1",
|
||||
"1.1.01",
|
||||
"1.2",
|
||||
"1.2.3.DEV",
|
||||
"1.2-SNAPSHOT",
|
||||
"1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788",
|
||||
"1.2-RC-SNAPSHOT",
|
||||
"-1.0.3-gamma+b7718",
|
||||
"+justmeta",
|
||||
"9.8.7+meta+meta",
|
||||
"9.8.7-whatever+meta+meta",
|
||||
]
|
||||
|
||||
PRERELEASE = [
|
||||
('1.0.0-alpha', True),
|
||||
('1.0.0-alpha.1', True),
|
||||
('1.0.0-0.3.7', True),
|
||||
('1.0.0-x.7.z.92', True),
|
||||
('0.1.2', False),
|
||||
('0.1.2+bob', False),
|
||||
('1.0.0', False),
|
||||
]
|
||||
|
||||
STABLE = [
|
||||
('1.0.0-alpha', False),
|
||||
('1.0.0-alpha.1', False),
|
||||
('1.0.0-0.3.7', False),
|
||||
('1.0.0-x.7.z.92', False),
|
||||
('0.1.2', False),
|
||||
('0.1.2+bob', False),
|
||||
('1.0.0', True),
|
||||
('1.0.0+bob', True),
|
||||
]
|
||||
|
||||
LOOSE_VERSION = [
|
||||
(LooseVersion('1'), SemanticVersion('1.0.0')),
|
||||
(LooseVersion('1-alpha'), SemanticVersion('1.0.0-alpha')),
|
||||
(LooseVersion('1.0.0-alpha+build'), SemanticVersion('1.0.0-alpha+build')),
|
||||
]
|
||||
|
||||
LOOSE_VERSION_INVALID = [
|
||||
LooseVersion('1.a.3'),
|
||||
LooseVersion(),
|
||||
'bar',
|
||||
StrictVersion('1.2.3'),
|
||||
]
|
||||
|
||||
|
||||
def test_semanticversion_none():
|
||||
assert SemanticVersion().major is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', EQ)
|
||||
def test_eq(left, right, expected):
|
||||
assert (SemanticVersion(left) == SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', NE)
|
||||
def test_ne(left, right, expected):
|
||||
assert (SemanticVersion(left) != SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', LT)
|
||||
def test_lt(left, right, expected):
|
||||
assert (SemanticVersion(left) < SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', LE)
|
||||
def test_le(left, right, expected):
|
||||
assert (SemanticVersion(left) <= SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', GT)
|
||||
def test_gt(left, right, expected):
|
||||
assert (SemanticVersion(left) > SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('left,right,expected', GE)
|
||||
def test_ge(left, right, expected):
|
||||
assert (SemanticVersion(left) >= SemanticVersion(right)) is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value', VALID)
|
||||
def test_valid(value):
|
||||
SemanticVersion(value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value', INVALID)
|
||||
def test_invalid(value):
|
||||
pytest.raises(ValueError, SemanticVersion, value)
|
||||
|
||||
|
||||
def test_example_precedence():
|
||||
# https://semver.org/#spec-item-11
|
||||
sv = SemanticVersion
|
||||
assert sv('1.0.0') < sv('2.0.0') < sv('2.1.0') < sv('2.1.1')
|
||||
assert sv('1.0.0-alpha') < sv('1.0.0')
|
||||
assert sv('1.0.0-alpha') < sv('1.0.0-alpha.1') < sv('1.0.0-alpha.beta')
|
||||
assert sv('1.0.0-beta') < sv('1.0.0-beta.2') < sv('1.0.0-beta.11') < sv('1.0.0-rc.1') < sv('1.0.0')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value,expected', PRERELEASE)
|
||||
def test_prerelease(value, expected):
|
||||
assert SemanticVersion(value).is_prerelease is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value,expected', STABLE)
|
||||
def test_stable(value, expected):
|
||||
assert SemanticVersion(value).is_stable is expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value,expected', LOOSE_VERSION)
|
||||
def test_from_loose_version(value, expected):
|
||||
assert SemanticVersion.from_loose_version(value) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value', LOOSE_VERSION_INVALID)
|
||||
def test_from_loose_version_invalid(value):
|
||||
pytest.raises((AttributeError, ValueError), SemanticVersion.from_loose_version, value)
|
||||
|
||||
|
||||
def test_comparison_with_string():
|
||||
assert SemanticVersion('1.0.0') > '0.1.0'
|
||||
|
||||
|
||||
def test_alpha():
|
||||
assert _Alpha('a') == _Alpha('a')
|
||||
assert _Alpha('a') == 'a'
|
||||
assert _Alpha('a') != _Alpha('b')
|
||||
assert _Alpha('a') != 1
|
||||
assert _Alpha('a') < _Alpha('b')
|
||||
assert _Alpha('a') < 'c'
|
||||
assert _Alpha('a') > _Numeric(1)
|
||||
with pytest.raises(ValueError):
|
||||
_Alpha('a') < None
|
||||
assert _Alpha('a') <= _Alpha('a')
|
||||
assert _Alpha('a') <= _Alpha('b')
|
||||
assert _Alpha('b') >= _Alpha('a')
|
||||
assert _Alpha('b') >= _Alpha('b')
|
||||
|
||||
|
||||
def test_numeric():
|
||||
assert _Numeric(1) == _Numeric(1)
|
||||
assert _Numeric(1) == 1
|
||||
assert _Numeric(1) != _Numeric(2)
|
||||
assert _Numeric(1) != 'a'
|
||||
assert _Numeric(1) < _Numeric(2)
|
||||
assert _Numeric(1) < 3
|
||||
assert _Numeric(1) < _Alpha('b')
|
||||
with pytest.raises(ValueError):
|
||||
_Numeric(1) < None
|
||||
assert _Numeric(1) <= _Numeric(1)
|
||||
assert _Numeric(1) <= _Numeric(2)
|
||||
assert _Numeric(2) >= _Numeric(1)
|
||||
assert _Numeric(2) >= _Numeric(2)
|
Loading…
Reference in New Issue