Skip to content

Commit d960a27

Browse files
committed
[OMCProcess] get file with port information from omc log
1 parent d87e46e commit d960a27

1 file changed

Lines changed: 40 additions & 24 deletions

File tree

OMPython/OMCSession.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ def __init__(
472472
except OSError as ex:
473473
raise OMCSessionException(f"Cannot open log file {logfile}.") from ex
474474

475+
self._re_portfile_path = re.compile(pattern=r'\nDumped server port in file: (.*?)($|\n)',
476+
flags=re.MULTILINE | re.DOTALL)
477+
475478
def __del__(self):
476479
if self._omc_loghandle is not None:
477480
try:
@@ -506,6 +509,17 @@ def get_log(self) -> str:
506509

507510
return log
508511

512+
def _get_portfile_path(self) -> Optional[pathlib.Path]:
513+
omc_log = self.get_log()
514+
515+
portfile = self._re_portfile_path.findall(string=omc_log)
516+
517+
portfile_path = None
518+
if portfile:
519+
portfile_path = pathlib.Path(portfile[-1][0])
520+
521+
return portfile_path
522+
509523

510524
class OMCProcessPort(OMCProcess):
511525

@@ -574,11 +588,11 @@ def _omc_port_get(self) -> str:
574588
# See if the omc server is running
575589
attempts = 0
576590
while True:
577-
omc_file_port = self._temp_dir / self._omc_file_port
591+
omc_portfile_path = self._get_portfile_path()
578592

579-
if omc_file_port.is_file():
593+
if omc_portfile_path is not None and omc_portfile_path.is_file():
580594
# Read the port file
581-
with open(file=omc_file_port, mode='r', encoding="utf-8") as f_p:
595+
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
582596
port = f_p.readline()
583597
break
584598

@@ -588,7 +602,7 @@ def _omc_port_get(self) -> str:
588602
attempts += 1
589603
if attempts == 80.0:
590604
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
591-
f"Could not open file {omc_file_port}. "
605+
f"Could not open file {omc_portfile_path}. "
592606
f"Log-file says:\n{self.get_log()}")
593607
time.sleep(self._timeout / 80.0)
594608

@@ -761,7 +775,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
761775
return omc_command
762776

763777
def _omc_port_get(self) -> str:
764-
omc_file_port = '/tmp/' + self._omc_file_port
765778
port = None
766779

767780
if not isinstance(self._dockerCid, str):
@@ -770,22 +783,24 @@ def _omc_port_get(self) -> str:
770783
# See if the omc server is running
771784
attempts = 0
772785
while True:
773-
try:
774-
output = subprocess.check_output(args=["docker",
775-
"exec", self._dockerCid,
776-
"cat", omc_file_port],
777-
stderr=subprocess.DEVNULL)
778-
port = output.decode().strip()
779-
except subprocess.CalledProcessError:
780-
pass
786+
omc_portfile_path = self._get_portfile_path()
787+
if omc_portfile_path is not None:
788+
try:
789+
output = subprocess.check_output(args=["docker",
790+
"exec", self._dockerCid,
791+
"cat", omc_portfile_path.as_posix()],
792+
stderr=subprocess.DEVNULL)
793+
port = output.decode().strip()
794+
except subprocess.CalledProcessError:
795+
pass
781796

782797
if port is not None:
783798
break
784799

785800
attempts += 1
786801
if attempts == 80.0:
787802
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). "
788-
f"Could not open file {omc_file_port}. "
803+
f"Could not open port file {omc_portfile_path}. "
789804
f"Log-file says:\n{self.get_log()}")
790805
time.sleep(self._timeout / 80.0)
791806

@@ -903,7 +918,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
903918
return omc_command
904919

905920
def _omc_port_get(self) -> str:
906-
omc_file_port = '/tmp/' + self._omc_file_port
907921
port = None
908922

909923
if not isinstance(self._dockerCid, str):
@@ -912,22 +926,24 @@ def _omc_port_get(self) -> str:
912926
# See if the omc server is running
913927
attempts = 0
914928
while True:
915-
try:
916-
output = subprocess.check_output(args=["docker",
917-
"exec", self._dockerCid,
918-
"cat", omc_file_port],
919-
stderr=subprocess.DEVNULL)
920-
port = output.decode().strip()
921-
except subprocess.CalledProcessError:
922-
pass
929+
omc_portfile_path = self._get_portfile_path()
930+
if omc_portfile_path is not None:
931+
try:
932+
output = subprocess.check_output(args=["docker",
933+
"exec", self._dockerCid,
934+
"cat", omc_portfile_path.as_posix()],
935+
stderr=subprocess.DEVNULL)
936+
port = output.decode().strip()
937+
except subprocess.CalledProcessError:
938+
pass
923939

924940
if port is not None:
925941
break
926942

927943
attempts += 1
928944
if attempts == 80.0:
929945
raise OMCSessionException(f"Docker container based OMC Server did not start (timeout={self._timeout}). "
930-
f"Could not open file {omc_file_port}. "
946+
f"Could not open port file {omc_portfile_path}. "
931947
f"Log-file says:\n{self.get_log()}")
932948
time.sleep(self._timeout / 80.0)
933949

0 commit comments

Comments
 (0)