Skip to content

Commit

Permalink
migrate from pkg_resources to importlib.metadata in help extractor (
Browse files Browse the repository at this point in the history
#22045)

Migrate from `pkg_resources` entry points API to `importlib.metadata` in
help extractor.
  • Loading branch information
tdyas authored Mar 5, 2025
1 parent 1208403 commit afd625a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/python/pants/help/help_info_extracter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ast
import dataclasses
import difflib
import importlib.metadata
import inspect
import itertools
import json
Expand All @@ -19,8 +20,6 @@
from pathlib import Path
from typing import Any, DefaultDict, TypeVar, Union, cast, get_type_hints

import pkg_resources

import pants.backend
from pants.base import deprecated
from pants.build_graph.build_configuration import BuildConfiguration
Expand Down Expand Up @@ -905,20 +904,29 @@ def discover_source_backends(root: Path, is_source_root: bool) -> set[Discovered
}
return backends

def discover_plugin_backends(entry_point_name: str) -> set[DiscoveredBackend]:
backends = {
def discover_plugin_backends(entry_point_name: str) -> frozenset[DiscoveredBackend]:
def get_dist_file(dist: importlib.metadata.Distribution, file_path: str):
try:
return dist.locate_file(file_path)
except Exception as e:
raise ValueError(
f"Failed to locate file `{file_path}` in distribution `{dist}`: {e}"
)

backends = frozenset(
DiscoveredBackend(
entry_point.dist.project_name,
entry_point.module_name,
entry_point.dist.name,
entry_point.module,
str(
Path(entry_point.dist.location)
/ (entry_point.module_name.replace(".", "/") + ".py")
get_dist_file(
entry_point.dist, entry_point.module.replace(".", "/") + ".py"
)
),
True,
)
for entry_point in pkg_resources.iter_entry_points(entry_point_name)
for entry_point in importlib.metadata.entry_points().select(group=entry_point_name)
if entry_point.dist is not None
}
)
return backends

global_options = options.for_global_scope()
Expand Down

0 comments on commit afd625a

Please sign in to comment.