Skip to content

Commit afd625a

Browse files
authored
migrate from pkg_resources to importlib.metadata in help extractor (#22045)
Migrate from `pkg_resources` entry points API to `importlib.metadata` in help extractor.
1 parent 1208403 commit afd625a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/python/pants/help/help_info_extracter.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ast
77
import dataclasses
88
import difflib
9+
import importlib.metadata
910
import inspect
1011
import itertools
1112
import json
@@ -19,8 +20,6 @@
1920
from pathlib import Path
2021
from typing import Any, DefaultDict, TypeVar, Union, cast, get_type_hints
2122

22-
import pkg_resources
23-
2423
import pants.backend
2524
from pants.base import deprecated
2625
from pants.build_graph.build_configuration import BuildConfiguration
@@ -905,20 +904,29 @@ def discover_source_backends(root: Path, is_source_root: bool) -> set[Discovered
905904
}
906905
return backends
907906

908-
def discover_plugin_backends(entry_point_name: str) -> set[DiscoveredBackend]:
909-
backends = {
907+
def discover_plugin_backends(entry_point_name: str) -> frozenset[DiscoveredBackend]:
908+
def get_dist_file(dist: importlib.metadata.Distribution, file_path: str):
909+
try:
910+
return dist.locate_file(file_path)
911+
except Exception as e:
912+
raise ValueError(
913+
f"Failed to locate file `{file_path}` in distribution `{dist}`: {e}"
914+
)
915+
916+
backends = frozenset(
910917
DiscoveredBackend(
911-
entry_point.dist.project_name,
912-
entry_point.module_name,
918+
entry_point.dist.name,
919+
entry_point.module,
913920
str(
914-
Path(entry_point.dist.location)
915-
/ (entry_point.module_name.replace(".", "/") + ".py")
921+
get_dist_file(
922+
entry_point.dist, entry_point.module.replace(".", "/") + ".py"
923+
)
916924
),
917925
True,
918926
)
919-
for entry_point in pkg_resources.iter_entry_points(entry_point_name)
927+
for entry_point in importlib.metadata.entry_points().select(group=entry_point_name)
920928
if entry_point.dist is not None
921-
}
929+
)
922930
return backends
923931

924932
global_options = options.for_global_scope()

0 commit comments

Comments
 (0)