@@ -456,22 +456,22 @@ def __init__(
456
456
self ._currentUser = "nobody"
457
457
458
458
# 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 } "
463
460
464
461
# get a temporary directory
465
462
self ._temp_dir = pathlib .Path (tempfile .gettempdir ())
466
463
467
464
# 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" )
469
466
self ._omc_loghandle : Optional [io .TextIOWrapper ] = None
470
467
try :
471
468
self ._omc_loghandle = open (file = logfile , mode = "w+" , encoding = "utf-8" )
472
469
except OSError as ex :
473
470
raise OMCSessionException (f"Cannot open log file { logfile } ." ) from ex
474
471
472
+ self ._re_portfile_path = re .compile (pattern = r'\nDumped server port in file: (.*?)($|\n)' ,
473
+ flags = re .MULTILINE | re .DOTALL )
474
+
475
475
def __del__ (self ):
476
476
if self ._omc_loghandle is not None :
477
477
try :
@@ -506,6 +506,17 @@ def get_log(self) -> str:
506
506
507
507
return log
508
508
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
+
509
520
510
521
class OMCProcessPort (OMCProcess ):
511
522
@@ -574,11 +585,11 @@ def _omc_port_get(self) -> str:
574
585
# See if the omc server is running
575
586
attempts = 0
576
587
while True :
577
- omc_file_port = self ._temp_dir / self . _omc_file_port
588
+ omc_portfile_path = self ._get_portfile_path ()
578
589
579
- if omc_file_port .is_file ():
590
+ if omc_portfile_path is not None and omc_portfile_path .is_file ():
580
591
# 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 :
582
593
port = f_p .readline ()
583
594
break
584
595
@@ -588,7 +599,7 @@ def _omc_port_get(self) -> str:
588
599
attempts += 1
589
600
if attempts == 80.0 :
590
601
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 } . "
592
603
f"Log-file says:\n { self .get_log ()} " )
593
604
time .sleep (self ._timeout / 80.0 )
594
605
@@ -742,7 +753,7 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
742
753
raise OMCSessionException (f'dockerNetwork was set to { self ._dockerNetwork } , '
743
754
'but only \" host\" or \" separate\" is allowed' )
744
755
745
- self ._dockerCidFile = self ._temp_dir / (self ._omc_file_port + ".docker.cid" )
756
+ self ._dockerCidFile = self ._temp_dir / (self ._omc_filebase + ".docker.cid" )
746
757
747
758
if isinstance (self ._interactivePort , int ):
748
759
extraFlags = extraFlags + [f"--interactivePort={ int (self ._interactivePort )} " ]
@@ -761,7 +772,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
761
772
return omc_command
762
773
763
774
def _omc_port_get (self ) -> str :
764
- omc_file_port = '/tmp/' + self ._omc_file_port
765
775
port = None
766
776
767
777
if not isinstance (self ._dockerCid , str ):
@@ -770,22 +780,24 @@ def _omc_port_get(self) -> str:
770
780
# See if the omc server is running
771
781
attempts = 0
772
782
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
781
793
782
794
if port is not None :
783
795
break
784
796
785
797
attempts += 1
786
798
if attempts == 80.0 :
787
799
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 } . "
789
801
f"Log-file says:\n { self .get_log ()} " )
790
802
time .sleep (self ._timeout / 80.0 )
791
803
@@ -903,7 +915,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
903
915
return omc_command
904
916
905
917
def _omc_port_get (self ) -> str :
906
- omc_file_port = '/tmp/' + self ._omc_file_port
907
918
port = None
908
919
909
920
if not isinstance (self ._dockerCid , str ):
@@ -912,22 +923,24 @@ def _omc_port_get(self) -> str:
912
923
# See if the omc server is running
913
924
attempts = 0
914
925
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
923
936
924
937
if port is not None :
925
938
break
926
939
927
940
attempts += 1
928
941
if attempts == 80.0 :
929
942
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 } . "
931
944
f"Log-file says:\n { self .get_log ()} " )
932
945
time .sleep (self ._timeout / 80.0 )
933
946
0 commit comments