use stevedore to load extensions

pkg_resources was deprecated in python3.12

Change-Id: Ifa4c93220d3f39f706cc7fd462f28ebcdce14707
This commit is contained in:
wu.chunyang 2024-09-04 09:40:05 +08:00
parent c7380db1bf
commit 2d912fb78f
4 changed files with 15 additions and 38 deletions

View File

@ -11,3 +11,4 @@ python-swiftclient>=3.2.0 # Apache-2.0
python-mistralclient!=3.2.0,>=3.1.0 # Apache-2.0 python-mistralclient!=3.2.0,>=3.1.0 # Apache-2.0
osc-lib>=1.8.0 # Apache-2.0 osc-lib>=1.8.0 # Apache-2.0
python-openstackclient>=3.12.0 # Apache-2.0 python-openstackclient>=3.12.0 # Apache-2.0
stevedore>=2.0.1 # Apache-2.0

View File

@ -17,7 +17,7 @@
import logging import logging
import pkg_resources import stevedore
from troveclient import exceptions from troveclient import exceptions
@ -33,15 +33,12 @@ def discover_auth_systems():
This won't take into account the old style auth-systems. This won't take into account the old style auth-systems.
""" """
ep_name = 'openstack.client.auth_plugin' global _discovered_plugins
for ep in pkg_resources.iter_entry_points(ep_name): mgr = stevedore.ExtensionManager(
try: namespace='openstack.client.auth_plugin',
auth_plugin = ep.load() invoke_on_load=True,
except (ImportError, pkg_resources.UnknownExtra, AttributeError) as e: )
LOG.debug("ERROR: Cannot load auth plugin %s", ep.name) _discovered_plugins = {ext.name: ext.obj for ext in mgr}
LOG.debug(e, exc_info=1)
else:
_discovered_plugins[ep.name] = auth_plugin
def load_auth_system_opts(parser): def load_auth_system_opts(parser):
@ -60,7 +57,7 @@ def load_auth_system_opts(parser):
def load_plugin(auth_system): def load_plugin(auth_system):
if auth_system in _discovered_plugins: if auth_system in _discovered_plugins:
return _discovered_plugins[auth_system]() return _discovered_plugins[auth_system]
raise exceptions.AuthSystemNotFound(auth_system) raise exceptions.AuthSystemNotFound(auth_system)

View File

@ -33,7 +33,8 @@ from keystoneauth1.identity.generic import token
from keystoneauth1 import loading from keystoneauth1 import loading
from oslo_utils import encodeutils from oslo_utils import encodeutils
from oslo_utils import importutils from oslo_utils import importutils
import pkg_resources import stevedore
from troveclient.apiclient import exceptions as exc from troveclient.apiclient import exceptions as exc
import troveclient.auth_plugin import troveclient.auth_plugin
@ -323,11 +324,10 @@ class OpenStackTroveShell(object):
yield name, module yield name, module
def _discover_via_entry_points(self): def _discover_via_entry_points(self):
for ep in pkg_resources.iter_entry_points('troveclient.extension'): mgr = stevedore.ExtensionManager(namespace='troveclient.extension',
name = ep.name invoke_on_load=True)
module = ep.load() for ext in mgr:
yield ext.name, ext.plugin
yield name, module
def _add_bash_completion_subparser(self, subparsers): def _add_bash_completion_subparser(self, subparsers):
subparser = subparsers.add_parser( subparser = subparsers.add_parser(

View File

@ -17,7 +17,6 @@ import inspect
import types import types
from unittest import mock from unittest import mock
import pkg_resources
import testtools import testtools
import troveclient.shell import troveclient.shell
@ -25,26 +24,6 @@ import troveclient.shell
class DiscoverTest(testtools.TestCase): 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 test_discover_extensions(self):
def mock_discover_via_python_path(self): def mock_discover_via_python_path(self):