Skip to content

Commit 3821ccf

Browse files
authored
migrate pkg_resources.resource_string usages to importlib.resources (#22031)
1 parent a5b2e7d commit 3821ccf

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/python/pants/backend/java/dependency_inference/java_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
from __future__ import annotations
55

6+
import importlib.resources
67
import json
78
import logging
89
import os.path
910
from dataclasses import dataclass
1011

11-
import pkg_resources
12-
1312
from pants.backend.java.dependency_inference.types import JavaSourceDependencyAnalysis
1413
from pants.core.goals.resolves import ExportableTool
1514
from pants.core.util_rules.source_files import SourceFiles
@@ -142,7 +141,8 @@ async def analyze_java_source_dependencies(
142141

143142

144143
def _load_javaparser_launcher_source() -> bytes:
145-
return pkg_resources.resource_string(__name__, _LAUNCHER_BASENAME)
144+
parent_module = ".".join(__name__.split(".")[:-1])
145+
return importlib.resources.files(parent_module).joinpath(_LAUNCHER_BASENAME).read_bytes()
146146

147147

148148
# TODO(13879): Consolidate compilation of wrapper binaries to common rules.

src/python/pants/jvm/jar_tool/jar_tool.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
from __future__ import annotations
55

6+
import importlib.resources
67
import os
78
from collections.abc import Iterable, Mapping
89
from dataclasses import dataclass
910
from enum import Enum, unique
1011

11-
import pkg_resources
12-
1312
from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
1413
from pants.core.goals.resolves import ExportableTool
1514
from pants.engine.fs import (
@@ -211,17 +210,18 @@ async def run_jar_tool(
211210

212211

213212
def _load_jar_tool_sources() -> list[FileContent]:
213+
parent_module = ".".join(__name__.split(".")[:-1])
214214
result = []
215215
for package in _JAR_TOOL_SRC_PACKAGES:
216216
# pkg_path = package.replace(".", os.path.sep)
217217
# relative_folder = os.path.join("src", pkg_path)
218-
for basename in pkg_resources.resource_listdir(__name__, package):
218+
for resource in importlib.resources.files(parent_module).joinpath(package).iterdir():
219+
if not resource.is_file():
220+
continue
219221
result.append(
220222
FileContent(
221-
path=os.path.join(package, basename),
222-
content=pkg_resources.resource_string(
223-
__name__, os.path.join(package, basename)
224-
),
223+
path=os.path.join(package, resource.name),
224+
content=resource.read_bytes(),
225225
)
226226
)
227227
return result

src/python/pants/jvm/strip_jar/strip_jar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4+
import importlib.resources
45
from dataclasses import dataclass
56

6-
import pkg_resources
7-
87
from pants.core.goals.resolves import ExportableTool
98
from pants.engine.fs import AddPrefix, CreateDigest, Digest, Directory, FileContent
109
from pants.engine.internals.native_engine import MergeDigests, RemovePrefix
@@ -97,7 +96,8 @@ async def strip_jar(
9796

9897

9998
def _load_strip_jar_source() -> bytes:
100-
return pkg_resources.resource_string(__name__, _STRIP_JAR_BASENAME)
99+
parent_module = ".".join(__name__.split(".")[:-1])
100+
return importlib.resources.files(parent_module).joinpath(_STRIP_JAR_BASENAME).read_bytes()
101101

102102

103103
# TODO(13879): Consolidate compilation of wrapper binaries to common rules.

0 commit comments

Comments
 (0)