From b18950efe91caf37ec5492179f15b0d8863cff82 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 22 Dec 2022 13:12:46 +0100 Subject: [PATCH 1/5] Adding on_docker check and get_mapdl_envvar --- src/ansys/mapdl/core/mapdl.py | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 2f928e0767..c8c00b2502 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -194,6 +194,8 @@ def __init__( self._cached_routine = None self._geometry = None self._kylov = None + self._on_docker = None + self._platform = None # Setting up loggers self._log = logger.add_instance_logger( @@ -3867,3 +3869,65 @@ def set( ) else: return output + + def get_mapdl_envvar(self, envvar): + """Get the value of an MAPDL environment variable. + + This variable can be only in one line. + + Parameters + ---------- + envvar : str + The name of the environment variable. + + Returns + ------- + str + The value of the environment variable. + + Examples + -------- + >>> mapdl.get_mapdl_envvar('ANSYS_VER')""" + self.inquire("MYSTRARR", "ENV", envvar) + return self.parameters["MYSTRARR"] + + def _check_mapdl_os(self): + platform = self.get_value("active", 0, "platform").strip() + if "l" in platform.lower(): + self._platform = "linux" + elif "w" in platform.lower(): + self._platform = "windows" + else: + raise MapdlRuntimeError("Unknown platform: {}".format(platform)) + + @property + def platform(self): + """Return the platform where MAPDL is running.""" + if self._platform is None: + self._check_mapdl_os() + return self._platform + + def _check_on_docker(self): + """Check if MAPDL is running on docker.""" + # self.get_mapdl_envvar("ON_DOCKER") # for later + + if self.platform == "linux": + self.sys( + "if grep -sq 'docker\|lxc' /proc/1/cgroup; then echo 'true' > __outputcmd__.txt; else echo 'false' > __outputcmd__.txt;fi;" + ) + elif self.platform == "windows": # pragma: no cover + return False # TODO: check if it is running a windows docker container. So far it is not supported. + + if self.is_grpc and not self._local: + return self._download_as_raw("__outputcmd__.txt").decode().strip() == "true" + else: + file_ = os.path.join(self.directory, "__outputcmd__.txt") + with open(file_, "r") as f: + return f.read().strip() == "true" + + @property + def on_docker(self): + """Check if MAPDL is running on docker.""" + if self._on_docker is None: + self._on_docker = self._check_on_docker() + return self._on_docker From 3e72f2cd2447ccd01f41c37bdffe51029f8eb173 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 22 Dec 2022 13:17:46 +0100 Subject: [PATCH 2/5] removing get_mapdl_envvar. It will go to another PR. --- src/ansys/mapdl/core/mapdl.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index c8c00b2502..3da2357b11 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3870,27 +3870,6 @@ def set( else: return output - def get_mapdl_envvar(self, envvar): - """Get the value of an MAPDL environment variable. - - This variable can be only in one line. - - Parameters - ---------- - envvar : str - The name of the environment variable. - - Returns - ------- - str - The value of the environment variable. - - Examples - -------- - >>> mapdl.get_mapdl_envvar('ANSYS_VER')""" - self.inquire("MYSTRARR", "ENV", envvar) - return self.parameters["MYSTRARR"] - def _check_mapdl_os(self): platform = self.get_value("active", 0, "platform").strip() if "l" in platform.lower(): From b85cab0c81697113b8839f169eb3d78f0e967eae Mon Sep 17 00:00:00 2001 From: German Date: Thu, 22 Dec 2022 13:19:09 +0100 Subject: [PATCH 3/5] Adding 'is_local' method. --- src/ansys/mapdl/core/mapdl.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 3da2357b11..799ed82f4e 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3897,7 +3897,7 @@ def _check_on_docker(self): elif self.platform == "windows": # pragma: no cover return False # TODO: check if it is running a windows docker container. So far it is not supported. - if self.is_grpc and not self._local: + if self.is_grpc and not self.is_local: return self._download_as_raw("__outputcmd__.txt").decode().strip() == "true" else: file_ = os.path.join(self.directory, "__outputcmd__.txt") @@ -3910,3 +3910,8 @@ def on_docker(self): if self._on_docker is None: self._on_docker = self._check_on_docker() return self._on_docker + + @property + def is_local(self): + """Check if the instance is running locally or remotely.""" + return self._local From dc1cf93306903459870b0c029c90977fcabed2a2 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 22 Dec 2022 13:24:47 +0100 Subject: [PATCH 4/5] Adding unit tests --- tests/test_mapdl.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 4f0699c5dd..3dbc43b8f8 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1658,3 +1658,15 @@ def test_mode(mapdl): assert mapdl.is_console mapdl._mode = "grpc" # Going back to default + + +def test_is_local(mapdl): + assert mapdl.is_local == mapdl._local + + +def test_on_docker(mapdl): + assert mapdl.on_docker == mapdl._on_docker + if os.getenv("PYMAPDL_START_INSTANCE", "false") == "true": + assert mapdl.on_docker + else: + assert not mapdl.on_docker From 44d2d8c3fe5f3e5eeca9b370cb50a7c7f7fad4d6 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 22 Dec 2022 14:33:43 +0100 Subject: [PATCH 5/5] fixing coverage --- src/ansys/mapdl/core/mapdl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 5684f33b41..50449c6bd7 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3874,9 +3874,9 @@ def _check_mapdl_os(self): platform = self.get_value("active", 0, "platform").strip() if "l" in platform.lower(): self._platform = "linux" - elif "w" in platform.lower(): + elif "w" in platform.lower(): # pragma: no cover self._platform = "windows" - else: + else: # pragma: no cover raise MapdlRuntimeError("Unknown platform: {}".format(platform)) @property @@ -3899,7 +3899,7 @@ def _check_on_docker(self): if self.is_grpc and not self.is_local: return self._download_as_raw("__outputcmd__.txt").decode().strip() == "true" - else: + else: # pragma: no cover file_ = os.path.join(self.directory, "__outputcmd__.txt") with open(file_, "r") as f: return f.read().strip() == "true"