@@ -972,3 +972,79 @@ def _omc_docker_start(self) -> Tuple[subprocess.Popen, DummyPopen]:
972972 f"/ { self ._dockerCid } . Log-file says:\n { self .get_log ()} " )
973973
974974 return omc_process , docker_process
975+
976+
977+ class OMCProcessWSL (OMCProcess ):
978+
979+ def __init__ (
980+ self ,
981+ timeout : float = 10.00 ,
982+ wsl_omc : str = 'omc' ,
983+ wsl_distribution : Optional [str ] = None ,
984+ wsl_user : Optional [str ] = None ,
985+ ) -> None :
986+
987+ super ().__init__ (timeout = timeout )
988+
989+ # get wsl base command
990+ self ._wsl_cmd = ['wsl' ]
991+ if isinstance (wsl_distribution , str ):
992+ self ._wsl_cmd += ['--distribution' , wsl_distribution ]
993+ if isinstance (wsl_user , str ):
994+ self ._wsl_cmd += ['--user' , wsl_user ]
995+ self ._wsl_cmd += ['--' ]
996+
997+ # where to find OpenModelica
998+ self ._wsl_omc = wsl_omc
999+ # start up omc executable, which is waiting for the ZMQ connection
1000+ self ._omc_process = self ._omc_process_get ()
1001+ # connect to the running omc instance using ZMQ
1002+ self ._omc_port = self ._omc_port_get ()
1003+
1004+ def _omc_process_get (self ) -> subprocess .Popen :
1005+ my_env = os .environ .copy ()
1006+
1007+ omc_command = self ._wsl_cmd + [
1008+ self ._wsl_omc ,
1009+ "--locale=C" ,
1010+ "--interactive=zmq" ,
1011+ f"-z={ self ._random_string } " ]
1012+
1013+ omc_process = subprocess .Popen (omc_command ,
1014+ stdout = self ._omc_loghandle ,
1015+ stderr = self ._omc_loghandle ,
1016+ env = my_env )
1017+ return omc_process
1018+
1019+ def _omc_port_get (self ) -> str :
1020+ omc_portfile_path : Optional [pathlib .Path ] = None
1021+ port = None
1022+
1023+ # See if the omc server is running
1024+ attempts = 0
1025+ while True :
1026+ try :
1027+ omc_portfile_path = self ._get_portfile_path ()
1028+ if omc_portfile_path is not None :
1029+ output = subprocess .check_output (
1030+ args = self ._wsl_cmd + ["cat" , omc_portfile_path .as_posix ()],
1031+ stderr = subprocess .DEVNULL ,
1032+ )
1033+ port = output .decode ().strip ()
1034+ except subprocess .CalledProcessError :
1035+ pass
1036+
1037+ if port is not None :
1038+ break
1039+
1040+ attempts += 1
1041+ if attempts == 80.0 :
1042+ raise OMCSessionException (f"WSL based OMC Server did not start (timeout={ self ._timeout } ). "
1043+ f"Could not open port file { omc_portfile_path } . "
1044+ f"Log-file says:\n { self .get_log ()} " )
1045+ time .sleep (self ._timeout / 80.0 )
1046+
1047+ logger .info (f"WSL based OMC Server is up and running at ZMQ port { port } "
1048+ f"pid={ self ._omc_process .pid if isinstance (self ._omc_process , subprocess .Popen ) else '?' } " )
1049+
1050+ return port
0 commit comments