@@ -456,22 +456,22 @@ def __init__(
456456 self ._currentUser = "nobody"
457457
458458 # omc port and log file
459- if sys .platform == 'win32' :
460- self ._omc_file_port = f"openmodelica.port.{ self ._random_string } "
461- else :
462- self ._omc_file_port = f"openmodelica.{ self ._currentUser } .port.{ self ._random_string } "
459+ self ._omc_filebase = f"openmodelica.{ self ._random_string } "
463460
464461 # get a temporary directory
465462 self ._temp_dir = pathlib .Path (tempfile .gettempdir ())
466463
467464 # setup log file - this file must be closed in the destructor
468- logfile = self ._temp_dir / (self ._omc_file_port + ' .log' )
465+ logfile = self ._temp_dir / (self ._omc_filebase + " .log" )
469466 self ._omc_loghandle : Optional [io .TextIOWrapper ] = None
470467 try :
471468 self ._omc_loghandle = open (file = logfile , mode = "w+" , encoding = "utf-8" )
472469 except OSError as ex :
473470 raise OMCSessionException (f"Cannot open log file { logfile } ." ) from ex
474471
472+ self ._re_portfile_path = re .compile (pattern = r'\nDumped server port in file: (.*?)($|\n)' ,
473+ flags = re .MULTILINE | re .DOTALL )
474+
475475 def __del__ (self ):
476476 if self ._omc_loghandle is not None :
477477 try :
@@ -506,6 +506,17 @@ def get_log(self) -> str:
506506
507507 return log
508508
509+ def _get_portfile_path (self ) -> Optional [pathlib .Path ]:
510+ omc_log = self .get_log ()
511+
512+ portfile = self ._re_portfile_path .findall (string = omc_log )
513+
514+ portfile_path = None
515+ if portfile :
516+ portfile_path = pathlib .Path (portfile [- 1 ][0 ])
517+
518+ return portfile_path
519+
509520
510521class OMCProcessPort (OMCProcess ):
511522
@@ -574,11 +585,11 @@ def _omc_port_get(self) -> str:
574585 # See if the omc server is running
575586 attempts = 0
576587 while True :
577- omc_file_port = self ._temp_dir / self . _omc_file_port
588+ omc_portfile_path = self ._get_portfile_path ()
578589
579- if omc_file_port .is_file ():
590+ if omc_portfile_path is not None and omc_portfile_path .is_file ():
580591 # Read the port file
581- with open (file = omc_file_port , mode = 'r' , encoding = "utf-8" ) as f_p :
592+ with open (file = omc_portfile_path , mode = 'r' , encoding = "utf-8" ) as f_p :
582593 port = f_p .readline ()
583594 break
584595
@@ -588,7 +599,7 @@ def _omc_port_get(self) -> str:
588599 attempts += 1
589600 if attempts == 80.0 :
590601 raise OMCSessionException (f"OMC Server did not start (timeout={ self ._timeout } ). "
591- f"Could not open file { omc_file_port } . "
602+ f"Could not open file { omc_portfile_path } . "
592603 f"Log-file says:\n { self .get_log ()} " )
593604 time .sleep (self ._timeout / 80.0 )
594605
@@ -742,7 +753,7 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
742753 raise OMCSessionException (f'dockerNetwork was set to { self ._dockerNetwork } , '
743754 'but only \" host\" or \" separate\" is allowed' )
744755
745- self ._dockerCidFile = self ._temp_dir / (self ._omc_file_port + ".docker.cid" )
756+ self ._dockerCidFile = self ._temp_dir / (self ._omc_filebase + ".docker.cid" )
746757
747758 if isinstance (self ._interactivePort , int ):
748759 extraFlags = extraFlags + [f"--interactivePort={ int (self ._interactivePort )} " ]
@@ -761,7 +772,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
761772 return omc_command
762773
763774 def _omc_port_get (self ) -> str :
764- omc_file_port = '/tmp/' + self ._omc_file_port
765775 port = None
766776
767777 if not isinstance (self ._dockerCid , str ):
@@ -770,22 +780,24 @@ def _omc_port_get(self) -> str:
770780 # See if the omc server is running
771781 attempts = 0
772782 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
783+ omc_portfile_path = self ._get_portfile_path ()
784+ if omc_portfile_path is not None :
785+ try :
786+ output = subprocess .check_output (args = ["docker" ,
787+ "exec" , self ._dockerCid ,
788+ "cat" , omc_portfile_path .as_posix ()],
789+ stderr = subprocess .DEVNULL )
790+ port = output .decode ().strip ()
791+ except subprocess .CalledProcessError :
792+ pass
781793
782794 if port is not None :
783795 break
784796
785797 attempts += 1
786798 if attempts == 80.0 :
787799 raise OMCSessionException (f"Docker based OMC Server did not start (timeout={ self ._timeout } ). "
788- f"Could not open file { omc_file_port } . "
800+ f"Could not open port file { omc_portfile_path } . "
789801 f"Log-file says:\n { self .get_log ()} " )
790802 time .sleep (self ._timeout / 80.0 )
791803
@@ -903,7 +915,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
903915 return omc_command
904916
905917 def _omc_port_get (self ) -> str :
906- omc_file_port = '/tmp/' + self ._omc_file_port
907918 port = None
908919
909920 if not isinstance (self ._dockerCid , str ):
@@ -912,22 +923,24 @@ def _omc_port_get(self) -> str:
912923 # See if the omc server is running
913924 attempts = 0
914925 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
926+ omc_portfile_path = self ._get_portfile_path ()
927+ if omc_portfile_path is not None :
928+ try :
929+ output = subprocess .check_output (args = ["docker" ,
930+ "exec" , self ._dockerCid ,
931+ "cat" , omc_portfile_path .as_posix ()],
932+ stderr = subprocess .DEVNULL )
933+ port = output .decode ().strip ()
934+ except subprocess .CalledProcessError :
935+ pass
923936
924937 if port is not None :
925938 break
926939
927940 attempts += 1
928941 if attempts == 80.0 :
929942 raise OMCSessionException (f"Docker container based OMC Server did not start (timeout={ self ._timeout } ). "
930- f"Could not open file { omc_file_port } . "
943+ f"Could not open port file { omc_portfile_path } . "
931944 f"Log-file says:\n { self .get_log ()} " )
932945 time .sleep (self ._timeout / 80.0 )
933946
0 commit comments