diff options
author | David Negreira <david.negreira@canonical.com> | 2024-03-02 17:05:12 +0100 |
---|---|---|
committer | Jake Hunsaker <jacob.r.hunsaker@gmail.com> | 2024-03-04 11:14:47 -0500 |
commit | 7e81246fcfd35e17a69bc3d40400cd123e8ca7bf (patch) | |
tree | 1e920f6ddc52cf60967e2c9b7ab7f0f58fe1add0 | |
parent | 5b0c986af87ad3a9f35196a3de73acda8249cb64 (diff) | |
download | sos-7e81246fcfd35e17a69bc3d40400cd123e8ca7bf.tar.gz |
[collect] refactor _format_version
Ensure that we format the package versions to the pep440 standard.
This is necessary as we are using `parse_version()` from the `packaging`
package to verify that the sos collector node version is equal or bigger
than the versions we are collecting from the nodes. If we pass the wrong
format to `parse_version()`, we are unable to do the comparison and
unable to run `sos report` on those nodes.
This addresses the issue of the Ubuntu packaging version naming with `+`
and `~` signals as well as generic versioning schemes.
Resolves: #3544
Signed-off-by: David Negreira <david.negreira@canonical.com>
-rw-r--r-- | sos/collector/sosnode.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/sos/collector/sosnode.py b/sos/collector/sosnode.py index 878d3b3a..07a71865 100644 --- a/sos/collector/sosnode.py +++ b/sos/collector/sosnode.py @@ -412,25 +412,29 @@ class SosNode(): :returns: True if installed version is at least ``ver``, else False :rtype: ``bool`` """ - def _format_version(ver): - # format the version we're checking to a standard form of X.Y.Z-R + def _format_version_to_pep440(ver): + """ Convert the version into a PEP440 compliant version scheme.""" + public_version_re = re.compile( + r"^([0-9][0-9.]*(?:(?:a|b|rc|.post|.dev)[0-9]+)*)\+?" + ) try: - _fver = ver.split('-')[0] - _rel = '' - if '-' in ver: - _rel = '-' + ver.split('-')[-1].split('.')[0] - if len(_fver.split('.')) == 2: - _fver += '.0' - - return _fver + _rel + _, public, local = public_version_re.split(ver, maxsplit=1) + if not local: + return ver + sanitized_local = re.sub("[+~]+", ".", local).strip("-") + pep440_version = f"{public}+{sanitized_local}" + return pep440_version except Exception as err: - self.log_debug("Unable to format '%s': %s" % (ver, err)) + self.log_debug(f"Unable to format {ver} to pep440 format: " + f"{err}") return ver - _ver = _format_version(ver) + _ver = _format_version_to_pep440(ver) + _node_formatted_version = _format_version_to_pep440( + self.sos_info['version']) try: - _node_ver = parse_version(self.sos_info['version']) + _node_ver = parse_version(_node_formatted_version) _test_ver = parse_version(_ver) return _node_ver >= _test_ver except Exception as err: |