diff options
author | Stephen Finucane <sfinucan@redhat.com> | 2022-05-18 12:22:26 +0100 |
---|---|---|
committer | Stephen Finucane <sfinucan@redhat.com> | 2022-07-15 16:08:26 +0100 |
commit | 86061ad87d2b60a6cc330fae9fc22249ce5916aa (patch) | |
tree | 12b2d60b0d4388e0021a472e8812649bd307b9cd | |
parent | 3e0eed49c5c7e38c906e6c31b948d273e3fa91be (diff) | |
download | cliff-86061ad87d2b60a6cc330fae9fc22249ce5916aa.tar.gz |
Remove final use of pkg_resources
'pkg_resources' is slow, while 'importlib.metadata' is the new shiny and
is *much* faster. Recent version of 'importlib.metadata' - namely those
found in Python 3.10 or provided by the 4.4 'importlib-metadata'
backport - now provide the last bit of functionality we were missing to
remove 'pkg_resources' entirely, namely the ability to map package names
to modules. This is used for generating epilogs.
The benefits of this are huge, yielding a near 40% decrease in runtime
for the cliffdemo app (100mS after compared to 160mS) before.
Change-Id: I934d8a196d76622671781643f36bdb8a07d2f319
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
-rw-r--r-- | cliff/command.py | 24 | ||||
-rw-r--r-- | requirements.txt | 2 |
2 files changed, 9 insertions, 17 deletions
diff --git a/cliff/command.py b/cliff/command.py index 0a02525..f8e38ad 100644 --- a/cliff/command.py +++ b/cliff/command.py @@ -13,6 +13,7 @@ import abc import inspect +import importlib_metadata from stevedore import extension from cliff import _argparse @@ -27,26 +28,15 @@ def _get_distributions_by_modules(): distribution name (the name used with pip and PyPI) do not always match. We want to report which distribution caused the command to be installed, so we need to look up the values. - """ - import pkg_resources global _dists_by_mods if _dists_by_mods is None: - results = {} - for dist in pkg_resources.working_set: - try: - mod_names = dist.get_metadata('top_level.txt').strip() - except Exception: - # Could not retrieve metadata. Either the file is not - # present or we cannot read it. Ignore the - # distribution. - pass - else: - # Distributions may include multiple top-level - # packages (see setuptools for an example). - for mod_name in mod_names.splitlines(): - results[mod_name] = dist.project_name - _dists_by_mods = results + # There can be multiple distribution in the case of namespace packages + # so we'll just grab the first one + _dists_by_mods = { + k: v[0] for k, v in + importlib_metadata.packages_distributions().items() + } return _dists_by_mods diff --git a/requirements.txt b/requirements.txt index 1399132..79ed14f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ autopage>=0.4.0 # Apache 2.0 +# TODO: Drop this when Python 3.10 is our minimum supported version +importlib_metadata>=4.4 # Apache-2.0 cmd2>=1.0.0 # MIT PrettyTable>=0.7.2 # BSD stevedore>=2.0.1 # Apache-2.0 |