From a5db32d800256240b9f8d6a3b644ab4c1c1674ea Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Thu, 3 Oct 2024 02:14:23 +0900 Subject: [PATCH] Get rid of pkg_resources ... because it was removed in Python 3.12 [1]. [1] https://docs.python.org/3/whatsnew/3.12.html#ensurepip Change-Id: I7bcbaade79f3d3662487b801263f446f4d30d88f --- keystonemiddleware/_common/config.py | 6 +++--- .../tests/unit/auth_token/test_auth_token_middleware.py | 8 ++++---- keystonemiddleware/tests/unit/auth_token/test_config.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/keystonemiddleware/_common/config.py b/keystonemiddleware/_common/config.py index 6194df82..bd903f75 100644 --- a/keystonemiddleware/_common/config.py +++ b/keystonemiddleware/_common/config.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import pkg_resources +import importlib.metadata from oslo_config import cfg from oslo_log import log as logging @@ -145,8 +145,8 @@ class Config(object): if project: try: - version = pkg_resources.get_distribution(project).version - except pkg_resources.DistributionNotFound: + version = importlib.metadata.version(project) + except importlib.metadata.PackageNotFoundError: version = "unknown" project = "%s/%s " % (project, version) diff --git a/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py b/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py index 06d33384..26258ec8 100644 --- a/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py +++ b/keystonemiddleware/tests/unit/auth_token/test_auth_token_middleware.py @@ -1997,8 +1997,8 @@ class TestAuthPluginUserAgentGeneration(BaseAuthTokenMiddlewareTest): self._assert_user_agent(app, project, ksm_version) def _create_app(self, conf, project_version, ksm_version): - fake_pkg_resources = mock.Mock() - fake_pkg_resources.get_distribution().version = project_version + fake_im = mock.Mock() + fake_im.return_value = project_version fake_version_info = mock.Mock() fake_version_info.version_string.return_value = ksm_version @@ -2007,10 +2007,10 @@ class TestAuthPluginUserAgentGeneration(BaseAuthTokenMiddlewareTest): body = uuid.uuid4().hex + at_im = 'keystonemiddleware._common.config.importlib.metadata.version' at_pbr = 'keystonemiddleware._common.config.pbr.version' - with mock.patch('keystonemiddleware._common.config.pkg_resources', - new=fake_pkg_resources): + with mock.patch(at_im, new=fake_im): with mock.patch(at_pbr, new=fake_pbr_version): return self.create_simple_middleware(body=body, conf=conf) diff --git a/keystonemiddleware/tests/unit/auth_token/test_config.py b/keystonemiddleware/tests/unit/auth_token/test_config.py index 6bc7c854..ad26ad1e 100644 --- a/keystonemiddleware/tests/unit/auth_token/test_config.py +++ b/keystonemiddleware/tests/unit/auth_token/test_config.py @@ -75,12 +75,12 @@ class TestAuthPluginLocalOsloConfig(base.BaseAuthTokenTestCase): if not project_version: project_version = uuid.uuid4().hex - fake_pkg_resources = mock.Mock() - fake_pkg_resources.get_distribution().version = project_version + fake_im = mock.Mock() + fake_im.return_value = project_version body = uuid.uuid4().hex - with mock.patch('keystonemiddleware._common.config.pkg_resources', - new=fake_pkg_resources): + at_im = 'keystonemiddleware._common.config.importlib.metadata.version' + with mock.patch(at_im, new=fake_im): # use_global_conf is poorly named. What it means is # don't use the config created in test setUp. return self.create_simple_middleware(body=body, conf=conf,