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

Refactor misc.py #1276

Merged
merged 9 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docs/source/getting_started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Install DPF Server

For more information about the license terms, see :ref:`DPF Preview License Agreement<target_to_license_terms>`.

To use a remote license, change or set the ``ANSYSLMD_LICENSE_FILE`` environment
To use a remote license set the ``ANSYSLMD_LICENSE_FILE`` environment
PProfizi marked this conversation as resolved.
Show resolved Hide resolved
variable to point to the Ansys license server ``<license_server_to_use>``:

.. code::
Expand Down
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
64 changes: 46 additions & 18 deletions src/ansys/dpf/gate/load_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,72 @@ def _get_path_in_install(is_posix: bool = None, internal_folder="dll"):
return path_in_install


def _get_dpf_path_in_install(is_posix: bool = None):
if not is_posix:
is_posix = os.name == "posix"
if not is_posix:
path_in_install = os.path.join("dpf", "bin", "winx64")
else:
path_in_install = os.path.join("dpf", "bin", "linx64")
return path_in_install


def _pythonize_awp_version(version):
if len(version) != 3:
return 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)
rafacanton marked this conversation as resolved.
Show resolved Hide resolved

if len(path_per_version) > 0:
rafacanton marked this conversation as resolved.
Show resolved Hide resolved
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 with an aisol folder
if not os.path.exists(os.path.join(ansys_path, _get_path_in_install())):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_path_in_install should be enriched to also check the existence of the dpf folder, where the majority of libraries are now placed

Copy link
Contributor Author

@PProfizi PProfizi Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafacanton we could, yet checks are usually made in methods which call _get_path_in_install:

That would mean refactoring all of those.
Still I agree that the current checks may not be enough. We should check that a given list of DLLs (TBD) is actually present.
This can be added to the objectives of this issue/PR, what do you think?

Copy link
Contributor Author

@PProfizi PProfizi Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafacanton while implementing a test for dpf/bin/winx64 or dpf/bin/linx64 folder, I realized that we may have an issue in ServerFactory.get_server_type_from_config which basically enforces use of AWP_ROOT242 when in non-legacy Grpc to define the aisol path to add to PATH... I think this is quite a problem. I also noticed that we still use _find_outdated_ansys_version() which tests for Ansys versions below 221... I do not think we still need that as it was used to force LegacyGrpc connections for servers 221 or below while now we could argue that the client itself does not support servers below 222. If we allow this connection to happen, there WILL be failures anyway.

I think there may be a lot of refactoring to do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: adding the test for a dpf folder, yet it appears consistently only starting with 231.

continue
# Check that it contains a DPF install with a dpf folder (for 231 and above)
if not os.path.exists(os.path.join(ansys_path, _get_dpf_path_in_install()))\
and \
int(awp_version) > 222:
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 +218,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 +233,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
Loading