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 <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2020-07-04 14:26:31 -04:00
parent 661a0eb4b5
commit 77e2f67d38
3 changed files with 17 additions and 5 deletions

View File

@ -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

View File

@ -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)

View File

@ -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