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
This commit is contained in:
Rodolfo Alonso Hernandez 2022-05-12 23:50:07 +00:00
parent e35188ef3d
commit eb99c22213
5 changed files with 22 additions and 23 deletions

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import distutils
import enum import enum
import re import re
import shutil import shutil
@ -244,11 +243,12 @@ def dnsmasq_version_supported():
env = {'LC_ALL': 'C'} env = {'LC_ALL': 'C'}
out = agent_utils.execute(cmd, addl_env=env) out = agent_utils.execute(cmd, addl_env=env)
m = re.search(r"version (\d+\.\d+)", out) m = re.search(r"version (\d+\.\d+)", out)
ver = distutils.version.StrictVersion(m.group(1) if m else '0.0') ver = versionutils.convert_version_to_tuple(m.group(1) if m else '0.0')
if ver < distutils.version.StrictVersion(MINIMUM_DNSMASQ_VERSION): if ver < versionutils.convert_version_to_tuple(
MINIMUM_DNSMASQ_VERSION):
return False return False
if (cfg.CONF.dnsmasq_enable_addr6_list is True and 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)): DNSMASQ_VERSION_HOST_ADDR6_LIST)):
LOG.warning('Support for multiple IPv6 addresses in host ' LOG.warning('Support for multiple IPv6 addresses in host '
'entries was introduced in dnsmasq version ' 'entries was introduced in dnsmasq version '

View File

@ -13,7 +13,6 @@
# under the License. # under the License.
import datetime import datetime
from distutils import version
import functools import functools
import math import math
import os import os
@ -24,6 +23,7 @@ from neutron_lib.agent import topics
from neutron_lib import constants from neutron_lib import constants
from neutron_lib import context from neutron_lib import context
from oslo_utils import timeutils from oslo_utils import timeutils
from packaging import version
import neutron import neutron
from neutron.agent.common import ovs_lib from neutron.agent.common import ovs_lib
@ -223,9 +223,8 @@ def skip_if_ovs_older_than(ovs_version):
@functools.wraps(f) @functools.wraps(f)
def check_ovs_and_skip(test): def check_ovs_and_skip(test):
ovs = ovs_lib.BaseOVS() ovs = ovs_lib.BaseOVS()
current_ovs_version = version.StrictVersion( current_ovs_version = version.Version(ovs.config['ovs_version'])
ovs.config['ovs_version']) if current_ovs_version < version.Version(ovs_version):
if current_ovs_version < version.StrictVersion(ovs_version):
test.skipTest("This test requires OVS version %s or higher." % test.skipTest("This test requires OVS version %s or higher." %
ovs_version) ovs_version)
return f(test) return f(test)

View File

@ -12,8 +12,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from distutils import spawn
import itertools import itertools
import shutil
import netaddr import netaddr
from oslo_log import log as logging from oslo_log import log as logging
@ -59,7 +59,7 @@ class FakeFullstackMachinesList(list):
class FakeFullstackMachine(machine_fixtures.FakeMachineBase): class FakeFullstackMachine(machine_fixtures.FakeMachineBase):
NO_RESOLV_CONF_DHCLIENT_SCRIPT_PATH = ( 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, def __init__(self, host, network_id, tenant_id, safe_client,
neutron_port=None, bridge_name=None, use_dhcp=False, neutron_port=None, bridge_name=None, use_dhcp=False,

View File

@ -13,9 +13,9 @@
# under the License. # under the License.
import datetime import datetime
from distutils import spawn
import os import os
import re import re
import shutil
import signal import signal
import fixtures import fixtures
@ -64,7 +64,7 @@ class ProcessFixture(fixtures.Fixture):
run_as_root = bool(self.namespace) run_as_root = bool(self.namespace)
exec_name = (self.exec_name exec_name = (self.exec_name
if run_as_root 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] cmd = [exec_name, '--log-dir', log_dir, '--log-file', log_file]
for filename in self.config_filenames: for filename in self.config_filenames:
cmd += ['--config-file', filename] cmd += ['--config-file', filename]
@ -206,7 +206,7 @@ class OVSAgentFixture(ServiceFixture):
self.process_fixture = self.useFixture(ProcessFixture( self.process_fixture = self.useFixture(ProcessFixture(
test_name=self.test_name, test_name=self.test_name,
process_name=constants.AGENT_PROCESS_OVS, process_name=constants.AGENT_PROCESS_OVS,
exec_name=spawn.find_executable( exec_name=shutil.which(
'ovs_agent.py', 'ovs_agent.py',
path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)), path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)),
config_filenames=config_filenames, config_filenames=config_filenames,
@ -227,7 +227,7 @@ class PlacementFixture(fixtures.Fixture):
self.process_fixture = self.useFixture(ProcessFixture( self.process_fixture = self.useFixture(ProcessFixture(
test_name=self.test_name, test_name=self.test_name,
process_name='placement', process_name='placement',
exec_name=spawn.find_executable( exec_name=shutil.which(
'placement.py', path=os.path.join(fullstack_base.ROOTDIR, 'placement.py', path=os.path.join(fullstack_base.ROOTDIR,
'servers') 'servers')
), ),
@ -313,7 +313,7 @@ class L3AgentFixture(ServiceFixture):
if self.namespace: if self.namespace:
exec_name = 'l3_agent.py' exec_name = 'l3_agent.py'
else: else:
exec_name = spawn.find_executable( exec_name = shutil.which(
'l3_agent.py', 'l3_agent.py',
path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)) path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER))
@ -354,7 +354,7 @@ class DhcpAgentFixture(fixtures.Fixture):
if self.namespace: if self.namespace:
exec_name = 'dhcp_agent.py' exec_name = 'dhcp_agent.py'
else: else:
exec_name = spawn.find_executable( exec_name = shutil.which(
'dhcp_agent.py', 'dhcp_agent.py',
path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER)) path=os.path.join(fullstack_base.ROOTDIR, CMD_FOLDER))

View File

@ -13,8 +13,8 @@
# under the License. # under the License.
from distutils import spawn
import os import os
import shutil
import fixtures import fixtures
import psutil import psutil
@ -64,7 +64,7 @@ class OvnNorthd(DaemonProcessFixture):
def start(self): def start(self):
# start the ovn-northd # start the ovn-northd
ovn_northd_cmd = [ ovn_northd_cmd = [
spawn.find_executable('ovn-northd'), '-vconsole:off', shutil.which('ovn-northd'), '-vconsole:off',
'--detach', '--detach',
'--ovnnb-db=%s' % self.ovn_nb_db, '--ovnnb-db=%s' % self.ovn_nb_db,
'--ovnsb-db=%s' % self.ovn_sb_db, '--ovnsb-db=%s' % self.ovn_sb_db,
@ -141,11 +141,11 @@ class OvsdbServer(DaemonProcessFixture):
def _init_ovsdb_pki(self): def _init_ovsdb_pki(self):
os.chdir(self.temp_dir) 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', '-d', self.temp_dir, '-l',
os.path.join(self.temp_dir, 'pki.log'), '--force'] os.path.join(self.temp_dir, 'pki.log'), '--force']
utils.execute(pki_init_cmd) 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', 'controller', '-d', self.temp_dir, '-l',
os.path.join(self.temp_dir, 'pki.log'), '--force'] os.path.join(self.temp_dir, 'pki.log'), '--force']
utils.execute(pki_req_sign) utils.execute(pki_req_sign)
@ -161,14 +161,14 @@ class OvsdbServer(DaemonProcessFixture):
pki_done = False pki_done = False
for ovsdb_process in self.ovsdb_server_processes: for ovsdb_process in self.ovsdb_server_processes:
# create the db from the schema using ovsdb-tool # 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'], 'create', ovsdb_process['db_path'],
ovsdb_process['schema_path']] ovsdb_process['schema_path']]
utils.execute(ovsdb_tool_cmd) utils.execute(ovsdb_tool_cmd)
# start the ovsdb-server # start the ovsdb-server
ovsdb_server_cmd = [ ovsdb_server_cmd = [
spawn.find_executable('ovsdb-server'), '-vconsole:off', shutil.which('ovsdb-server'), '-vconsole:off',
'--detach', '--detach',
'--pidfile=%s' % os.path.join( '--pidfile=%s' % os.path.join(
self.temp_dir, ovsdb_process['pidfile']), self.temp_dir, ovsdb_process['pidfile']),
@ -190,7 +190,7 @@ class OvsdbServer(DaemonProcessFixture):
obj, _ = utils.create_process(ovsdb_server_cmd) obj, _ = utils.create_process(ovsdb_server_cmd)
obj.communicate() 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'], '--db=unix:%s' % ovsdb_process['remote_path'],
'set-connection', 'set-connection',
'p%s:%s:%s' % (ovsdb_process['protocol'], 'p%s:%s:%s' % (ovsdb_process['protocol'],