From 2d912fb78f4ced48a51c693b1f81646c622d35bd Mon Sep 17 00:00:00 2001 From: "wu.chunyang" Date: Wed, 4 Sep 2024 09:40:05 +0800 Subject: [PATCH] use stevedore to load extensions pkg_resources was deprecated in python3.12 Change-Id: Ifa4c93220d3f39f706cc7fd462f28ebcdce14707 --- requirements.txt | 1 + troveclient/auth_plugin.py | 19 ++++++++----------- troveclient/shell.py | 12 ++++++------ troveclient/tests/test_discover.py | 21 --------------------- 4 files changed, 15 insertions(+), 38 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9459683a..69f35bdb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ python-swiftclient>=3.2.0 # Apache-2.0 python-mistralclient!=3.2.0,>=3.1.0 # Apache-2.0 osc-lib>=1.8.0 # Apache-2.0 python-openstackclient>=3.12.0 # Apache-2.0 +stevedore>=2.0.1 # Apache-2.0 diff --git a/troveclient/auth_plugin.py b/troveclient/auth_plugin.py index ab0d2d82..0c6cdc5e 100644 --- a/troveclient/auth_plugin.py +++ b/troveclient/auth_plugin.py @@ -17,7 +17,7 @@ import logging -import pkg_resources +import stevedore from troveclient import exceptions @@ -33,15 +33,12 @@ def discover_auth_systems(): This won't take into account the old style auth-systems. """ - ep_name = 'openstack.client.auth_plugin' - for ep in pkg_resources.iter_entry_points(ep_name): - try: - auth_plugin = ep.load() - except (ImportError, pkg_resources.UnknownExtra, AttributeError) as e: - LOG.debug("ERROR: Cannot load auth plugin %s", ep.name) - LOG.debug(e, exc_info=1) - else: - _discovered_plugins[ep.name] = auth_plugin + global _discovered_plugins + mgr = stevedore.ExtensionManager( + namespace='openstack.client.auth_plugin', + invoke_on_load=True, + ) + _discovered_plugins = {ext.name: ext.obj for ext in mgr} def load_auth_system_opts(parser): @@ -60,7 +57,7 @@ def load_auth_system_opts(parser): def load_plugin(auth_system): if auth_system in _discovered_plugins: - return _discovered_plugins[auth_system]() + return _discovered_plugins[auth_system] raise exceptions.AuthSystemNotFound(auth_system) diff --git a/troveclient/shell.py b/troveclient/shell.py index ef3aa43f..e4fbe0b5 100644 --- a/troveclient/shell.py +++ b/troveclient/shell.py @@ -33,7 +33,8 @@ from keystoneauth1.identity.generic import token from keystoneauth1 import loading from oslo_utils import encodeutils from oslo_utils import importutils -import pkg_resources +import stevedore + from troveclient.apiclient import exceptions as exc import troveclient.auth_plugin @@ -323,11 +324,10 @@ class OpenStackTroveShell(object): yield name, module def _discover_via_entry_points(self): - for ep in pkg_resources.iter_entry_points('troveclient.extension'): - name = ep.name - module = ep.load() - - yield name, module + mgr = stevedore.ExtensionManager(namespace='troveclient.extension', + invoke_on_load=True) + for ext in mgr: + yield ext.name, ext.plugin def _add_bash_completion_subparser(self, subparsers): subparser = subparsers.add_parser( diff --git a/troveclient/tests/test_discover.py b/troveclient/tests/test_discover.py index f94084d6..72a8a5bf 100644 --- a/troveclient/tests/test_discover.py +++ b/troveclient/tests/test_discover.py @@ -17,7 +17,6 @@ import inspect import types from unittest import mock -import pkg_resources import testtools import troveclient.shell @@ -25,26 +24,6 @@ import troveclient.shell class DiscoverTest(testtools.TestCase): - def test_discover_via_entry_points(self): - - def mock_iter_entry_points(group): - if group == 'troveclient.extension': - fake_ep = mock.Mock() - fake_ep.name = 'foo' - fake_ep.module = types.ModuleType('foo') - fake_ep.load.return_value = fake_ep.module - return [fake_ep] - - @mock.patch.object(pkg_resources, 'iter_entry_points', - mock_iter_entry_points) - def test(): - shell = troveclient.shell.OpenStackTroveShell() - for name, module in shell._discover_via_entry_points(): - self.assertEqual('foo', name) - self.assertTrue(inspect.ismodule(module)) - - test() - def test_discover_extensions(self): def mock_discover_via_python_path(self):