From 77e2f67d3856153f875552f84f0a3565f557f2c3 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Sat, 4 Jul 2020 14:26:31 -0400 Subject: [PATCH] switch to importlib.metadata for entrypoint loading Importing pkg_resources scans every installed distribution to find all of the entry points. importlib.metadata can import entry points without scanning all of the installed packages. Change-Id: I9917b10292d191710b37838b3cf099d75139f123 Signed-off-by: Doug Hellmann --- lower-constraints.txt | 1 + openstack/connection.py | 19 ++++++++++++++----- requirements.txt | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lower-constraints.txt b/lower-constraints.txt index f643a2712..b45cc49c5 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -6,6 +6,7 @@ decorator==4.4.1 doc8==0.8.0 dogpile.cache==0.6.5 fixtures==3.0.0 +importlib_metadata==1.7.0 iso8601==0.1.11 jmespath==0.9.0 jsonpatch==1.16 diff --git a/openstack/connection.py b/openstack/connection.py index 3030164cb..7c36b9dbd 100644 --- a/openstack/connection.py +++ b/openstack/connection.py @@ -179,6 +179,13 @@ Additional information about the services can be found in the import warnings import weakref +try: + # For python 3.8 and later + import importlib.metadata as importlib_metadata +except ImportError: + # For everyone else + import importlib_metadata + import concurrent.futures import keystoneauth1.exceptions import requestsexceptions @@ -423,15 +430,17 @@ class Connection( (package_name, function) = vendor_hook.rsplit(':') if package_name and function: - import pkg_resources - ep = pkg_resources.EntryPoint( - 'vendor_hook', package_name, attrs=(function,)) - hook = ep.resolve() + ep = importlib_metadata.EntryPoint( + name='vendor_hook', + value=vendor_hook, + group='vendor_hook', + ) + hook = ep.load() hook(self) except ValueError: self.log.warning('Hook should be in the entrypoint ' 'module:attribute format') - except (ImportError, TypeError) as e: + except (ImportError, TypeError, AttributeError) as e: self.log.warning('Configured hook %s cannot be executed: %s', vendor_hook, e) diff --git a/requirements.txt b/requirements.txt index c443a4544..52e05ec93 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,5 @@ netifaces>=0.10.4 # MIT dogpile.cache>=0.6.5 # BSD cryptography>=2.1 # BSD/Apache-2.0 + +importlib_metadata>=1.7.0;python_version<'3.8' # Apache-2.0