From e1787a1738fceb78c9ee61541c0df3ac8c87dc9b Mon Sep 17 00:00:00 2001 From: cbellot Date: Fri, 11 Apr 2025 08:01:23 +0200 Subject: [PATCH 1/9] fix no context --- src/ansys/dpf/core/server_context.py | 3 +- src/ansys/dpf/core/server_types.py | 51 +++++++++++++++------------- tests/test_service.py | 1 + 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/ansys/dpf/core/server_context.py b/src/ansys/dpf/core/server_context.py index 3587989716d..12d6e079184 100644 --- a/src/ansys/dpf/core/server_context.py +++ b/src/ansys/dpf/core/server_context.py @@ -45,7 +45,6 @@ class LicensingContextType(Enum): """Enum representing different types of licensing contexts.""" - none = 5 premium = 1 """Checks if at least one license increment exists and allows operators to block an increment.""" @@ -295,7 +294,7 @@ def __ne__(self, other): class AvailableServerContexts: """Defines available server contexts.""" - no_context = ServerContext(LicensingContextType.none, "") + no_context = ServerContext(2, "") pre_defined_environment = ServerContext(0) """DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will be taken""" diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index db360af1cf6..3a3f1f5128c 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -50,6 +50,7 @@ from ansys.dpf.core import __version__, errors, server_context, server_factory from ansys.dpf.core._version import min_server_version, server_to_ansys_version from ansys.dpf.core.check_version import server_meet_version +from ansys.dpf.core.server_context import ServerContext from ansys.dpf.gate import data_processing_grpcapi, load_api if TYPE_CHECKING: # pragma: no cover @@ -114,7 +115,11 @@ def _verify_ansys_path_is_valid(ansys_path, executable, path_in_install=None): def _run_launch_server_process( - ip, port, ansys_path=None, docker_config=server_factory.RunningDockerConfig() + ip, + port, + ansys_path=None, + docker_config=server_factory.RunningDockerConfig(), + context: ServerContext = None, ): bShell = False if docker_config.use_docker: @@ -127,6 +132,8 @@ def _run_launch_server_process( if os.name == "nt": executable = "Ans.Dpf.Grpc.bat" run_cmd = f"{executable} --address {ip} --port {port}" + if context is not None: + run_cmd += f" --context {context.licensing_context_type}" else: executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover run_cmd = [ @@ -134,6 +141,8 @@ def _run_launch_server_process( f"--address {ip}", f"--port {port}", ] # pragma: no cover + if context is not None: + run_cmd.append(f"--context {context.licensing_context_type}") path_in_install = load_api._get_path_in_install(internal_folder="bin") dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install) @@ -204,7 +213,9 @@ def read_stdout(): raise RuntimeError(errstr) -def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10): +def launch_dpf( + ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10, context: ServerContext = None +): """Launch Ansys DPF. Parameters @@ -222,9 +233,10 @@ def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10): Maximum number of seconds for the initialization attempt. The default is ``10``. Once the specified number of seconds passes, the connection fails. - + context : , optional + Context to apply to DPF server when launching it. """ - process = _run_launch_server_process(ip, port, ansys_path) + process = _run_launch_server_process(ip, port, ansys_path, context=context) lines = [] current_errors = [] _wait_and_check_server_connection( @@ -766,7 +778,7 @@ def __init__( launch_server: bool = True, docker_config: DockerConfig = RUNNING_DOCKER, use_pypim: bool = True, - context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT, + context: server_context.ServerContext = server_context.SERVER_CONTEXT, ): # Load DPFClientAPI from ansys.dpf.core.misc import is_pypim_configured @@ -808,7 +820,7 @@ def __init__( timeout=timeout, ) else: - launch_dpf(ansys_path, ip, port, timeout=timeout) + launch_dpf(ansys_path, ip, port, timeout=timeout, context=context) self._local_server = True # store port and ip for later reference @@ -820,15 +832,11 @@ def __init__( self._create_shutdown_funcs() self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time if context: - if context == core.AvailableServerContexts.no_context: - self._base_service.initialize() + try: + self._base_service.initialize_with_context(context) self._context = context - else: - try: - self._base_service.initialize_with_context(context) - self._context = context - except errors.DpfVersionNotSupported: - pass + except errors.DpfVersionNotSupported: + pass self.set_as_global(as_global=as_global) def _check_first_call(self, timeout: float): @@ -1201,7 +1209,7 @@ def __init__( launch_server: bool = True, docker_config: DockerConfig = RUNNING_DOCKER, use_pypim: bool = True, - context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT, + context: server_context.ServerContext = server_context.SERVER_CONTEXT, ): """Start the DPF server.""" # Use ansys.grpc.dpf @@ -1247,7 +1255,7 @@ def __init__( timeout=timeout, ) else: - launch_dpf(ansys_path, ip, port, timeout=timeout) + launch_dpf(ansys_path, ip, port, timeout=timeout, context=context) self._local_server = True from ansys.dpf.core import misc, settings @@ -1267,14 +1275,11 @@ def __init__( check_ansys_grpc_dpf_version(self, timeout) if context: - if context == core.AvailableServerContexts.no_context: + try: + self._base_service.initialize_with_context(context) self._context = context - else: - try: - self._base_service.initialize_with_context(context) - self._context = context - except errors.DpfVersionNotSupported: - pass + except errors.DpfVersionNotSupported: + pass self.set_as_global(as_global=as_global) def _create_shutdown_funcs(self): diff --git a/tests/test_service.py b/tests/test_service.py index 69b9503e993..d6413342807 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -510,6 +510,7 @@ def test_server_without_context(remote_config_server_type): ) none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type assert server.context.licensing_context_type == none_type + assert len(dpf.core.available_operator_names(server=server)) < 20 @pytest.mark.order("last") From b23d4ec46c09a8deadf07ac75145bb03845657b7 Mon Sep 17 00:00:00 2001 From: cbellot Date: Fri, 11 Apr 2025 08:37:51 +0200 Subject: [PATCH 2/9] fixes --- src/ansys/dpf/core/server.py | 2 +- src/ansys/dpf/core/server_context.py | 4 ---- tests/test_service.py | 3 ++- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ansys/dpf/core/server.py b/src/ansys/dpf/core/server.py index fd8b9e75ebf..305bfdd6f88 100644 --- a/src/ansys/dpf/core/server.py +++ b/src/ansys/dpf/core/server.py @@ -160,7 +160,7 @@ def start_local_server( config=None, use_pypim_by_default=True, context=None, -): +) -> BaseServer: """Start a new local DPF server at a given port and IP address. This method requires Windows and ANSYS 2021 R1 or later. If ``as_global=True``, which is diff --git a/src/ansys/dpf/core/server_context.py b/src/ansys/dpf/core/server_context.py index 12d6e079184..413dfd65d01 100644 --- a/src/ansys/dpf/core/server_context.py +++ b/src/ansys/dpf/core/server_context.py @@ -80,10 +80,6 @@ def same_licensing_context(first, second): bool True if the licensing contexts are compatible, False otherwise. """ - if (first == LicensingContextType.none and second != LicensingContextType.none) or ( - first != LicensingContextType.none and second == LicensingContextType.none - ): - return False if int(first) == int(LicensingContextType.entry) and int(second) != int( LicensingContextType.entry ): diff --git a/tests/test_service.py b/tests/test_service.py index d6413342807..df6073a805e 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -510,7 +510,8 @@ def test_server_without_context(remote_config_server_type): ) none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type assert server.context.licensing_context_type == none_type - assert len(dpf.core.available_operator_names(server=server)) < 20 + if server.check_version("10.0"): # Before, there was a bug + assert len(dpf.core.available_operator_names(server=server)) < 20 @pytest.mark.order("last") From 52ae14d41361d101f9b13be511e965b8b16768c8 Mon Sep 17 00:00:00 2001 From: cbellot Date: Fri, 11 Apr 2025 08:46:58 +0200 Subject: [PATCH 3/9] fix --- src/ansys/dpf/core/server_types.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index 3a3f1f5128c..ac5dee64b92 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -133,7 +133,7 @@ def _run_launch_server_process( executable = "Ans.Dpf.Grpc.bat" run_cmd = f"{executable} --address {ip} --port {port}" if context is not None: - run_cmd += f" --context {context.licensing_context_type}" + run_cmd += f" --context {int(context.licensing_context_type)}" else: executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover run_cmd = [ @@ -142,7 +142,7 @@ def _run_launch_server_process( f"--port {port}", ] # pragma: no cover if context is not None: - run_cmd.append(f"--context {context.licensing_context_type}") + run_cmd.append(f"--context {int(context.licensing_context_type)}") path_in_install = load_api._get_path_in_install(internal_folder="bin") dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install) @@ -1047,18 +1047,14 @@ def __init__( ) raise e if context: - if context == core.AvailableServerContexts.no_context: - self._base_service.initialize() - self._context = context - else: - try: - self.apply_context(context) - except errors.DpfVersionNotSupported: - self._base_service.initialize_with_context( - server_context.AvailableServerContexts.premium - ) - self._context = server_context.AvailableServerContexts.premium - pass + try: + self.apply_context(context) + except errors.DpfVersionNotSupported: + self._base_service.initialize_with_context( + server_context.AvailableServerContexts.premium + ) + self._context = server_context.AvailableServerContexts.premium + pass self.set_as_global(as_global=as_global) # Update the python os.environment if not os.name == "posix": From 5a805705c076bb973a049b2e3d6434bc79507cee Mon Sep 17 00:00:00 2001 From: cbellot Date: Fri, 11 Apr 2025 13:56:55 +0200 Subject: [PATCH 4/9] fix test --- tests/test_service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_service.py b/tests/test_service.py index df6073a805e..87fedb5af87 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -30,7 +30,7 @@ import pytest from ansys import dpf -from ansys.dpf.core import examples, path_utilities +from ansys.dpf.core import check_version, examples, path_utilities import conftest from conftest import running_docker @@ -510,7 +510,7 @@ def test_server_without_context(remote_config_server_type): ) none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type assert server.context.licensing_context_type == none_type - if server.check_version("10.0"): # Before, there was a bug + if check_version.server_meet_version_and_raise("10.0", server): # Before, there was a bug assert len(dpf.core.available_operator_names(server=server)) < 20 From c0629fa1b5e447c52f3c81bb51f06abc474cdd6b Mon Sep 17 00:00:00 2001 From: cbellot Date: Fri, 11 Apr 2025 14:21:15 +0200 Subject: [PATCH 5/9] fix --- tests/test_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index 87fedb5af87..dc20780036b 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -510,7 +510,7 @@ def test_server_without_context(remote_config_server_type): ) none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type assert server.context.licensing_context_type == none_type - if check_version.server_meet_version_and_raise("10.0", server): # Before, there was a bug + if check_version.server_meet_version("10.0", server): # Before, there was a bug assert len(dpf.core.available_operator_names(server=server)) < 20 From 2ff2b151797a090d3793e05602a3f7be6782c318 Mon Sep 17 00:00:00 2001 From: cbellot Date: Tue, 15 Apr 2025 09:05:49 +0200 Subject: [PATCH 6/9] deactivate test on docker --- tests/test_service.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index dc20780036b..0b9438e7b40 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -499,7 +499,8 @@ def test_context_environment_variable(reset_context_environment_variable): @pytest.mark.skipif( - not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0, reason="Failures on Windows 231" + not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0 or running_docker, + reason="Failures on Windows 231", ) def test_server_without_context(remote_config_server_type): """Tests starting a server without a no_context given.""" From a8fe83750bc929fcc95dacf0abbaaf9565ca6328 Mon Sep 17 00:00:00 2001 From: cbellot Date: Tue, 15 Apr 2025 10:18:34 +0200 Subject: [PATCH 7/9] fix --- src/ansys/dpf/core/server_types.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index ac5dee64b92..2602201b281 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -50,7 +50,7 @@ from ansys.dpf.core import __version__, errors, server_context, server_factory from ansys.dpf.core._version import min_server_version, server_to_ansys_version from ansys.dpf.core.check_version import server_meet_version -from ansys.dpf.core.server_context import ServerContext +from ansys.dpf.core.server_context import AvailableServerContexts, ServerContext from ansys.dpf.gate import data_processing_grpcapi, load_api if TYPE_CHECKING: # pragma: no cover @@ -132,7 +132,11 @@ def _run_launch_server_process( if os.name == "nt": executable = "Ans.Dpf.Grpc.bat" run_cmd = f"{executable} --address {ip} --port {port}" - if context is not None: + if context not in ( + None, + AvailableServerContexts.entry, + AvailableServerContexts.premium, + ): run_cmd += f" --context {int(context.licensing_context_type)}" else: executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover @@ -141,7 +145,11 @@ def _run_launch_server_process( f"--address {ip}", f"--port {port}", ] # pragma: no cover - if context is not None: + if context not in ( + None, + AvailableServerContexts.entry, + AvailableServerContexts.premium, + ): run_cmd.append(f"--context {int(context.licensing_context_type)}") path_in_install = load_api._get_path_in_install(internal_folder="bin") dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install) From 9fbf1f3bdb688829d426c06124a01f261c86805d Mon Sep 17 00:00:00 2001 From: cbellot Date: Tue, 15 Apr 2025 15:13:40 +0200 Subject: [PATCH 8/9] remove test --- tests/test_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index 0b9438e7b40..d75662a1e3a 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -499,7 +499,7 @@ def test_context_environment_variable(reset_context_environment_variable): @pytest.mark.skipif( - not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0 or running_docker, + not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0 or running_docker, reason="Failures on Windows 231", ) def test_server_without_context(remote_config_server_type): From 5a83c6466b1d420761005307dbf814d73f4b4a29 Mon Sep 17 00:00:00 2001 From: Camille Bellot <80476446+cbellot000@users.noreply.github.com> Date: Wed, 16 Apr 2025 11:21:08 +0200 Subject: [PATCH 9/9] Apply suggestions from code review --- src/ansys/dpf/core/server_context.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/server_context.py b/src/ansys/dpf/core/server_context.py index 413dfd65d01..16c115a558b 100644 --- a/src/ansys/dpf/core/server_context.py +++ b/src/ansys/dpf/core/server_context.py @@ -290,7 +290,8 @@ def __ne__(self, other): class AvailableServerContexts: """Defines available server contexts.""" - no_context = ServerContext(2, "") + """Applies an empty plugins xml: no plugins are loaded.""" + no_context = ServerContext(2, "") # 2 == ContextType.userDefined. This is not exposed publicly. pre_defined_environment = ServerContext(0) """DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will be taken"""