Skip to content

Commit 65643d4

Browse files
committed
[OMCProcess] get file with port information from omc log
1 parent 99572e4 commit 65643d4

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

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

@@ -759,7 +773,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
759773
return omc_command
760774

761775
def _omc_port_get(self) -> str:
762-
omc_file_port = '/tmp/' + self._omc_file_port
763776
port = None
764777

765778
if not isinstance(self._dockerCid, str):
@@ -768,22 +781,24 @@ def _omc_port_get(self) -> str:
768781
# See if the omc server is running
769782
attempts = 0
770783
while True:
771-
try:
772-
output = subprocess.check_output(args=["docker",
773-
"exec", self._dockerCid,
774-
"cat", omc_file_port],
775-
stderr=subprocess.DEVNULL)
776-
port = output.decode().strip()
777-
except subprocess.CalledProcessError:
778-
pass
784+
omc_portfile_path = self._get_portfile_path()
785+
if omc_portfile_path is not None:
786+
try:
787+
output = subprocess.check_output(args=["docker",
788+
"exec", self._dockerCid,
789+
"cat", omc_portfile_path.as_posix()],
790+
stderr=subprocess.DEVNULL)
791+
port = output.decode().strip()
792+
except subprocess.CalledProcessError:
793+
pass
779794

780795
if port is not None:
781796
break
782797

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

@@ -901,7 +916,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
901916
return omc_command
902917

903918
def _omc_port_get(self) -> str:
904-
omc_file_port = '/tmp/' + self._omc_file_port
905919
port = None
906920

907921
if not isinstance(self._dockerCid, str):
@@ -910,22 +924,24 @@ def _omc_port_get(self) -> str:
910924
# See if the omc server is running
911925
attempts = 0
912926
while True:
913-
try:
914-
output = subprocess.check_output(args=["docker",
915-
"exec", self._dockerCid,
916-
"cat", omc_file_port],
917-
stderr=subprocess.DEVNULL)
918-
port = output.decode().strip()
919-
except subprocess.CalledProcessError:
920-
pass
927+
omc_portfile_path = self._get_portfile_path()
928+
if omc_portfile_path is not None:
929+
try:
930+
output = subprocess.check_output(args=["docker",
931+
"exec", self._dockerCid,
932+
"cat", omc_portfile_path.as_posix()],
933+
stderr=subprocess.DEVNULL)
934+
port = output.decode().strip()
935+
except subprocess.CalledProcessError:
936+
pass
921937

922938
if port is not None:
923939
break
924940

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

0 commit comments

Comments
 (0)