From cc2f96240823c93ebe3ff238b81e857b99b21fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 13 Oct 2023 15:11:52 +0200 Subject: [PATCH 1/3] Expect ImportError instead of SyntaxError when importing pkg_resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Getting SyntaxError from pkg_resources is unlikely. I hereby declare that you can do whatever you want with this commit: Signed-off-by: Miro Hrončok --- sos/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sos/utilities.py b/sos/utilities.py index 69e1c4e565..10c4f6ffa3 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -24,7 +24,7 @@ try: from pkg_resources import parse_version as version_parse -except SyntaxError: +except ImportError: from packaging.version import parse as version_parse # try loading magic>=0.4.20 which implements detect_from_filename method From c1155d65b826c2bff845255556230fd8cf31123c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 13 Oct 2023 15:12:57 +0200 Subject: [PATCH 2/3] Instead of a parse_version -> version_parse function, use parse_version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This seems a bit more straightforward and achieves the same result. This also defines the API of sos.utilities to avoid flake8 "imported but unused" I hereby declare that you can do whatever you want with this commit: Signed-off-by: Miro Hrončok --- sos/utilities.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/sos/utilities.py b/sos/utilities.py index 10c4f6ffa3..03a4a7b6e6 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -23,9 +23,9 @@ from collections import deque try: - from pkg_resources import parse_version as version_parse + from pkg_resources import parse_version except ImportError: - from packaging.version import parse as version_parse + from packaging.version import parse as parse_version # try loading magic>=0.4.20 which implements detect_from_filename method magic_mod = False @@ -46,6 +46,32 @@ TIMEOUT_DEFAULT = 300 +__all__ = [ + 'TIMEOUT_DEFAULT', + 'ImporterHelper', + 'SoSTimeoutError', + 'TempFileUtil', + 'bold', + 'file_is_binary', + 'fileobj', + 'find', + 'get_human_readable', + 'grep', + 'import_module', + 'is_executable', + 'listdir', + 'parse_version', + 'path_exists', + 'path_isdir', + 'path_isfile', + 'path_islink', + 'path_join', + 'recursive_dict_values_by_key', + 'shell_out', + 'sos_get_command_output', + 'tail', +] + def tail(filename, number_of_bytes): """Returns the last number_of_bytes of filename""" @@ -413,12 +439,6 @@ def recursive_dict_values_by_key(dobj, keys=[]): return [d for d in _items if d not in _filt] -def parse_version(version): - """Parse the version string - """ - return version_parse(version) - - class FakeReader(): """Used as a replacement AsyncReader for when we are writing directly to disk, and allows us to keep more simplified flows for executing, From a6802bcff32b96cc1a432a0a0507dd2ef8de31b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 13 Oct 2023 16:09:30 +0200 Subject: [PATCH 3/3] Prefer packaging.version over pkg_resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pkg_resources bundle and call packaging.version internally anyway. In RHEL 10 and Ubuntu, we have packaging in BaseOS/main. I hereby declare that you can do whatever you want with this commit: Signed-off-by: Miro Hrončok --- sos/utilities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sos/utilities.py b/sos/utilities.py index 03a4a7b6e6..d56afe28cf 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -23,9 +23,9 @@ from collections import deque try: - from pkg_resources import parse_version -except ImportError: from packaging.version import parse as parse_version +except ImportError: + from pkg_resources import parse_version # try loading magic>=0.4.20 which implements detect_from_filename method magic_mod = False