From eb99c222138c49a360f384cebcacb6eeffe84ecd Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Thu, 12 May 2022 23:50:07 +0000 Subject: [PATCH] Remove "distutils" library Library "distutils" will be marked as deprecated in Python 3.10: https://peps.python.org/pep-0386/ This patch does the following replacements, that provide the same functionality and API: - distutils.version.StrictVersion -> packaging.version.Version - distutils.spawn.find_executable -> shutil.which Closes-Bug: #1973780 Change-Id: Iad96ad3e7055f71c629efbe80070adbe297cd7aa --- neutron/cmd/sanity/checks.py | 8 ++++---- neutron/tests/common/helpers.py | 7 +++---- neutron/tests/fullstack/resources/machine.py | 4 ++-- neutron/tests/fullstack/resources/process.py | 12 ++++++------ neutron/tests/functional/resources/process.py | 14 +++++++------- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/neutron/cmd/sanity/checks.py b/neutron/cmd/sanity/checks.py index 8bbf00703e2..819c9b4dcf3 100644 --- a/neutron/cmd/sanity/checks.py +++ b/neutron/cmd/sanity/checks.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import distutils import enum import re import shutil @@ -244,11 +243,12 @@ def dnsmasq_version_supported(): env = {'LC_ALL': 'C'} out = agent_utils.execute(cmd, addl_env=env) m = re.search(r"version (\d+\.\d+)", out) - ver = distutils.version.StrictVersion(m.group(1) if m else '0.0') - if ver < distutils.version.StrictVersion(MINIMUM_DNSMASQ_VERSION): + ver = versionutils.convert_version_to_tuple(m.group(1) if m else '0.0') + if ver < versionutils.convert_version_to_tuple( + MINIMUM_DNSMASQ_VERSION): return False if (cfg.CONF.dnsmasq_enable_addr6_list is True and - ver < distutils.version.StrictVersion( + ver < versionutils.convert_version_to_tuple( DNSMASQ_VERSION_HOST_ADDR6_LIST)): LOG.warning('Support for multiple IPv6 addresses in host ' 'entries was introduced in dnsmasq version ' diff --git a/neutron/tests/common/helpers.py b/neutron/tests/common/helpers.py index a156f935b7a..30130c97e8b 100644 --- a/neutron/tests/common/helpers.py +++ b/neutron/tests/common/helpers.py @@ -13,7 +13,6 @@ # under the License. import datetime -from distutils import version import functools import math import os @@ -24,6 +23,7 @@ from neutron_lib.agent import topics from neutron_lib import constants from neutron_lib import context from oslo_utils import timeutils +from packaging import version import neutron from neutron.agent.common import ovs_lib @@ -223,9 +223,8 @@ def skip_if_ovs_older_than(ovs_version): @functools.wraps(f) def check_ovs_and_skip(test): ovs = ovs_lib.BaseOVS() - current_ovs_version = version.StrictVersion( - ovs.config['ovs_version']) - if current_ovs_version < version.StrictVersion(ovs_version): + current_ovs_version = version.Version(ovs.config['ovs_version']) + if current_ovs_version < version.Version(ovs_version): test.skipTest("This test requires OVS version %s or higher." % ovs_version) return f(test) diff --git a/neutron/tests/fullstack/resources/machine.py b/neutron/tests/fullstack/resources/machine.py index b9ccef079ea..81fbba2781d 100644 --- a/neutron/tests/fullstack/resources/machine.py +++ b/neutron/tests/fullstack/resources/machine.py @@ -12,8 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. -from distutils import spawn import itertools +import shutil import netaddr from oslo_log import log as logging @@ -59,7 +59,7 @@ class FakeFullstackMachinesList(list): class FakeFullstackMachine(machine_fixtures.FakeMachineBase): NO_RESOLV_CONF_DHCLIENT_SCRIPT_PATH = ( - spawn.find_executable(FULLSTACK_DHCLIENT_SCRIPT)) + shutil.which(FULLSTACK_DHCLIENT_SCRIPT)) def __init__(self, host, network_id, tenant_id, safe_client, neutron_port=None, bridge_name=None, use_dhcp=False, diff --git a/neutron/tests/fullstack/resources/process.py b/neutron/tests/fullstack/resources/process.py index 647934a9e4b..a062bc5671d 100644 --- a/neutron/tests/fullstack/resources/process.py +++ b/neutron/tests/fullstack/resources/process.py @@ -13,9 +13,9 @@ # under the License. import datetime -from distutils import spawn import os import re +import shutil import signal import fixtures @@ -64,7 +64,7 @@ class ProcessFixture(fixtures.Fixture): run_as_root = bool(self.namespace) exec_name = (self.exec_name if run_as_root - else spawn.find_executable(self.exec_name)) + else shutil.which(self.exec_name)) cmd = [exec_name, '--log-dir', log_dir, '--log-file', log_file] for filename in self.config_filenames: cmd += ['--config-file', filename] @@ -206,7 +206,7 @@ class OVSAgentFixture(ServiceFixture): self.process_fixture = self.useFixture(ProcessFixture( test_name=self.test_name, process_name=constants.AGENT_PROCESS_OVS, - exec_name=spawn.find_executable( + exec_name=shutil.which( 'ovs_agent.py', path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)), config_filenames=config_filenames, @@ -227,7 +227,7 @@ class PlacementFixture(fixtures.Fixture): self.process_fixture = self.useFixture(ProcessFixture( test_name=self.test_name, process_name='placement', - exec_name=spawn.find_executable( + exec_name=shutil.which( 'placement.py', path=os.path.join(fullstack_base.ROOTDIR, 'servers') ), @@ -313,7 +313,7 @@ class L3AgentFixture(ServiceFixture): if self.namespace: exec_name = 'l3_agent.py' else: - exec_name = spawn.find_executable( + exec_name = shutil.which( 'l3_agent.py', path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)) @@ -354,7 +354,7 @@ class DhcpAgentFixture(fixtures.Fixture): if self.namespace: exec_name = 'dhcp_agent.py' else: - exec_name = spawn.find_executable( + exec_name = shutil.which( 'dhcp_agent.py', path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)) diff --git a/neutron/tests/functional/resources/process.py b/neutron/tests/functional/resources/process.py index eada6970f6b..3201d0d90e8 100644 --- a/neutron/tests/functional/resources/process.py +++ b/neutron/tests/functional/resources/process.py @@ -13,8 +13,8 @@ # under the License. -from distutils import spawn import os +import shutil import fixtures import psutil @@ -64,7 +64,7 @@ class OvnNorthd(DaemonProcessFixture): def start(self): # start the ovn-northd ovn_northd_cmd = [ - spawn.find_executable('ovn-northd'), '-vconsole:off', + shutil.which('ovn-northd'), '-vconsole:off', '--detach', '--ovnnb-db=%s' % self.ovn_nb_db, '--ovnsb-db=%s' % self.ovn_sb_db, @@ -141,11 +141,11 @@ class OvsdbServer(DaemonProcessFixture): def _init_ovsdb_pki(self): os.chdir(self.temp_dir) - pki_init_cmd = [spawn.find_executable('ovs-pki'), 'init', + pki_init_cmd = [shutil.which('ovs-pki'), 'init', '-d', self.temp_dir, '-l', os.path.join(self.temp_dir, 'pki.log'), '--force'] utils.execute(pki_init_cmd) - pki_req_sign = [spawn.find_executable('ovs-pki'), 'req+sign', 'ovn', + pki_req_sign = [shutil.which('ovs-pki'), 'req+sign', 'ovn', 'controller', '-d', self.temp_dir, '-l', os.path.join(self.temp_dir, 'pki.log'), '--force'] utils.execute(pki_req_sign) @@ -161,14 +161,14 @@ class OvsdbServer(DaemonProcessFixture): pki_done = False for ovsdb_process in self.ovsdb_server_processes: # create the db from the schema using ovsdb-tool - ovsdb_tool_cmd = [spawn.find_executable('ovsdb-tool'), + ovsdb_tool_cmd = [shutil.which('ovsdb-tool'), 'create', ovsdb_process['db_path'], ovsdb_process['schema_path']] utils.execute(ovsdb_tool_cmd) # start the ovsdb-server ovsdb_server_cmd = [ - spawn.find_executable('ovsdb-server'), '-vconsole:off', + shutil.which('ovsdb-server'), '-vconsole:off', '--detach', '--pidfile=%s' % os.path.join( self.temp_dir, ovsdb_process['pidfile']), @@ -190,7 +190,7 @@ class OvsdbServer(DaemonProcessFixture): obj, _ = utils.create_process(ovsdb_server_cmd) obj.communicate() - conn_cmd = [spawn.find_executable(ovsdb_process['ctl_cmd']), + conn_cmd = [shutil.which(ovsdb_process['ctl_cmd']), '--db=unix:%s' % ovsdb_process['remote_path'], 'set-connection', 'p%s:%s:%s' % (ovsdb_process['protocol'],