Skip to content

Commit

Permalink
Refactor and check for aisol path in install
Browse files Browse the repository at this point in the history
  • Loading branch information
PProfizi committed Nov 15, 2023
1 parent 87bcbf8 commit 26c8105
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
34 changes: 3 additions & 31 deletions src/ansys/dpf/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,8 @@ def _pythonize_awp_version(version):
return "20" + version[0:2] + "." + version[2]


def _find_latest_ansys_versions():
if hasattr(load_api, "_find_latest_ansys_versions"):
return load_api._find_latest_ansys_versions()
awp_versions = [key[-3:] for key in os.environ.keys() if "AWP_ROOT" in key]
installed_packages_list = {}

for awp_version in awp_versions:
if not awp_version.isnumeric():
continue
ansys_path = os.environ.get("AWP_ROOT" + awp_version)
if ansys_path:
installed_packages_list[
packaging.version.parse(_pythonize_awp_version(awp_version))
] = ansys_path

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)
installed_packages_list[
packaging.version.parse(module.__version__)
] = module.__path__[0]
except ModuleNotFoundError:
pass
except AttributeError:
pass
if len(installed_packages_list) > 0:
return installed_packages_list[sorted(installed_packages_list)[-1]]
def _find_latest_dpf_server():
return load_api._find_latest_dpf_server()


def find_ansys():
Expand All @@ -159,7 +131,7 @@ def find_ansys():
>>> path = find_ansys()
"""
latest_install = _find_latest_ansys_versions()
latest_install = _find_latest_dpf_server()
if latest_install:
return latest_install

Expand Down
49 changes: 31 additions & 18 deletions src/ansys/dpf/gate/load_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,51 @@ def _pythonize_awp_version(version):
return "20" + version[0:2] + "." + version[2]


def _find_latest_ansys_versions():
awp_versions = [key[-3:] for key in os.environ.keys() if "AWP_ROOT" in key]
installed_packages_list = {}
def _find_latest_dpf_server():
path_per_version = {}

for awp_version in awp_versions:
if not awp_version.isnumeric():
continue
ansys_path = os.environ.get("AWP_ROOT" + awp_version)
if ansys_path:
installed_packages_list[
packaging.version.parse(_pythonize_awp_version(awp_version))
] = ansys_path
path_per_version = _paths_to_dpf_in_unified_installs(path_per_version)

path_per_version = _paths_to_dpf_server_library_installs(path_per_version)

if len(path_per_version) > 0:
return path_per_version[sorted(path_per_version)[-1]]


def _paths_to_dpf_server_library_installs(path_per_version: dict) -> dict:
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)
installed_packages_list[
path_per_version[
packaging.version.parse(module.__version__)
] = module.__path__[0]
except ModuleNotFoundError:
pass
except AttributeError:
pass
if len(installed_packages_list) > 0:
return installed_packages_list[sorted(installed_packages_list)[-1]]
return path_per_version


def _unified_installer_path_if_exists():
return _find_latest_ansys_versions()
def _paths_to_dpf_in_unified_installs(path_per_version: dict) -> dict:
awp_versions = [key[-3:] for key in os.environ.keys() if "AWP_ROOT" in key]
for awp_version in awp_versions:
if not awp_version.isnumeric():
continue
ansys_path = os.environ.get("AWP_ROOT" + awp_version)
if ansys_path:
# Check that this ansys path exists
if not os.path.isdir(ansys_path):
continue
# Check that it contains a DPF install
if not os.path.exists(os.path.join(ansys_path, _get_path_in_install())):
continue
path_per_version[
packaging.version.parse(_pythonize_awp_version(awp_version))
] = ansys_path
return path_per_version


def _get_api_path_from_installer_or_package(ansys_path: str, is_posix: bool):
Expand Down Expand Up @@ -190,7 +203,7 @@ def load_client_api(ansys_path=None):

ANSYS_PATH = ansys_path
if ANSYS_PATH is None:
ANSYS_PATH = _unified_installer_path_if_exists()
ANSYS_PATH = _find_latest_dpf_server()
path = _get_api_path_from_installer_or_package(ANSYS_PATH, ISPOSIX)

return _try_load_api(path=path, name=name)
Expand All @@ -205,7 +218,7 @@ def load_grpc_client(ansys_path=None):

ANSYS_PATH = ansys_path
if ANSYS_PATH is None:
ANSYS_PATH = _unified_installer_path_if_exists()
ANSYS_PATH = _find_latest_dpf_server()
path = _get_api_path_from_installer_or_package(ANSYS_PATH, ISPOSIX)

# PATH should be set only on Windows and only if working
Expand Down

0 comments on commit 26c8105

Please sign in to comment.