This is an automated email from the ASF dual-hosted git repository. akitouni pushed a commit to branch abderrahim/no-pkg-resources in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 20da7c0f16526e34d36fd88451d5f6c303545203 Author: Abderrahim Kitouni <[email protected]> AuthorDate: Sun Aug 11 21:32:35 2024 +0100 pluginoriginpip: use importlib instead of pkg_resources --- src/buildstream/_pluginfactory/pluginoriginpip.py | 54 +++++++++++------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/buildstream/_pluginfactory/pluginoriginpip.py b/src/buildstream/_pluginfactory/pluginoriginpip.py index 3bded89ab..a633b3ab2 100644 --- a/src/buildstream/_pluginfactory/pluginoriginpip.py +++ b/src/buildstream/_pluginfactory/pluginoriginpip.py @@ -32,7 +32,8 @@ class PluginOriginPip(PluginOrigin): def get_plugin_paths(self, kind, plugin_type): - import pkg_resources + from packaging.requirements import Requirement, InvalidRequirement + from importlib.metadata import distribution, PackageNotFoundError try: # For setuptools >= 70 @@ -53,38 +54,40 @@ class PluginOriginPip(PluginOrigin): else: assert False, "unreachable" - # key by a tuple to avoid collision try: - package = pkg_resources.get_entry_info(self._package_name, entrypoint_group, kind) - except pkg_resources.DistributionNotFound as e: + package = Requirement(self._package_name) + except InvalidRequirement as e: + raise PluginError( + "{}: Malformed package-name '{}' encountered: {}".format( + self.provenance_node.get_provenance(), self._package_name, e + ), + reason="package-malformed-requirement", + ) from e + + try: + dist = distribution(package.name) + except PackageNotFoundError as e: raise PluginError( "{}: Failed to load {} plugin '{}': {}".format( self.provenance_node.get_provenance(), plugin_type, kind, e ), reason="package-not-found", ) from e - except pkg_resources.VersionConflict as e: + + if dist.version not in package.specifier: raise PluginError( "{}: Version conflict encountered while loading {} plugin '{}'".format( self.provenance_node.get_provenance(), plugin_type, kind ), - detail=e.report(), + detail="{} {} is installed but {} is required".format(dist.name, dist.version, package), reason="package-version-conflict", ) from e - except ( - # For setuptools < 49.0.0 - pkg_resources.RequirementParseError, - # For setuptools >= 49.0.0 - packaging.requirements.InvalidRequirement, - ) as e: - raise PluginError( - "{}: Malformed package-name '{}' encountered: {}".format( - self.provenance_node.get_provenance(), self._package_name, e - ), - reason="package-malformed-requirement", - ) from e - if package is None: + entrypoints = dist.entry_points.select(name=kind, group=entrypoint_group) + + if len(entrypoints) == 1: + entrypoint = entrypoints[0] + else: raise PluginError( "{}: Pip package {} does not contain a {} plugin named '{}'".format( self.provenance_node.get_provenance(), self._package_name, plugin_type, kind @@ -92,17 +95,10 @@ class PluginOriginPip(PluginOrigin): reason="plugin-not-found", ) - location = package.dist.get_resource_filename( - pkg_resources._manager, package.module_name.replace(".", os.sep) + ".py" - ) + location = dist.locate_file(package.module_name.replace(".", os.sep) + ".py") + defaults = dist.locate_file(package.module_name.replace(".", os.sep) + ".yaml") - # Also load the defaults - required since setuptools - # may need to extract the file. - try: - defaults = package.dist.get_resource_filename( - pkg_resources._manager, package.module_name.replace(".", os.sep) + ".yaml" - ) - except KeyError: + if not defaults.exists(): # The plugin didn't have an accompanying YAML file defaults = None
