Skip to content

Commit

Permalink
Deprecate pkg_resources usage (#2008)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PProfizi authored Jan 14, 2025
1 parent e4f9aff commit f89b36c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
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

0 comments on commit f89b36c

Please sign in to comment.