Skip to content

Commit

Permalink
Fall back from distutils -> packaging.version (bugfix) (#1203)
Browse files Browse the repository at this point in the history
* Remove dependency on distutils -> packaging.version

* Test the changes

Minor: black formatting

* Fixed return value

* NOQA flake8 builtins

* two spaces whatever

* No fall back, only new thing
  • Loading branch information
Hook25 authored Apr 23, 2024
1 parent 6273897 commit b056c5b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
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())

0 comments on commit b056c5b

Please sign in to comment.