Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 8 additions & 7 deletions src/ansys/dpf/core/server_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,14 @@ def get_server_type_from_config(
if config.protocol == CommunicationProtocols.gRPC and config.legacy:
return LegacyGrpcServer
elif config.protocol == CommunicationProtocols.gRPC and not config.legacy:
if ansys_path is None:
from ansys.dpf.core.misc import __ansys_version__

ansys_path = os.environ.get("AWP_ROOT" + str(__ansys_version__), None)
if ansys_path is not None:
sub_folders = os.path.join(ansys_path, _get_path_in_install())
os.environ["PATH"] += sub_folders
# if ansys_path is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

what was that for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been added when we switched to ansys.dpf.gate. Apparently adding the path to GrpcClient.dll. Not required today.

# from ansys.dpf.core.misc import __ansys_version__
#
# ansys_path = os.environ.get("AWP_ROOT" + str(__ansys_version__), None)
# if ansys_path is not None:
# sub_folders = os.path.join(ansys_path, _get_path_in_install())
# os.environ["PATH"] += sub_folders
# print(f"{len(get_system_path())} Len(Python.PATH) after sub_folders")
return GrpcServer
elif config.protocol == CommunicationProtocols.InProcess and not config.legacy:
return InProcessServer
Expand Down
21 changes: 13 additions & 8 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,7 @@ def __init__(
# Forced to use ctypes to get the updated PATH due to sys.exec not the Python
# interpreter when running Python plugin test VS project
# The better solution would be to not need to update the path
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
windll.kernel32.GetEnvironmentVariableA.restype = c_int
name = "PATH"
b_name = name.encode("utf-8")
size = 32767
buffer = create_string_buffer(b"", size)
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
os.environ["PATH"] = buffer.value.decode("utf-8")
os.environ["PATH"] = get_system_path()

@property
def version(self):
Expand Down Expand Up @@ -977,6 +970,18 @@ def config(self):
return server_factory.AvailableServerConfigs.InProcessServer


def get_system_path() -> str:
"""Return the current PATH environment variable value of the system."""
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
windll.kernel32.GetEnvironmentVariableA.restype = c_int
name = "PATH"
b_name = name.encode("utf-8")
size = 32767
buffer = create_string_buffer(b"", size)
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
return buffer.value.decode("utf-8")


class LegacyGrpcServer(BaseServer):
"""Provides an instance of the DPF server using InProcess gRPC.
Kept for backward-compatibility with dpf servers <0.5.0.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def test_start_local_server(self, server_config):
assert has_local_server()
shutdown_all_session_servers()

def test_server_env_path_cleanup(self, server_config):
"""Test that running and stopping servers does not pollute the system PATH."""
from ansys.dpf.core.server_types import get_system_path
path_len_init = len(get_system_path())
server_0 = dpf.core.start_local_server(config=server_config)
assert len(get_system_path()) == path_len_init
server_0.release()
assert len(get_system_path()) == path_len_init

def test_start_local_server_with_config(self, server_config):
set_server_configuration(None)
shutdown_all_session_servers()
Expand Down