|
1 | 1 | import os
|
| 2 | +import subprocess # nosec B404 |
| 3 | +import sys |
| 4 | + |
2 | 5 | 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 |
5 | 10 | from ansys.dpf.gate.generated import capi
|
6 | 11 | from ansys.dpf.gate import utils, errors
|
7 | 12 | from ansys.dpf.gate._version import __ansys_version__
|
@@ -74,19 +79,27 @@ def _find_latest_ansys_versions():
|
74 | 79 |
|
75 | 80 | def _paths_to_dpf_server_library_installs() -> dict:
|
76 | 81 | 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 |
90 | 103 | return path_per_version
|
91 | 104 |
|
92 | 105 |
|
|
0 commit comments