Skip to content

Commit f89b36c

Browse files
authored
Deprecate pkg_resources usage (#2008)
* Remove pkg_resources in __init__.py * Remove pkg_resources in ansys.dpf.gate.load_api._paths_to_dpf_server_library_installs * Fix module name for cases where package is reported as "ansys-dpf-server-20XX-Y-ZZZ" * Remove rogue print * Fix QA * Fix QA
1 parent e4f9aff commit f89b36c

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/ansys/dpf/core/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import pkg_resources
32

43
try:
54
import importlib.metadata as importlib_metadata
@@ -25,7 +24,7 @@
2524
except: # pragma: no cover
2625
pass
2726

28-
installed = [d.project_name for d in pkg_resources.working_set]
27+
installed = [d.metadata["Name"] for d in importlib_metadata.distributions()]
2928
check_for = ["ansys-dpf-gatebin", "ansys-dpf-gate", "ansys-grpc-dpf"]
3029
if any([c in installed for c in check_for]):
3130
raise ImportError(f"Error during import of ansys-dpf-core:\n"

src/ansys/dpf/gate/load_api.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import os
2+
import subprocess # nosec B404
3+
import sys
4+
25
import packaging.version
3-
import pkg_resources
4-
import importlib
6+
try:
7+
import importlib.metadata as importlib_metadata
8+
except ImportError: # Python < 3.10 (backport)
9+
import importlib_metadata as importlib_metadata
510
from ansys.dpf.gate.generated import capi
611
from ansys.dpf.gate import utils, errors
712
from ansys.dpf.gate._version import __ansys_version__
@@ -74,19 +79,27 @@ def _find_latest_ansys_versions():
7479

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

92105

0 commit comments

Comments
 (0)