1 From 80ee2e342d1854f439a1ec25c2f6a3a3625a0720 Mon Sep 17 00:00:00 2001
2 From: Don Penney <don.penney@windriver.com>
3 Date: Sun, 22 Dec 2019 22:45:18 -0500
4 Subject: [PATCH] Remove use of rpmUtils.miscutils from cgcs-patch
6 The rpmUtils.miscutils.stringToVersion function will not be available
7 in CentOS8, as it is not currently provided for python3. A similar
8 function exists in cgcs_patch.patch_functions, using regex to parse
9 the version from an RPM filename. This update adds a new function,
10 expand_pkgver, implemented in a similar fashion using regex, providing
11 the same capability as rpmUtils.miscutils.stringToVersion.
13 Change-Id: I2a04f3dbf85d62c87ca1afcf988b672aafceb642
16 Signed-off-by: Don Penney <don.penney@windriver.com>
19 cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py | 11 +++++------
20 cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py | 6 +++---
21 cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py | 18 ++++++++++++++++++
22 .../cgcs-patch/cgcs_patch/tests/test_patch_agent.py | 2 --
23 .../cgcs_patch/tests/test_patch_controller.py | 2 --
24 .../cgcs-patch/cgcs_patch/tests/test_patch_utils.py | 14 ++++++++++++++
25 6 files changed, 40 insertions(+), 13 deletions(-)
27 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
28 index b95b79d..77930d7 100644
29 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
30 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
31 @@ -19,9 +19,8 @@ import sys
35 -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error
37 from cgcs_patch.patch_functions import configure_logging
38 +from cgcs_patch.patch_functions import parse_pkgver
39 from cgcs_patch.patch_functions import LOG
40 import cgcs_patch.config as cfg
41 from cgcs_patch.base import PatchService
42 @@ -519,8 +518,8 @@ class PatchAgent(PatchService):
43 # 1, if first arg is higher version
44 # 0, if versions are same
45 # -1, if first arg is lower version
46 - rc = rpm.labelCompare(stringToVersion(version),
47 - stringToVersion(stored_ver))
48 + rc = rpm.labelCompare(parse_pkgver(version),
49 + parse_pkgver(stored_ver))
53 @@ -709,8 +708,8 @@ class PatchAgent(PatchService):
54 compare_version = base_version
56 # Compare the installed version to what's in the repo
57 - rc = rpm.labelCompare(stringToVersion(installed_version),
58 - stringToVersion(compare_version))
59 + rc = rpm.labelCompare(parse_pkgver(installed_version),
60 + parse_pkgver(compare_version))
62 # Versions match, nothing to do.
64 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
65 index 1ba8f5e..4b94a5f 100644
66 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
67 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
68 @@ -17,7 +17,7 @@ import rpm
72 -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error
73 +from cgcs_patch.patch_functions import parse_pkgver
75 from wsgiref import simple_server
76 from cgcs_patch.api import app
77 @@ -776,8 +776,8 @@ class PatchController(PatchService):
79 patch_ver = patch_ver.split(':')[1]
81 - rc = rpm.labelCompare(stringToVersion(installed_ver),
82 - stringToVersion(patch_ver))
83 + rc = rpm.labelCompare(parse_pkgver(installed_ver),
84 + parse_pkgver(patch_ver))
86 if self.patch_data.metadata[patch_id]["repostate"] == constants.AVAILABLE:
87 # The RPM is not expected to be installed.
88 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
89 index 832e4e9..281a286 100644
90 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
91 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
92 @@ -176,6 +176,24 @@ def parse_rpm_filename(filename):
93 return (pkgname, arch, PackageVersion(epoch, version, release))
96 +def parse_pkgver(pkgver):
97 + # Version format is:
98 + # [<epoch>:]<version>-<release>
100 + pattern = re.compile(r'((([^:]):)?)([^-]+)((-(.*))?)$')
102 + m = pattern.match(pkgver)
105 + raise ValueError("Package version does not match expected format: %s" % pkgver)
108 + version = m.group(4)
109 + release = m.group(7)
111 + return (epoch, version, release)
114 class PackageVersion(object):
116 The PackageVersion class provides a structure for RPM version information,
117 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
118 index c953e4f..bd1eef9 100644
119 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py
120 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py
121 @@ -10,8 +10,6 @@ import sys
124 sys.modules['rpm'] = mock.Mock()
125 -sys.modules['rpmUtils'] = mock.Mock()
126 -sys.modules['rpmUtils.miscutils'] = mock.Mock()
128 import cgcs_patch.patch_agent # noqa: E402
130 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
131 index d11623f..e2b02c0 100644
132 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py
133 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py
134 @@ -10,8 +10,6 @@ import sys
137 sys.modules['rpm'] = mock.Mock()
138 -sys.modules['rpmUtils'] = mock.Mock()
139 -sys.modules['rpmUtils.miscutils'] = mock.Mock()
141 import cgcs_patch.patch_controller # noqa: E402
143 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
144 index a5eb8d4..653c65a 100644
145 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py
146 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py
147 @@ -9,6 +9,7 @@ import socket
150 import cgcs_patch.constants
151 +import cgcs_patch.patch_functions
152 import cgcs_patch.utils
155 @@ -130,3 +131,16 @@ class CgcsPatchUtilsTestCase(testtools.TestCase):
157 result = cgcs_patch.utils.ip_to_versioned_localhost(ip)
158 self.assertEqual(expected_result, result)
160 + def test_parse_pkgver(self):
162 + '0:1.2.3-r4': ('0', '1.2.3', 'r4'),
163 + '4.3.2-1': (None, '4.3.2', '1'),
164 + '8.1.4': (None, '8.1.4', None),
165 + '5:7.5.3': ('5', '7.5.3', None),
166 + 'This is a weird version string': (None, 'This is a weird version string', None),
169 + for ver, expected in versions.items():
170 + result = cgcs_patch.patch_functions.parse_pkgver(ver)
171 + self.assertEqual(result, expected)