From aa88db9448c7f840ff619de989ff6f4564f47aae Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 24 Jul 2024 16:20:21 +0100 Subject: [PATCH] Add callback on plugin load failure I have seen a few cases where import errors (distutils - I am looking at you) result in an extension not being available, but there is no indication why this is the case. We do configure logging, but this happens too late (as part of the 'cliff.app.App.run' call to execute a command, which calls osc-lib's 'configure_logging' but which happens long after we've tried to import our plugins) to be of any use. Instead, make use of a callback to make it more obvious. Change-Id: Id68b06161e445b79fe43f463e06cda3c4771ef02 Signed-off-by: Stephen Finucane --- openstackclient/common/clientmanager.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/openstackclient/common/clientmanager.py b/openstackclient/common/clientmanager.py index 8caf94a5d9..6ace1f1703 100644 --- a/openstackclient/common/clientmanager.py +++ b/openstackclient/common/clientmanager.py @@ -158,10 +158,22 @@ class ClientManager(clientmanager.ClientManager): # Plugin Support +def _on_load_failure_callback( + manager: stevedore.ExtensionManager, + ep: importlib.metadata.EntryPoint, + err: Exception, +) -> None: + sys.stderr.write( + f"WARNING: Failed to import plugin {ep.group}:{ep.name}: {err}.\n" + ) + + def get_plugin_modules(group): """Find plugin entry points""" mod_list = [] - mgr = stevedore.ExtensionManager(group) + mgr = stevedore.ExtensionManager( + group, on_load_failure_callback=_on_load_failure_callback + ) for ep in mgr: LOG.debug('Found plugin %s', ep.name) @@ -180,9 +192,8 @@ def get_plugin_modules(group): module = importlib.import_module(module_name) except Exception as err: sys.stderr.write( - "WARNING: Failed to import plugin {}: {}.\n".format( - ep.name, err - ) + f"WARNING: Failed to import plugin {ep.group}:{ep.name}: " + f"{err}.\n" ) continue