From d960a277d8958db96fef2bc395c1e353cc7041b4 Mon Sep 17 00:00:00 2001 From: syntron Date: Thu, 12 Jun 2025 19:42:02 +0200 Subject: [PATCH 1/2] [OMCProcess] get file with port information from omc log --- OMPython/OMCSession.py | 64 ++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index b7adf0a88..8ba18c077 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -472,6 +472,9 @@ def __init__( except OSError as ex: raise OMCSessionException(f"Cannot open log file {logfile}.") from ex + self._re_portfile_path = re.compile(pattern=r'\nDumped server port in file: (.*?)($|\n)', + flags=re.MULTILINE | re.DOTALL) + def __del__(self): if self._omc_loghandle is not None: try: @@ -506,6 +509,17 @@ def get_log(self) -> str: return log + def _get_portfile_path(self) -> Optional[pathlib.Path]: + omc_log = self.get_log() + + portfile = self._re_portfile_path.findall(string=omc_log) + + portfile_path = None + if portfile: + portfile_path = pathlib.Path(portfile[-1][0]) + + return portfile_path + class OMCProcessPort(OMCProcess): @@ -574,11 +588,11 @@ def _omc_port_get(self) -> str: # See if the omc server is running attempts = 0 while True: - omc_file_port = self._temp_dir / self._omc_file_port + omc_portfile_path = self._get_portfile_path() - if omc_file_port.is_file(): + if omc_portfile_path is not None and omc_portfile_path.is_file(): # Read the port file - with open(file=omc_file_port, mode='r', encoding="utf-8") as f_p: + with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p: port = f_p.readline() break @@ -588,7 +602,7 @@ def _omc_port_get(self) -> str: attempts += 1 if attempts == 80.0: raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). " - f"Could not open file {omc_file_port}. " + f"Could not open file {omc_portfile_path}. " f"Log-file says:\n{self.get_log()}") time.sleep(self._timeout / 80.0) @@ -761,7 +775,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list: return omc_command def _omc_port_get(self) -> str: - omc_file_port = '/tmp/' + self._omc_file_port port = None if not isinstance(self._dockerCid, str): @@ -770,14 +783,16 @@ def _omc_port_get(self) -> str: # See if the omc server is running attempts = 0 while True: - try: - output = subprocess.check_output(args=["docker", - "exec", self._dockerCid, - "cat", omc_file_port], - stderr=subprocess.DEVNULL) - port = output.decode().strip() - except subprocess.CalledProcessError: - pass + omc_portfile_path = self._get_portfile_path() + if omc_portfile_path is not None: + try: + output = subprocess.check_output(args=["docker", + "exec", self._dockerCid, + "cat", omc_portfile_path.as_posix()], + stderr=subprocess.DEVNULL) + port = output.decode().strip() + except subprocess.CalledProcessError: + pass if port is not None: break @@ -785,7 +800,7 @@ def _omc_port_get(self) -> str: attempts += 1 if attempts == 80.0: raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). " - f"Could not open file {omc_file_port}. " + f"Could not open port file {omc_portfile_path}. " f"Log-file says:\n{self.get_log()}") time.sleep(self._timeout / 80.0) @@ -903,7 +918,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list: return omc_command def _omc_port_get(self) -> str: - omc_file_port = '/tmp/' + self._omc_file_port port = None if not isinstance(self._dockerCid, str): @@ -912,14 +926,16 @@ def _omc_port_get(self) -> str: # See if the omc server is running attempts = 0 while True: - try: - output = subprocess.check_output(args=["docker", - "exec", self._dockerCid, - "cat", omc_file_port], - stderr=subprocess.DEVNULL) - port = output.decode().strip() - except subprocess.CalledProcessError: - pass + omc_portfile_path = self._get_portfile_path() + if omc_portfile_path is not None: + try: + output = subprocess.check_output(args=["docker", + "exec", self._dockerCid, + "cat", omc_portfile_path.as_posix()], + stderr=subprocess.DEVNULL) + port = output.decode().strip() + except subprocess.CalledProcessError: + pass if port is not None: break @@ -927,7 +943,7 @@ def _omc_port_get(self) -> str: attempts += 1 if attempts == 80.0: raise OMCSessionException(f"Docker container based OMC Server did not start (timeout={self._timeout}). " - f"Could not open file {omc_file_port}. " + f"Could not open port file {omc_portfile_path}. " f"Log-file says:\n{self.get_log()}") time.sleep(self._timeout / 80.0) From 450172963d734b82397afd6ba8484e53140e7f6d Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 13 Jun 2025 21:48:49 +0200 Subject: [PATCH 2/2] [OMCProcess] rename _omc_file_port => only needed for log and docker container ID file --- OMPython/OMCSession.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 8ba18c077..8bdff30a6 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -456,16 +456,13 @@ def __init__( self._currentUser = "nobody" # omc port and log file - if sys.platform == 'win32': - self._omc_file_port = f"openmodelica.port.{self._random_string}" - else: - self._omc_file_port = f"openmodelica.{self._currentUser}.port.{self._random_string}" + self._omc_filebase = f"openmodelica.{self._random_string}" # get a temporary directory self._temp_dir = pathlib.Path(tempfile.gettempdir()) # setup log file - this file must be closed in the destructor - logfile = self._temp_dir / (self._omc_file_port + '.log') + logfile = self._temp_dir / (self._omc_filebase + ".log") self._omc_loghandle: Optional[io.TextIOWrapper] = None try: self._omc_loghandle = open(file=logfile, mode="w+", encoding="utf-8") @@ -756,7 +753,7 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list: raise OMCSessionException(f'dockerNetwork was set to {self._dockerNetwork}, ' 'but only \"host\" or \"separate\" is allowed') - self._dockerCidFile = self._temp_dir / (self._omc_file_port + ".docker.cid") + self._dockerCidFile = self._temp_dir / (self._omc_filebase + ".docker.cid") if isinstance(self._interactivePort, int): extraFlags = extraFlags + [f"--interactivePort={int(self._interactivePort)}"]