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 3587989716d..16c115a558b 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.""" @@ -81,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 ): @@ -295,7 +290,8 @@ def __ne__(self, other): class AvailableServerContexts: """Defines available server contexts.""" - no_context = ServerContext(LicensingContextType.none, "") + """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""" diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index db360af1cf6..2602201b281 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 AvailableServerContexts, 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,12 @@ def _run_launch_server_process( if os.name == "nt": executable = "Ans.Dpf.Grpc.bat" run_cmd = f"{executable} --address {ip} --port {port}" + 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 run_cmd = [ @@ -134,6 +145,12 @@ def _run_launch_server_process( f"--address {ip}", f"--port {port}", ] # pragma: no cover + 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) @@ -204,7 +221,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 +241,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 +786,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 +828,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 +840,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): @@ -1039,18 +1055,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": @@ -1201,7 +1213,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 +1259,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 +1279,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..d75662a1e3a 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 @@ -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_8_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.""" @@ -510,6 +511,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 + if check_version.server_meet_version("10.0", server): # Before, there was a bug + assert len(dpf.core.available_operator_names(server=server)) < 20 @pytest.mark.order("last")