From 3045032f9c23952d208c7cf5a70096147b70f4b5 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:37:33 +0000 Subject: [PATCH 1/6] fix: envvars ignoring current environment --- src/ansys/mapdl/core/launcher.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 349602c667..a9dacb6194 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -602,7 +602,7 @@ def launch_grpc( env_vars = update_env_vars(add_env_vars, replace_env_vars) LOG.info( - f"Running a local instance at port {port} the following command: '{command}'" + f"Running a local instance in {run_location} at port {port} the following command: '{command}'" ) LOG.debug("MAPDL starting in background.") @@ -681,6 +681,7 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): empty_i = 0 terminal_output = "" + LOG.debug(f"Checking if MAPDL server is alive") while time.time() < (t0 + timeout): terminal_output += "\n".join(_get_std_output(std_queue=stdout_queue)).strip() @@ -700,6 +701,7 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): break else: + LOG.debug(f"MAPDL gRPC server didn't print any valid output:\n{terminal_output}") raise MapdlDidNotStart("MAPDL failed to start the gRPC server") @@ -1811,8 +1813,7 @@ def launch_mapdl( port, actual_run_location, process = launch_grpc( port=port, - add_env_vars=add_env_vars, - replace_env_vars=replace_env_vars, + replace_env_vars=env_vars, **start_parm, ) @@ -1917,7 +1918,7 @@ def check_mode(mode, version): return mode -def update_env_vars(add_env_vars, replace_env_vars): +def update_env_vars(add_env_vars: dict, replace_env_vars: dict) -> dict: """ Update environment variables for the MAPDL process. @@ -1939,6 +1940,8 @@ def update_env_vars(add_env_vars, replace_env_vars): """ # Expanding/replacing env variables for the process. + envvars = os.environ.copy() + if add_env_vars and replace_env_vars: raise ValueError( "'add_env_vars' and 'replace_env_vars' are incompatible. Please provide only one." @@ -1950,9 +1953,8 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'add_env_vars' should be a dict with env vars." ) - add_env_vars.update(os.environ) + envvars.update(add_env_vars) LOG.debug(f"Updating environment variables with: {add_env_vars}") - return add_env_vars elif replace_env_vars: if not isinstance(replace_env_vars, dict): @@ -1960,7 +1962,9 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'replace_env_vars' should be a dict with env vars." ) LOG.debug(f"Replacing environment variables with: {replace_env_vars}") - return replace_env_vars + envvars = replace_env_vars + + return envvars def _check_license_argument(license_type, additional_switches): From 8ca48a68f1256723b7b38bd3e2c9bf3557132375 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:37:33 +0000 Subject: [PATCH 2/6] fix: envvars ignoring current environment --- src/ansys/mapdl/core/launcher.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 349602c667..a9dacb6194 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -602,7 +602,7 @@ def launch_grpc( env_vars = update_env_vars(add_env_vars, replace_env_vars) LOG.info( - f"Running a local instance at port {port} the following command: '{command}'" + f"Running a local instance in {run_location} at port {port} the following command: '{command}'" ) LOG.debug("MAPDL starting in background.") @@ -681,6 +681,7 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): empty_i = 0 terminal_output = "" + LOG.debug(f"Checking if MAPDL server is alive") while time.time() < (t0 + timeout): terminal_output += "\n".join(_get_std_output(std_queue=stdout_queue)).strip() @@ -700,6 +701,7 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): break else: + LOG.debug(f"MAPDL gRPC server didn't print any valid output:\n{terminal_output}") raise MapdlDidNotStart("MAPDL failed to start the gRPC server") @@ -1811,8 +1813,7 @@ def launch_mapdl( port, actual_run_location, process = launch_grpc( port=port, - add_env_vars=add_env_vars, - replace_env_vars=replace_env_vars, + replace_env_vars=env_vars, **start_parm, ) @@ -1917,7 +1918,7 @@ def check_mode(mode, version): return mode -def update_env_vars(add_env_vars, replace_env_vars): +def update_env_vars(add_env_vars: dict, replace_env_vars: dict) -> dict: """ Update environment variables for the MAPDL process. @@ -1939,6 +1940,8 @@ def update_env_vars(add_env_vars, replace_env_vars): """ # Expanding/replacing env variables for the process. + envvars = os.environ.copy() + if add_env_vars and replace_env_vars: raise ValueError( "'add_env_vars' and 'replace_env_vars' are incompatible. Please provide only one." @@ -1950,9 +1953,8 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'add_env_vars' should be a dict with env vars." ) - add_env_vars.update(os.environ) + envvars.update(add_env_vars) LOG.debug(f"Updating environment variables with: {add_env_vars}") - return add_env_vars elif replace_env_vars: if not isinstance(replace_env_vars, dict): @@ -1960,7 +1962,9 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'replace_env_vars' should be a dict with env vars." ) LOG.debug(f"Replacing environment variables with: {replace_env_vars}") - return replace_env_vars + envvars = replace_env_vars + + return envvars def _check_license_argument(license_type, additional_switches): From e77b46f3e816781e947f27f5bc70911b9b3cfc1e Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:25:14 +0000 Subject: [PATCH 3/6] feat: getting env vars in `launch_mapdl` --- src/ansys/mapdl/core/launcher.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index a9dacb6194..51cc2fb25b 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -1770,6 +1770,9 @@ def launch_mapdl( f"The machine has {machine_cores} cores. PyMAPDL is asking for {nproc} cores." ) + # Setting env vars + env_vars = update_env_vars(add_env_vars, replace_env_vars) + start_parm.update( { "exec_file": exec_file, From 74bbc3bb7a8a9c0d5815e1695d3b57b3fefa06da Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:29:37 +0000 Subject: [PATCH 4/6] ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci --- src/ansys/mapdl/core/launcher.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 51cc2fb25b..ceafbbf806 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -701,7 +701,9 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): break else: - LOG.debug(f"MAPDL gRPC server didn't print any valid output:\n{terminal_output}") + LOG.debug( + f"MAPDL gRPC server didn't print any valid output:\n{terminal_output}" + ) raise MapdlDidNotStart("MAPDL failed to start the gRPC server") From 80a9f7f910074ee11f52f5cf7891246b0acc670b Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 7 Oct 2024 08:30:51 +0000 Subject: [PATCH 5/6] chore: adding changelog file 3461.fixed.md --- doc/changelog.d/3461.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3461.fixed.md diff --git a/doc/changelog.d/3461.fixed.md b/doc/changelog.d/3461.fixed.md new file mode 100644 index 0000000000..76f0aad061 --- /dev/null +++ b/doc/changelog.d/3461.fixed.md @@ -0,0 +1 @@ +fix: environment variables not being passed to MAPDL process \ No newline at end of file From e3605d022ecc983c75c3023b35104f33529c3d49 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:30:48 +0200 Subject: [PATCH 6/6] fix: test --- tests/test_launcher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 5be0570c31..0e4bfd396b 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -256,7 +256,8 @@ def test_remove_temp_files_fail(tmpdir, mapdl): def test_env_injection(): - assert update_env_vars(None, None) is None + no_inject = update_env_vars(None, None) + assert no_inject == os.environ.copy() # return os.environ assert "myenvvar" in update_env_vars({"myenvvar": "True"}, None)