From b6aeeff8b3b308350638117b55198fefaf8ca5a3 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Thu, 25 Jan 2024 09:51:54 +0100 Subject: [PATCH 1/5] Add update_virtual_environment_for_custom_operators in custom_operator.py (WIP) Signed-off-by: paul.profizi --- src/ansys/dpf/core/custom_operator.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ansys/dpf/core/custom_operator.py b/src/ansys/dpf/core/custom_operator.py index ea5c53a0af..cec7bf2c25 100644 --- a/src/ansys/dpf/core/custom_operator.py +++ b/src/ansys/dpf/core/custom_operator.py @@ -31,6 +31,26 @@ from ansys.dpf.gate import object_handler, capi, dpf_vector, integral_types +def update_virtual_environment_for_custom_operators(restore_original: bool = False): + """Updates the dpf-site.zip file used to start a venv for Python custom operators to run in. + + It updates the site-packages in dpf-site.zip with the site-packages of the current venv. + It stores the original dpf-site.zip in the users files. + + Parameters + ---------- + restore_original: + If ``True``, restores the original dpf-site.zip. + """ + # Restore the original dpf-site.zip + if restore_original: + return + # Update the dpf-site.zip based on the current virtual environment + current_site_packages_path = None + current_dpf_site_zip_path = None + original_dpf_site_zip_path = None + + def record_operator(operator_type, *args) -> None: """ Add an operator (with its name, run callback, and specification) to the DPF core registry. From cdc15c9e9cfa6c82eb00567d9050fc767dbdfaeb Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Thu, 25 Jan 2024 10:43:51 +0100 Subject: [PATCH 2/5] Add logic (WIP) Signed-off-by: paul.profizi --- src/ansys/dpf/core/custom_operator.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ansys/dpf/core/custom_operator.py b/src/ansys/dpf/core/custom_operator.py index cec7bf2c25..07562828e6 100644 --- a/src/ansys/dpf/core/custom_operator.py +++ b/src/ansys/dpf/core/custom_operator.py @@ -42,13 +42,18 @@ def update_virtual_environment_for_custom_operators(restore_original: bool = Fal restore_original: If ``True``, restores the original dpf-site.zip. """ + # Get the path to the dpf-site.zip in the current DPF server + current_dpf_site_zip_path = None + # Get the path to where we store the original dpf-site.zip + original_dpf_site_zip_path = None # Restore the original dpf-site.zip if restore_original: return - # Update the dpf-site.zip based on the current virtual environment + # Otherwise get the path to the site_packages of the current virtual environment current_site_packages_path = None - current_dpf_site_zip_path = None - original_dpf_site_zip_path = None + # Zip it + # Store original dpf-site.zip for this DPF Server + # Update with new def record_operator(operator_type, *args) -> None: From 0c913a47f5f1601f8c89e87e21b1df64018493ab Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Thu, 25 Jan 2024 11:47:34 +0100 Subject: [PATCH 3/5] Working version Signed-off-by: paul.profizi --- src/ansys/dpf/core/custom_operator.py | 57 +++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/ansys/dpf/core/custom_operator.py b/src/ansys/dpf/core/custom_operator.py index 07562828e6..972d86e135 100644 --- a/src/ansys/dpf/core/custom_operator.py +++ b/src/ansys/dpf/core/custom_operator.py @@ -8,6 +8,13 @@ import abc import ctypes +import os +import pathlib +import shutil +import sys +import warnings +import zipfile + import numpy import traceback @@ -37,23 +44,57 @@ def update_virtual_environment_for_custom_operators(restore_original: bool = Fal It updates the site-packages in dpf-site.zip with the site-packages of the current venv. It stores the original dpf-site.zip in the users files. + .. note:: + This feature is only available InProcess to ensure compatibility of the current venv + client-side with the machine where the server is run. + Parameters ---------- restore_original: If ``True``, restores the original dpf-site.zip. """ # Get the path to the dpf-site.zip in the current DPF server - current_dpf_site_zip_path = None + server = dpf.server.get_or_create_server(dpf.SERVER) + if server.has_client(): + raise NotImplementedError( + "Updating the dpf-site.zip of a DPF Server is only available when InProcess." + ) + current_dpf_site_zip_path = os.path.join(server.ansys_path, "dpf", "python", "dpf-site.zip") # Get the path to where we store the original dpf-site.zip - original_dpf_site_zip_path = None + original_dpf_site_zip_path = os.path.join( + server.ansys_path, "dpf", "python", "original", "dpf-site.zip" + ) # Restore the original dpf-site.zip if restore_original: - return - # Otherwise get the path to the site_packages of the current virtual environment - current_site_packages_path = None - # Zip it - # Store original dpf-site.zip for this DPF Server - # Update with new + if os.path.exists(original_dpf_site_zip_path): + shutil.move(src=original_dpf_site_zip_path, dst=current_dpf_site_zip_path) + os.rmdir(os.path.dirname(original_dpf_site_zip_path)) + else: + warnings.warn("No original dpf-site.zip found. Current is most likely the original.") + else: + # Get the current paths to site_packages + import site + paths_to_current_site_packages = site.getsitepackages() + current_site_packages_path = None + # Get the first one targeting an actual site-packages folder + for path_to_site_packages in paths_to_current_site_packages: + if path_to_site_packages[-13:] == "site-packages": + current_site_packages_path = pathlib.Path(path_to_site_packages) + break + if current_site_packages_path is None: + warnings.warn("Could not find a currently loaded site-packages folder to update from.") + return + # Store original dpf-site.zip for this DPF Server if no original is stored + if not os.path.exists(os.path.dirname(original_dpf_site_zip_path)): + os.mkdir(os.path.dirname(original_dpf_site_zip_path)) + shutil.move(src=current_dpf_site_zip_path, dst=original_dpf_site_zip_path) + # Zip the current site-packages at the destination + with zipfile.ZipFile(current_dpf_site_zip_path, mode="w") as archive: + for file_path in current_site_packages_path.rglob("*"): + archive.write( + filename=file_path, + arcname=file_path.relative_to(current_site_packages_path) + ) def record_operator(operator_type, *args) -> None: From 714052940d4bf6ffc3dd2a14fa07a8d497cc34fa Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Thu, 25 Jan 2024 11:49:13 +0100 Subject: [PATCH 4/5] Working version Signed-off-by: paul.profizi --- src/ansys/dpf/core/custom_operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/custom_operator.py b/src/ansys/dpf/core/custom_operator.py index 972d86e135..c4c6efd554 100644 --- a/src/ansys/dpf/core/custom_operator.py +++ b/src/ansys/dpf/core/custom_operator.py @@ -42,7 +42,7 @@ def update_virtual_environment_for_custom_operators(restore_original: bool = Fal """Updates the dpf-site.zip file used to start a venv for Python custom operators to run in. It updates the site-packages in dpf-site.zip with the site-packages of the current venv. - It stores the original dpf-site.zip in the users files. + It stores the original dpf-site.zip for future restoration. .. note:: This feature is only available InProcess to ensure compatibility of the current venv From f3ec00cd6be293cb7902da5b4381d6d066db2b3a Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Thu, 25 Jan 2024 11:58:20 +0100 Subject: [PATCH 5/5] Fix QA Signed-off-by: paul.profizi --- src/ansys/dpf/core/custom_operator.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/dpf/core/custom_operator.py b/src/ansys/dpf/core/custom_operator.py index c4c6efd554..cdfc5e93cd 100644 --- a/src/ansys/dpf/core/custom_operator.py +++ b/src/ansys/dpf/core/custom_operator.py @@ -11,7 +11,6 @@ import os import pathlib import shutil -import sys import warnings import zipfile