Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back from distutils -> packaging.version (bugfix) #1203

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions providers/base/bin/wifi_nmcli_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import subprocess as sp
import sys

from distutils.version import LooseVersion
from packaging import version as version_parser


NM_CON_DIR = "/etc/NetworkManager/system-connections"
SAVE_DIR = os.path.join(
Expand All @@ -25,12 +26,10 @@
def legacy_nmcli():
cmd = "nmcli -v"
output = sp.check_output(cmd, shell=True)
version = LooseVersion(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
if version < LooseVersion("1.12.0"):
return True
return False
return version < version_parser.parse("1.12.0")


# Creation of keyfile names can be found in:
Expand Down
10 changes: 5 additions & 5 deletions providers/base/bin/wifi_nmcli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
import sys
import time

from distutils.version import LooseVersion
from packaging import version as version_parser

from gateway_ping_test import ping


print = functools.partial(print, flush=True)


def legacy_nmcli():
cmd = "nmcli -v"
output = sp.check_output(cmd, shell=True)
version = LooseVersion(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
if version < LooseVersion("1.9.9"):
return True
return False
return version < version_parser.parse("1.9.9")


def print_head(txt):
Expand Down
38 changes: 38 additions & 0 deletions providers/base/tests/test_wifi_nmcli_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# Written by:
# Massimiliano Girardi <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.


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 = (
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 = (
b"nmcli tool, version 1.46.0-2"
)
self.assertFalse(legacy_nmcli())
38 changes: 38 additions & 0 deletions providers/base/tests/test_wifi_nmcli_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# Written by:
# Massimiliano Girardi <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.


import unittest
from unittest.mock import patch

from wifi_nmcli_test import legacy_nmcli


class WifiNmcliBackupTests(unittest.TestCase):
@patch("wifi_nmcli_test.sp")
def test_legacy_nmcli_true(self, subprocess_mock):
subprocess_mock.check_output.return_value = (
b"nmcli tool, version 1.9.8-5"
)
self.assertTrue(legacy_nmcli())

@patch("wifi_nmcli_test.sp")
def test_legacy_nmcli_false(self, subprocess_mock):
subprocess_mock.check_output.return_value = (
b"nmcli tool, version 1.46.0-2"
)
self.assertFalse(legacy_nmcli())
Loading