1 From a6912bf7cffaade9647d8921816cc30db85630bb 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>
18 cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py | 11 +++++------
19 cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py | 6 +++---
20 cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py | 18 ++++++++++++++++++
21 .../cgcs-patch/cgcs_patch/tests/test_patch_agent.py | 2 --
22 .../cgcs_patch/tests/test_patch_controller.py | 2 --
23 .../cgcs-patch/cgcs_patch/tests/test_patch_utils.py | 14 ++++++++++++++
24 6 files changed, 40 insertions(+), 13 deletions(-)
26 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
27 index ed6f67e..547db52 100644
28 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
29 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_agent.py
30 @@ -19,9 +19,8 @@ import sys
34 -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error
36 from cgcs_patch.patch_functions import configure_logging
37 +from cgcs_patch.patch_functions import parse_pkgver
38 from cgcs_patch.patch_functions import LOG
39 import cgcs_patch.config as cfg
40 from cgcs_patch.base import PatchService
41 @@ -519,8 +518,8 @@ class PatchAgent(PatchService):
42 # 1, if first arg is higher version
43 # 0, if versions are same
44 # -1, if first arg is lower version
45 - rc = rpm.labelCompare(stringToVersion(version),
46 - stringToVersion(stored_ver))
47 + rc = rpm.labelCompare(parse_pkgver(version),
48 + parse_pkgver(stored_ver))
52 @@ -709,8 +708,8 @@ class PatchAgent(PatchService):
53 compare_version = base_version
55 # Compare the installed version to what's in the repo
56 - rc = rpm.labelCompare(stringToVersion(installed_version),
57 - stringToVersion(compare_version))
58 + rc = rpm.labelCompare(parse_pkgver(installed_version),
59 + parse_pkgver(compare_version))
61 # Versions match, nothing to do.
63 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
64 index 60b2b14..79a6401 100644
65 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
66 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_controller.py
67 @@ -17,7 +17,7 @@ import rpm
71 -from rpmUtils.miscutils import stringToVersion # pylint: disable=import-error
72 +from cgcs_patch.patch_functions import parse_pkgver
74 from wsgiref import simple_server
75 from cgcs_patch.api import app
76 @@ -776,8 +776,8 @@ class PatchController(PatchService):
78 patch_ver = patch_ver.split(':')[1]
80 - rc = rpm.labelCompare(stringToVersion(installed_ver),
81 - stringToVersion(patch_ver))
82 + rc = rpm.labelCompare(parse_pkgver(installed_ver),
83 + parse_pkgver(patch_ver))
85 if self.patch_data.metadata[patch_id]["repostate"] == constants.AVAILABLE:
86 # The RPM is not expected to be installed.
87 diff --git a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
88 index 211c2c9..5866e8b 100644
89 --- a/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
90 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/patch_functions.py
91 @@ -176,6 +176,24 @@ def parse_rpm_filename(filename):
92 return (pkgname, arch, PackageVersion(epoch, version, release))
95 +def parse_pkgver(pkgver):
96 + # Version format is:
97 + # [<epoch>:]<version>-<release>
99 + pattern = re.compile(r'((([^:]):)?)([^-]+)((-(.*))?)$')
101 + m = pattern.match(pkgver)
104 + raise ValueError("Package version does not match expected format: %s" % pkgver)
107 + version = m.group(4)
108 + release = m.group(7)
110 + return (epoch, version, release)
113 class PackageVersion(object):
115 The PackageVersion class provides a structure for RPM version information,
116 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
117 index c953e4f..bd1eef9 100644
118 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py
119 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_agent.py
120 @@ -10,8 +10,6 @@ import sys
123 sys.modules['rpm'] = mock.Mock()
124 -sys.modules['rpmUtils'] = mock.Mock()
125 -sys.modules['rpmUtils.miscutils'] = mock.Mock()
127 import cgcs_patch.patch_agent # noqa: E402
129 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
130 index 1c603b2..1db4b68 100644
131 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py
132 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_controller.py
133 @@ -10,8 +10,6 @@ import sys
136 sys.modules['rpm'] = mock.Mock()
137 -sys.modules['rpmUtils'] = mock.Mock()
138 -sys.modules['rpmUtils.miscutils'] = mock.Mock()
140 import cgcs_patch.patch_controller # noqa: E402
142 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
143 index a5eb8d4..653c65a 100644
144 --- a/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py
145 +++ b/cgcs-patch/cgcs-patch/cgcs_patch/tests/test_patch_utils.py
146 @@ -9,6 +9,7 @@ import socket
149 import cgcs_patch.constants
150 +import cgcs_patch.patch_functions
151 import cgcs_patch.utils
154 @@ -130,3 +131,16 @@ class CgcsPatchUtilsTestCase(testtools.TestCase):
156 result = cgcs_patch.utils.ip_to_versioned_localhost(ip)
157 self.assertEqual(expected_result, result)
159 + def test_parse_pkgver(self):
161 + '0:1.2.3-r4': ('0', '1.2.3', 'r4'),
162 + '4.3.2-1': (None, '4.3.2', '1'),
163 + '8.1.4': (None, '8.1.4', None),
164 + '5:7.5.3': ('5', '7.5.3', None),
165 + 'This is a weird version string': (None, 'This is a weird version string', None),
168 + for ver, expected in versions.items():
169 + result = cgcs_patch.patch_functions.parse_pkgver(ver)
170 + self.assertEqual(result, expected)