| 1 |
# Copyright (c) 2020 Matt Martz <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from ansible.module_utils.compat.version import LooseVersion, Version |
| 9 |
|
| 10 |
|
| 11 |
# Regular expression taken from |
| 12 |
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string |
| 13 |
SEMVER_RE = re.compile( |
| 14 |
r""" |
| 15 |
^ |
| 16 |
(?P<major>0|[1-9]\d*) |
| 17 |
\. |
| 18 |
(?P<minor>0|[1-9]\d*) |
| 19 |
\. |
| 20 |
(?P<patch>0|[1-9]\d*) |
| 21 |
(?: |
| 22 |
- |
| 23 |
(?P<prerelease> |
| 24 |
(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) |
| 25 |
(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))* |
| 26 |
) |
| 27 |
)? |
| 28 |
(?: |
| 29 |
\+ |
| 30 |
(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*) |
| 31 |
)? |
| 32 |
$ |
| 33 |
""", |
| 34 |
flags=re.X |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
class _Alpha: |
| 39 |
"""Class to easily allow comparing strings |
| 40 |
|
| 41 |
Largely this exists to make comparing an integer and a string on py3 |
| 42 |
so that it works like py2. |
| 43 |
""" |
| 44 |
def __init__(self, specifier): |
| 45 |
self.specifier = specifier |
| 46 |
|
| 47 |
def __repr__(self): |
| 48 |
return repr(self.specifier) |
| 49 |
|
| 50 |
def __eq__(self, other): |
| 51 |
if isinstance(other, _Alpha): |
| 52 |
return self.specifier == other.specifier |
| 53 |
elif isinstance(other, str): |
| 54 |
return self.specifier == other |
| 55 |
|
| 56 |
return False |
| 57 |
|
| 58 |
def __ne__(self, other): |
| 59 |
return not self.__eq__(other) |
| 60 |
|
| 61 |
def __lt__(self, other): |
| 62 |
if isinstance(other, _Alpha): |
| 63 |
return self.specifier < other.specifier |
| 64 |
elif isinstance(other, str): |
| 65 |
return self.specifier < other |
| 66 |
elif isinstance(other, _Numeric): |
| 67 |
return False |
| 68 |
|
| 69 |
raise ValueError |
| 70 |
|
| 71 |
def __le__(self, other): |
| 72 |
return self.__lt__(other) or self.__eq__(other) |
| 73 |
|
| 74 |
def __gt__(self, other): |
| 75 |
return not self.__le__(other) |
| 76 |
|
| 77 |
def __ge__(self, other): |
| 78 |
return not self.__lt__(other) |
| 79 |
|
| 80 |
|
| 81 |
class _Numeric: |
| 82 |
"""Class to easily allow comparing numbers |
| 83 |
|
| 84 |
Largely this exists to make comparing an integer and a string on py3 |
| 85 |
so that it works like py2. |
| 86 |
""" |
| 87 |
def __init__(self, specifier): |
| 88 |
self.specifier = int(specifier) |
| 89 |
|
| 90 |
def __repr__(self): |
| 91 |
return repr(self.specifier) |
| 92 |
|
| 93 |
def __eq__(self, other): |
| 94 |
if isinstance(other, _Numeric): |
| 95 |
return self.specifier == other.specifier |
| 96 |
elif isinstance(other, int): |
| 97 |
return self.specifier == other |
| 98 |
|
| 99 |
return False |
| 100 |
|
| 101 |
def __ne__(self, other): |
| 102 |
return not self.__eq__(other) |
| 103 |
|
| 104 |
def __lt__(self, other): |
| 105 |
if isinstance(other, _Numeric): |
| 106 |
return self.specifier < other.specifier |
| 107 |
elif isinstance(other, int): |
| 108 |
return self.specifier < other |
| 109 |
elif isinstance(other, _Alpha): |
| 110 |
return True |
| 111 |
|
| 112 |
raise ValueError |
| 113 |
|
| 114 |
def __le__(self, other): |
| 115 |
return self.__lt__(other) or self.__eq__(other) |
| 116 |
|
| 117 |
def __gt__(self, other): |
| 118 |
return not self.__le__(other) |
| 119 |
|
| 120 |
def __ge__(self, other): |
| 121 |
return not self.__lt__(other) |
| 122 |
|
| 123 |
|
| 124 |
class SemanticVersion(Version): |
| 125 |
"""Version comparison class that implements Semantic Versioning 2.0.0 |
| 126 |
|
| 127 |
Based off of ``distutils.version.Version`` |
| 128 |
""" |
| 129 |
|
| 130 |
version_re = SEMVER_RE |
| 131 |
|
| 132 |
def __init__(self, vstring=None): |
| 133 |
self.vstring = vstring |
| 134 |
self.major = None |
| 135 |
self.minor = None |
| 136 |
self.patch = None |
| 137 |
self.prerelease = () |
| 138 |
self.buildmetadata = () |
| 139 |
|
| 140 |
if vstring: |
| 141 |
self.parse(vstring) |
| 142 |
|
| 143 |
def __repr__(self): |
| 144 |
return 'SemanticVersion(%r)' % self.vstring |
| 145 |
|
| 146 |
@staticmethod |
| 147 |
def from_loose_version(loose_version): |
| 148 |
"""This method is designed to take a ``LooseVersion`` |
| 149 |
and attempt to construct a ``SemanticVersion`` from it |
| 150 |
|
| 151 |
This is useful where you want to do simple version math |
| 152 |
without requiring users to provide a compliant semver. |
| 153 |
""" |
| 154 |
if not isinstance(loose_version, LooseVersion): |
| 155 |
raise ValueError("%r is not a LooseVersion" % loose_version) |
| 156 |
|
| 157 |
try: |
| 158 |
version = loose_version.version[:] |
| 159 |
except AttributeError: |
| 160 |
raise ValueError("%r is not a LooseVersion" % loose_version) |
| 161 |
|
| 162 |
extra_idx = 3 |
| 163 |
for marker in ('-', '+'): |
| 164 |
try: |
| 165 |
idx = version.index(marker) |
| 166 |
except ValueError: |
| 167 |
continue |
| 168 |
else: |
| 169 |
if idx < extra_idx: |
| 170 |
extra_idx = idx |
| 171 |
version = version[:extra_idx] |
| 172 |
|
| 173 |
if version and set(type(v) for v in version) != set((int,)): |
| 174 |
raise ValueError("Non integer values in %r" % loose_version) |
| 175 |
|
| 176 |
# Extra is everything to the right of the core version |
| 177 |
extra = re.search('[+-].+$', loose_version.vstring) |
| 178 |
|
| 179 |
version = version + [0] * (3 - len(version)) |
| 180 |
return SemanticVersion( |
| 181 |
'%s%s' % ( |
| 182 |
'.'.join(str(v) for v in version), |
| 183 |
extra.group(0) if extra else '' |
| 184 |
) |
| 185 |
) |
| 186 |
|
| 187 |
def parse(self, vstring): |
| 188 |
match = SEMVER_RE.match(vstring) |
| 189 |
if not match: |
| 190 |
raise ValueError("invalid semantic version '%s'" % vstring) |
| 191 |
|
| 192 |
(major, minor, patch, prerelease, buildmetadata) = match.group(1, 2, 3, 4, 5) |
| 193 |
self.vstring = vstring |
| 194 |
self.major = int(major) |
| 195 |
self.minor = int(minor) |
| 196 |
self.patch = int(patch) |
| 197 |
|
| 198 |
if prerelease: |
| 199 |
self.prerelease = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in prerelease.split('.')) |
| 200 |
if buildmetadata: |
| 201 |
self.buildmetadata = tuple(_Numeric(x) if x.isdigit() else _Alpha(x) for x in buildmetadata.split('.')) |
| 202 |
|
| 203 |
@property |
| 204 |
def core(self): |
| 205 |
return self.major, self.minor, self.patch |
| 206 |
|
| 207 |
@property |
| 208 |
def is_prerelease(self): |
| 209 |
return bool(self.prerelease) |
| 210 |
|
| 211 |
@property |
| 212 |
def is_stable(self): |
| 213 |
# Major version zero (0.y.z) is for initial development. Anything MAY change at any time. |
| 214 |
# The public API SHOULD NOT be considered stable. |
| 215 |
# https://semver.org/#spec-item-4 |
| 216 |
return not (self.major == 0 or self.is_prerelease) |
| 217 |
|
| 218 |
def _cmp(self, other): |
| 219 |
if isinstance(other, str): |
| 220 |
other = SemanticVersion(other) |
| 221 |
|
| 222 |
if self.core != other.core: |
| 223 |
# if the core version doesn't match |
| 224 |
# prerelease and buildmetadata doesn't matter |
| 225 |
if self.core < other.core: |
| 226 |
return -1 |
| 227 |
else: |
| 228 |
return 1 |
| 229 |
|
| 230 |
if not any((self.prerelease, other.prerelease)): |
| 231 |
return 0 |
| 232 |
|
| 233 |
if self.prerelease and not other.prerelease: |
| 234 |
return -1 |
| 235 |
elif not self.prerelease and other.prerelease: |
| 236 |
return 1 |
| 237 |
else: |
| 238 |
if self.prerelease < other.prerelease: |
| 239 |
return -1 |
| 240 |
elif self.prerelease > other.prerelease: |
| 241 |
return 1 |
| 242 |
|
| 243 |
# Build metadata MUST be ignored when determining version precedence |
| 244 |
# https://semver.org/#spec-item-10 |
| 245 |
# With the above in mind it is ignored here |
| 246 |
|
| 247 |
# If we have made it here, things should be equal |
| 248 |
return 0 |
| 249 |
|
| 250 |
# The Py2 and Py3 implementations of distutils.version.Version |
| 251 |
# are quite different, this makes the Py2 and Py3 implementations |
| 252 |
# the same |
| 253 |
def __eq__(self, other): |
| 254 |
return self._cmp(other) == 0 |
| 255 |
|
| 256 |
def __ne__(self, other): |
| 257 |
return not self.__eq__(other) |
| 258 |
|
| 259 |
def __lt__(self, other): |
| 260 |
return self._cmp(other) < 0 |
| 261 |
|
| 262 |
def __le__(self, other): |
| 263 |
return self._cmp(other) <= 0 |
| 264 |
|
| 265 |
def __gt__(self, other): |
| 266 |
return self._cmp(other) > 0 |
| 267 |
|
| 268 |
def __ge__(self, other): |
| 269 |
return self._cmp(other) >= 0 |