From 2a3922dee7e2d552a2e398a25e6cd217c4f4a2c4 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 24 Jul 2024 16:08:32 +0100 Subject: [PATCH] Remove use of distutils This has been removed in Python 3.12, which is preventing us using osc-placement in those environments. Thankfully we only use it for StrictVersion which we are using to parse microversions. This is actually overkill here since we can always expect a version string like '1.5', thus we do this parsing ourselves rather than drag in a new library like 'microversion_parse'. Change-Id: I2923238f8b30568bceb1799882d4468ff1d7e538 Signed-off-by: Stephen Finucane --- osc_placement/tests/unit/test_version.py | 2 +- osc_placement/version.py | 26 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/osc_placement/tests/unit/test_version.py b/osc_placement/tests/unit/test_version.py index 1fcff43..a40085d 100644 --- a/osc_placement/tests/unit/test_version.py +++ b/osc_placement/tests/unit/test_version.py @@ -70,7 +70,7 @@ class TestVersion(base.BaseTestCase): self.assertRaisesRegex( ValueError, 'Operation or argument is not supported', - version.compare, '3.1.2', version.gt('3.1.3')) + version.compare, '3.1', version.gt('3.2')) def test_check_decorator(self): fake_api = mock.Mock() diff --git a/osc_placement/version.py b/osc_placement/version.py index bcb452b..88be219 100644 --- a/osc_placement/version.py +++ b/osc_placement/version.py @@ -10,8 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. -from distutils.version import StrictVersion +import functools import operator +import re NEGOTIATE_VERSIONS = [ @@ -60,8 +61,29 @@ SUPPORTED_VERSIONS = SUPPORTED_MICROVERSIONS + NEGOTIATE_VERSIONS MAX_VERSION_NO_GAP = '1.29' +@functools.total_ordering +class _Version: + _version_re = re.compile(r'^(\d) \. (\d+)$', re.VERBOSE | re.ASCII) + + def __init__(self, version): + match = self._version_re.match(version) + if not match: + raise ValueError('invalid version number %s' % version) + major, minor = match.group(1, 2) + self.version = (int(major), int(minor)) + + def __str__(self): + return '.'.join(str(v) for v in self.version) + + def __eq__(self, other): + return self.version == other.version + + def __lt__(self, other): + return self.version < other.version + + def _op(func, b, msg): - return lambda a: func(StrictVersion(a), StrictVersion(b)) or msg + return lambda a: func(_Version(a), _Version(b)) or msg def lt(b):