Skip to content

Commit

Permalink
migrate pkg_resources.resource_string usages to `importlib.resource…
Browse files Browse the repository at this point in the history
…s` (#22031)
  • Loading branch information
tdyas authored Mar 2, 2025
1 parent a5b2e7d commit 3821ccf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from __future__ import annotations

import importlib.resources
import json
import logging
import os.path
from dataclasses import dataclass

import pkg_resources

from pants.backend.java.dependency_inference.types import JavaSourceDependencyAnalysis
from pants.core.goals.resolves import ExportableTool
from pants.core.util_rules.source_files import SourceFiles
Expand Down Expand Up @@ -142,7 +141,8 @@ async def analyze_java_source_dependencies(


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


# TODO(13879): Consolidate compilation of wrapper binaries to common rules.
Expand Down
14 changes: 7 additions & 7 deletions src/python/pants/jvm/jar_tool/jar_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from __future__ import annotations

import importlib.resources
import os
from collections.abc import Iterable, Mapping
from dataclasses import dataclass
from enum import Enum, unique

import pkg_resources

from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
from pants.core.goals.resolves import ExportableTool
from pants.engine.fs import (
Expand Down Expand Up @@ -211,17 +210,18 @@ async def run_jar_tool(


def _load_jar_tool_sources() -> list[FileContent]:
parent_module = ".".join(__name__.split(".")[:-1])
result = []
for package in _JAR_TOOL_SRC_PACKAGES:
# pkg_path = package.replace(".", os.path.sep)
# relative_folder = os.path.join("src", pkg_path)
for basename in pkg_resources.resource_listdir(__name__, package):
for resource in importlib.resources.files(parent_module).joinpath(package).iterdir():
if not resource.is_file():
continue
result.append(
FileContent(
path=os.path.join(package, basename),
content=pkg_resources.resource_string(
__name__, os.path.join(package, basename)
),
path=os.path.join(package, resource.name),
content=resource.read_bytes(),
)
)
return result
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/jvm/strip_jar/strip_jar.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import importlib.resources
from dataclasses import dataclass

import pkg_resources

from pants.core.goals.resolves import ExportableTool
from pants.engine.fs import AddPrefix, CreateDigest, Digest, Directory, FileContent
from pants.engine.internals.native_engine import MergeDigests, RemovePrefix
Expand Down Expand Up @@ -97,7 +96,8 @@ async def strip_jar(


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


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

0 comments on commit 3821ccf

Please sign in to comment.