Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate pkg_resources usage #2008

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import pkg_resources

try:
import importlib.metadata as importlib_metadata
Expand All @@ -25,7 +24,7 @@
except: # pragma: no cover
pass

installed = [d.project_name for d in pkg_resources.working_set]
installed = [d.metadata["Name"] for d in importlib_metadata.distributions()]
check_for = ["ansys-dpf-gatebin", "ansys-dpf-gate", "ansys-grpc-dpf"]
if any([c in installed for c in check_for]):
raise ImportError(f"Error during import of ansys-dpf-core:\n"
Expand Down
43 changes: 28 additions & 15 deletions src/ansys/dpf/gate/load_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os
import subprocess # nosec B404
import sys

import packaging.version
import pkg_resources
import importlib
try:
import importlib.metadata as importlib_metadata
except ImportError: # Python < 3.10 (backport)
import importlib_metadata as importlib_metadata
from ansys.dpf.gate.generated import capi
from ansys.dpf.gate import utils, errors
from ansys.dpf.gate._version import __ansys_version__
Expand Down Expand Up @@ -74,19 +79,27 @@ def _find_latest_ansys_versions():

def _paths_to_dpf_server_library_installs() -> dict:
path_per_version = {}
installed_packages = pkg_resources.working_set
for i in installed_packages:
if "ansys-dpf-server" in i.key:
file_name = pkg_resources.to_filename(i.project_name.replace("ansys-dpf-", ""))
try:
module = importlib.import_module("ansys.dpf." + file_name)
path_per_version[
packaging.version.parse(module.__version__)
] = module.__path__[0]
except ModuleNotFoundError:
pass
except AttributeError:
pass
for d in importlib_metadata.distributions():
distribution_name = d.metadata["Name"]
if "ansys-dpf-server" in distribution_name:
# Cannot use the distribution.files() as those only list the files in the site-packages,
# which for editable installations does not necessarily give the actual location of the
# source files. It may rely on a Finder, which has to run.
# The most robust way of resolving the location is to let the import machinery do its
# job, using importlib.import_module. We do not want however to actually import the
# server libraries found, which is why we do it in a subprocess.
package_path = subprocess.check_output( # nosec B603
args=[
sys.executable,
"-c",
f"""import importlib
print(importlib.import_module('ansys.dpf.server{distribution_name[16:]}'.replace('-', '_')).__path__[0])""",
],
text=True,
).rstrip()
path_per_version[
packaging.version.parse(d.version)
] = package_path
return path_per_version


Expand Down
Loading