From 704d0cfc2c9d87d3d0264d8961766bfb0a148c03 Mon Sep 17 00:00:00 2001 From: Hook25 Date: Mon, 22 Apr 2024 15:56:31 +0200 Subject: [PATCH 1/6] Remove dependency on distutils -> packaging.version --- providers/base/bin/wifi_nmcli_backup.py | 12 +++++++----- providers/base/bin/wifi_nmcli_test.py | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index a585ac37cd..ee3abf8074 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -12,7 +12,11 @@ import subprocess as sp import sys -from distutils.version import LooseVersion +try: + from distutils.version import LooseVersion as version_parser +except ModuleNotFoundError: + from packaging import version + version_parser = version.parse NM_CON_DIR = "/etc/NetworkManager/system-connections" SAVE_DIR = os.path.join( @@ -25,12 +29,10 @@ def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = LooseVersion(output.strip().split()[-1].decode()) + version = version_parser(output.strip().split()[-1].decode()) # check if using an earlier nmcli version with different api # nmcli in cosmic is 1.12.4, bionic is 1.10 - if version < LooseVersion("1.12.0"): - return True - return False + return version < version_parser("1.12.0") # Creation of keyfile names can be found in: diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index 3adb6f3a16..77a96b07a6 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -17,8 +17,12 @@ import sys import time -from distutils.version import LooseVersion from gateway_ping_test import ping +try: + from distutils.version import LooseVersion as version_parser +except ModuleNotFoundError: + from packaging import version + version_parser = version.parse print = functools.partial(print, flush=True) @@ -26,12 +30,10 @@ def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = LooseVersion(output.strip().split()[-1].decode()) + version = version_parser(output.strip().split()[-1].decode()) # check if using the 16.04 nmcli because of this bug # https://bugs.launchpad.net/plano/+bug/1896806 - if version < LooseVersion("1.9.9"): - return True - return False + return version < version_parser("1.9.9") def print_head(txt): From c44a8963cdc294b80832fc54e3f9f21c20d96b4a Mon Sep 17 00:00:00 2001 From: Hook25 Date: Tue, 23 Apr 2024 11:39:12 +0200 Subject: [PATCH 2/6] Test the changes Minor: black formatting --- providers/base/bin/wifi_nmcli_backup.py | 1 + providers/base/bin/wifi_nmcli_test.py | 2 + .../base/tests/test_wifi_nmcli_backup.py | 38 +++++++++++++++++++ providers/base/tests/test_wifi_nmcli_test.py | 38 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 providers/base/tests/test_wifi_nmcli_backup.py create mode 100644 providers/base/tests/test_wifi_nmcli_test.py diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index ee3abf8074..c69d71b104 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -16,6 +16,7 @@ from distutils.version import LooseVersion as version_parser except ModuleNotFoundError: from packaging import version + version_parser = version.parse NM_CON_DIR = "/etc/NetworkManager/system-connections" diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index 77a96b07a6..c5441e10b0 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -18,10 +18,12 @@ import time from gateway_ping_test import ping + try: from distutils.version import LooseVersion as version_parser except ModuleNotFoundError: from packaging import version + version_parser = version.parse print = functools.partial(print, flush=True) diff --git a/providers/base/tests/test_wifi_nmcli_backup.py b/providers/base/tests/test_wifi_nmcli_backup.py new file mode 100644 index 0000000000..25a7545ba6 --- /dev/null +++ b/providers/base/tests/test_wifi_nmcli_backup.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# Written by: +# Massimiliano Girardi +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import unittest +from unittest.mock import patch + +from wifi_nmcli_backup import legacy_nmcli + + +class WifiNmcliBackupTests(unittest.TestCase): + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_true(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + "nmcli tool, version 1.11.3-5" + ) + self.assertTrue(legacy_nmcli()) + + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_false(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + "nmcli tool, version 1.46.0-2" + ) + self.assertFalse(legacy_nmcli()) diff --git a/providers/base/tests/test_wifi_nmcli_test.py b/providers/base/tests/test_wifi_nmcli_test.py new file mode 100644 index 0000000000..8812e8090e --- /dev/null +++ b/providers/base/tests/test_wifi_nmcli_test.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# Written by: +# Massimiliano Girardi +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import unittest +from unittest.mock import patch + +from wifi_nmcli_test import legacy_nmcli + + +class WifiNmcliBackupTests(unittest.TestCase): + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_true(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + "nmcli tool, version 1.9.8-5" + ) + self.assertTrue(legacy_nmcli()) + + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_false(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + "nmcli tool, version 1.46.0-2" + ) + self.assertFalse(legacy_nmcli()) From 7e4257f2dda3452b26c8112aea6be6211c74f891 Mon Sep 17 00:00:00 2001 From: Hook25 Date: Tue, 23 Apr 2024 11:43:33 +0200 Subject: [PATCH 3/6] Fixed return value --- providers/base/tests/test_wifi_nmcli_backup.py | 4 ++-- providers/base/tests/test_wifi_nmcli_test.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/providers/base/tests/test_wifi_nmcli_backup.py b/providers/base/tests/test_wifi_nmcli_backup.py index 25a7545ba6..9cb65cfd9a 100644 --- a/providers/base/tests/test_wifi_nmcli_backup.py +++ b/providers/base/tests/test_wifi_nmcli_backup.py @@ -26,13 +26,13 @@ class WifiNmcliBackupTests(unittest.TestCase): @patch("wifi_nmcli_backup.sp") def test_legacy_nmcli_true(self, subprocess_mock): subprocess_mock.check_output.return_value = ( - "nmcli tool, version 1.11.3-5" + b"nmcli tool, version 1.11.3-5" ) self.assertTrue(legacy_nmcli()) @patch("wifi_nmcli_backup.sp") def test_legacy_nmcli_false(self, subprocess_mock): subprocess_mock.check_output.return_value = ( - "nmcli tool, version 1.46.0-2" + b"nmcli tool, version 1.46.0-2" ) self.assertFalse(legacy_nmcli()) diff --git a/providers/base/tests/test_wifi_nmcli_test.py b/providers/base/tests/test_wifi_nmcli_test.py index 8812e8090e..e0b52a3925 100644 --- a/providers/base/tests/test_wifi_nmcli_test.py +++ b/providers/base/tests/test_wifi_nmcli_test.py @@ -23,16 +23,16 @@ class WifiNmcliBackupTests(unittest.TestCase): - @patch("wifi_nmcli_backup.sp") + @patch("wifi_nmcli_test.sp") def test_legacy_nmcli_true(self, subprocess_mock): subprocess_mock.check_output.return_value = ( - "nmcli tool, version 1.9.8-5" + b"nmcli tool, version 1.9.8-5" ) self.assertTrue(legacy_nmcli()) - @patch("wifi_nmcli_backup.sp") + @patch("wifi_nmcli_test.sp") def test_legacy_nmcli_false(self, subprocess_mock): subprocess_mock.check_output.return_value = ( - "nmcli tool, version 1.46.0-2" + b"nmcli tool, version 1.46.0-2" ) self.assertFalse(legacy_nmcli()) From 29c6c2fb0e94f68aa0091df299a1bf425622e1e8 Mon Sep 17 00:00:00 2001 From: Hook25 Date: Tue, 23 Apr 2024 12:10:55 +0200 Subject: [PATCH 4/6] NOQA flake8 builtins --- providers/base/bin/wifi_nmcli_backup.py | 2 +- providers/base/bin/wifi_nmcli_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index c69d71b104..cc577248a1 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -14,7 +14,7 @@ try: from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: +except ModuleNotFoundError: # noqa: F821 from packaging import version version_parser = version.parse diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index c5441e10b0..6e525a3b60 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -21,7 +21,7 @@ try: from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: +except ModuleNotFoundError: # noqa: F821 from packaging import version version_parser = version.parse From cb9dbd5e1c536455f65e702dc976c2f3efe02f31 Mon Sep 17 00:00:00 2001 From: Hook25 Date: Tue, 23 Apr 2024 12:15:35 +0200 Subject: [PATCH 5/6] two spaces whatever --- providers/base/bin/wifi_nmcli_backup.py | 2 +- providers/base/bin/wifi_nmcli_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index cc577248a1..188925361e 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -14,7 +14,7 @@ try: from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: # noqa: F821 +except ModuleNotFoundError: # noqa: F821 from packaging import version version_parser = version.parse diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index 6e525a3b60..5e62191381 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -21,7 +21,7 @@ try: from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: # noqa: F821 +except ModuleNotFoundError: # noqa: F821 from packaging import version version_parser = version.parse From e9293f696b10fc632702e31d3f7b791edfb8afba Mon Sep 17 00:00:00 2001 From: Hook25 Date: Tue, 23 Apr 2024 15:29:01 +0200 Subject: [PATCH 6/6] No fall back, only new thing --- providers/base/bin/wifi_nmcli_backup.py | 10 +++------- providers/base/bin/wifi_nmcli_test.py | 12 ++++-------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index 188925361e..e2c2dcf02b 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -12,12 +12,8 @@ import subprocess as sp import sys -try: - from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: # noqa: F821 - from packaging import version +from packaging import version as version_parser - version_parser = version.parse NM_CON_DIR = "/etc/NetworkManager/system-connections" SAVE_DIR = os.path.join( @@ -30,10 +26,10 @@ def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = version_parser(output.strip().split()[-1].decode()) + version = version_parser.parse(output.strip().split()[-1].decode()) # check if using an earlier nmcli version with different api # nmcli in cosmic is 1.12.4, bionic is 1.10 - return version < version_parser("1.12.0") + return version < version_parser.parse("1.12.0") # Creation of keyfile names can be found in: diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index 5e62191381..0f735c5a97 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -17,14 +17,10 @@ import sys import time -from gateway_ping_test import ping +from packaging import version as version_parser -try: - from distutils.version import LooseVersion as version_parser -except ModuleNotFoundError: # noqa: F821 - from packaging import version +from gateway_ping_test import ping - version_parser = version.parse print = functools.partial(print, flush=True) @@ -32,10 +28,10 @@ def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = version_parser(output.strip().split()[-1].decode()) + version = version_parser.parse(output.strip().split()[-1].decode()) # check if using the 16.04 nmcli because of this bug # https://bugs.launchpad.net/plano/+bug/1896806 - return version < version_parser("1.9.9") + return version < version_parser.parse("1.9.9") def print_head(txt):