drop usage of pkg_resource for newer python compat

pkg_resources comes from setuptools and not the python stdlib. Starting
with python 3.12 and newer, setuptools won't automatically be installed
into virtualenvs so the lack of a direct dependency will result in an
error. Further more setuptools has deprecated pkg_resources and intends
to remove it so adding a dependency would just be a band aid. The python
stdlib has added a replacement for how sushy uses pkg_resources starting
with python 3.9 and newer. Since sushy supports python 3.8 and newer we
must support using the backport package.

Depends-on: https://review.opendev.org/c/openstack/requirements/+/925306
Change-Id: I83719e368f8f7d39963c29c0013f512d1883a8dc
This commit is contained in:
Doug Goldstein
2024-07-30 16:36:13 -05:00
parent 7ccea0a4a0
commit 9246f0ad69
3 changed files with 30 additions and 18 deletions

View File

@@ -10,3 +10,4 @@ pbr!=2.1.0,>=2.0.0 # Apache-2.0
requests>=2.14.2 # Apache-2.0
python-dateutil>=2.7.0 # BSD
stevedore>=1.29.0 # Apache-2.0
importlib_resources>=1.3; python_version<'3.9' # Apache-2.0

View File

@@ -16,7 +16,14 @@ import collections
import logging
import os
import pkg_resources
try:
from importlib import resources
if not hasattr(resources, "files"):
import importlib_resources as resources
except ImportError:
import importlib_resources as resources
import requests
from sushy import auth as sushy_auth
@@ -41,7 +48,7 @@ from sushy import utils
LOG = logging.getLogger(__name__)
STANDARD_REGISTRY_PATH = 'standard_registries/'
STANDARD_REGISTRY_PATH = 'standard_registries'
class ProtocolFeaturesSupportedField(base.CompositeField):
@@ -568,18 +575,17 @@ class Sushy(base.ResourceBase):
:returns: list of MessageRegistry
"""
message_registries = []
resource_package_name = __name__
for json_file in pkg_resources.resource_listdir(
resource_package_name, STANDARD_REGISTRY_PATH):
# Not using path.join according to pkg_resources docs
mes_reg = message_registry.MessageRegistry(
None, STANDARD_REGISTRY_PATH + json_file,
reader=base.JsonPackagedFileReader(
resource_package_name))
message_registries.append(mes_reg)
return message_registries
return [
message_registry.MessageRegistry(
None,
os.path.join(STANDARD_REGISTRY_PATH, json_file.name),
reader=base.JsonPackagedFileReader(__package__),
)
for json_file in resources.files(__package__)
.joinpath(STANDARD_REGISTRY_PATH)
.iterdir()
if json_file.is_file()
]
@property
@utils.cache_it

View File

@@ -22,7 +22,12 @@ import json
import logging
import zipfile
import pkg_resources
try:
from importlib import resources
if not hasattr(resources, 'files'):
import importlib_resources as resources
except ImportError:
import importlib_resources as resources
from sushy import exceptions
from sushy.resources import constants
@@ -486,9 +491,9 @@ class JsonPackagedFileReader(AbstractDataReader):
def get_data(self):
"""Gets JSON file from packaged file denoted by path"""
with pkg_resources.resource_stream(self._resource_package_name,
self._path) as resource:
json_data = json.loads(resource.read().decode(encoding='utf-8'))
ref = resources.files(self._resource_package_name).joinpath(self._path)
with ref.open(encoding='utf-8') as fp:
json_data = json.load(fp)
return FieldData(None, None, json_data)