Skip to content

Commit 1d6c176

Browse files
authored
edk2_invocable: Fix pkg_resources deprecation warning (#985)
The following warning is now present (with Python 3.12.10): ``` /opt/hostedtoolcache/Python/3.12.10/x64/lib/python3.12/site-packages edk2toolext/edk2_invocable.py:31: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. import pkg_resources ``` This change replaces it with `importlib`. Signed-off-by: Michael Kubacki <[email protected]>
1 parent 14586dd commit 1d6c176

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

edk2toolext/edk2_invocable.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import argparse
21+
import importlib.metadata
2122
import inspect
2223
import logging
2324
import os
@@ -28,7 +29,6 @@
2829
from textwrap import dedent
2930
from typing import Iterable
3031

31-
import pkg_resources
3232
from edk2toollib.utility_functions import GetHostInfo, RunCmd, import_module_by_file_name, locate_class_in_module
3333

3434
from edk2toolext.base_abstract_invocable import BaseAbstractInvocable
@@ -203,12 +203,21 @@ def collect_python_pip_info(cls: "Edk2Invocable") -> None:
203203
ver_agg = version_aggregator.GetVersionAggregator()
204204
ver_agg.ReportVersion("Python", cur_py, version_aggregator.VersionTypes.TOOL)
205205
# Get a list of all the packages currently installed in pip
206-
pip_packages = [p for p in pkg_resources.working_set]
206+
try:
207+
pip_packages = importlib.metadata.distributions()
208+
except Exception:
209+
pip_packages = []
207210
# go through all installed pip versions
208211
for package in pip_packages:
209-
version = pkg_resources.get_distribution(package).version
210-
logging.info("{0} version: {1}".format(package.project_name, version))
211-
ver_agg.ReportVersion(package.project_name, version, version_aggregator.VersionTypes.PIP)
212+
try:
213+
version = package.version
214+
name = package.metadata["Name"] if "Name" in package.metadata else package.metadata.get("name", None)
215+
if not name:
216+
name = package.metadata.get("Summary", "unknown")
217+
except Exception:
218+
continue
219+
logging.info("{0} version: {1}".format(name, version))
220+
ver_agg.ReportVersion(name, version, version_aggregator.VersionTypes.PIP)
212221

213222
@classmethod
214223
def collect_rust_info(cls: "Edk2Invocable") -> None:

tests.unit/test_nuget_dependency.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
##
99
"""Unit test suite for the GitDependency class."""
1010

11+
import importlib.resources
1112
import logging
1213
import os
1314
import sys
1415
import tempfile
1516
import unittest
1617

17-
import pkg_resources
1818
from edk2toolext.bin import nuget
1919
from edk2toolext.environment import environment_descriptor_files as EDF
2020
from edk2toolext.environment import version_aggregator
@@ -90,11 +90,12 @@ def tearDown(self) -> None:
9090
os.environ[NugetDependency.NUGET_ENV_VAR_NAME] = self.saved_nuget_path
9191

9292
# Fix the nuget.exe is missing....download again
93-
requirement = pkg_resources.Requirement.parse("edk2-pytool-extensions")
94-
nuget_file_path = os.path.join("edk2toolext", "bin", "NuGet.exe")
95-
nuget_path = pkg_resources.resource_filename(requirement, nuget_file_path)
96-
97-
if not os.path.isfile(nuget_path):
93+
try:
94+
with importlib.resources.path("edk2toolext.bin", "NuGet.exe") as nuget_path:
95+
if not os.path.isfile(nuget_path):
96+
nuget.DownloadNuget()
97+
except Exception:
98+
# fallback for older Python or importlib.resources issues
9899
nuget.DownloadNuget()
99100

100101
def test_can_get_nuget_path(self) -> None:

0 commit comments

Comments
 (0)