From 80ee2e342d1854f439a1ec25c2f6a3a3625a0720 Mon Sep 17 00:00:00 2001 From: Don Penney Date: Sun, 22 Dec 2019 22:45:18 -0500 Subject: [PATCH] Remove use of rpmUtils.miscutils from cgcs-patch The rpmUtils.miscutils.stringToVersion function will not be available in CentOS8, as it is not currently provided for python3. A similar function exists in cgcs_patch.patch_functions, using regex to parse the version from an RPM filename. This update adds a new function, expand_pkgver, implemented in a similar fashion using regex, providing the same capability as rpmUtils.miscutils.stringToVersion. Change-Id: I2a04f3dbf85d62c87ca1afcf988b672aafceb642 Story: 2006228 Task: 37871 Signed-off-by: Don Penney --- cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py | 11 +++++------ cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py | 6 +++--- cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py | 18 ++++++++++++++++++ .../cgcs-patch/cgcs_patch/tests/test_patch_agent.py | 2 -- .../cgcs_patch/tests/test_patch_controller.py | 2 -- .../cgcs-patch/cgcs_patch/tests/test_patch_utils.py | 14 ++++++++++++++ 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py index b95b79d..77930d7 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py @@ -19,9 +19,8 @@ import sys import yaml import shutil -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error - from cgcs_patch.patch_functions import configure_logging +from cgcs_patch.patch_functions import parse_pkgver from cgcs_patch.patch_functions import LOG import cgcs_patch.config as cfg from cgcs_patch.base import PatchService @@ -519,8 +518,8 @@ class PatchAgent(PatchService): # 1, if first arg is higher version # 0, if versions are same # -1, if first arg is lower version - rc = rpm.labelCompare(stringToVersion(version), - stringToVersion(stored_ver)) + rc = rpm.labelCompare(parse_pkgver(version), + parse_pkgver(stored_ver)) if rc > 0: # Update version @@ -709,8 +708,8 @@ class PatchAgent(PatchService): compare_version = base_version # Compare the installed version to what's in the repo - rc = rpm.labelCompare(stringToVersion(installed_version), - stringToVersion(compare_version)) + rc = rpm.labelCompare(parse_pkgver(installed_version), + parse_pkgver(compare_version)) if rc == 0: # Versions match, nothing to do. continue diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py index 1ba8f5e..4b94a5f 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py @@ -17,7 +17,7 @@ import rpm import os import gc -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error +from cgcs_patch.patch_functions import parse_pkgver from wsgiref import simple_server from cgcs_patch.api import app @@ -776,8 +776,8 @@ class PatchController(PatchService): # Ignore epoch patch_ver = patch_ver.split(':')[1] - rc = rpm.labelCompare(stringToVersion(installed_ver), - stringToVersion(patch_ver)) + rc = rpm.labelCompare(parse_pkgver(installed_ver), + parse_pkgver(patch_ver)) if self.patch_data.metadata[patch_id]["repostate"] == constants.AVAILABLE: # The RPM is not expected to be installed. diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py index 832e4e9..281a286 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py @@ -176,6 +176,24 @@ def parse_rpm_filename(filename): return (pkgname, arch, PackageVersion(epoch, version, release)) +def parse_pkgver(pkgver): + # Version format is: + # [:]- + # + pattern = re.compile(r'((([^:]):)?)([^-]+)((-(.*))?)$') + + m = pattern.match(pkgver) + + if m is None: + raise ValueError("Package version does not match expected format: %s" % pkgver) + + epoch = m.group(3) + version = m.group(4) + release = m.group(7) + + return (epoch, version, release) + + class PackageVersion(object): """ The PackageVersion class provides a structure for RPM version information, diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py index c953e4f..bd1eef9 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py @@ -10,8 +10,6 @@ import sys import testtools sys.modules['rpm'] = mock.Mock() -sys.modules['rpmUtils'] = mock.Mock() -sys.modules['rpmUtils.miscutils'] = mock.Mock() import cgcs_patch.patch_agent # noqa: E402 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py index d11623f..e2b02c0 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py @@ -10,8 +10,6 @@ import sys import testtools sys.modules['rpm'] = mock.Mock() -sys.modules['rpmUtils'] = mock.Mock() -sys.modules['rpmUtils.miscutils'] = mock.Mock() import cgcs_patch.patch_controller # noqa: E402 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py index a5eb8d4..653c65a 100644 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py @@ -9,6 +9,7 @@ import socket import testtools import cgcs_patch.constants +import cgcs_patch.patch_functions import cgcs_patch.utils @@ -130,3 +131,16 @@ class CgcsPatchUtilsTestCase(testtools.TestCase): result = cgcs_patch.utils.ip_to_versioned_localhost(ip) self.assertEqual(expected_result, result) + + def test_parse_pkgver(self): + versions = { + '0:1.2.3-r4': ('0', '1.2.3', 'r4'), + '4.3.2-1': (None, '4.3.2', '1'), + '8.1.4': (None, '8.1.4', None), + '5:7.5.3': ('5', '7.5.3', None), + 'This is a weird version string': (None, 'This is a weird version string', None), + } + + for ver, expected in versions.items(): + result = cgcs_patch.patch_functions.parse_pkgver(ver) + self.assertEqual(result, expected)