Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,22 +456,22 @@ def __init__(
self._currentUser = "nobody"

# omc port and log file
if sys.platform == 'win32':
self._omc_file_port = f"openmodelica.port.{self._random_string}"
else:
self._omc_file_port = f"openmodelica.{self._currentUser}.port.{self._random_string}"
self._omc_filebase = f"openmodelica.{self._random_string}"

# get a temporary directory
self._temp_dir = pathlib.Path(tempfile.gettempdir())

# setup log file - this file must be closed in the destructor
logfile = self._temp_dir / (self._omc_file_port + '.log')
logfile = self._temp_dir / (self._omc_filebase + ".log")
self._omc_loghandle: Optional[io.TextIOWrapper] = None
try:
self._omc_loghandle = open(file=logfile, mode="w+", encoding="utf-8")
except OSError as ex:
raise OMCSessionException(f"Cannot open log file {logfile}.") from ex

self._re_portfile_path = re.compile(pattern=r'\nDumped server port in file: (.*?)($|\n)',
flags=re.MULTILINE | re.DOTALL)

def __del__(self):
if self._omc_loghandle is not None:
try:
Expand Down Expand Up @@ -506,6 +506,17 @@ def get_log(self) -> str:

return log

def _get_portfile_path(self) -> Optional[pathlib.Path]:
omc_log = self.get_log()

portfile = self._re_portfile_path.findall(string=omc_log)

portfile_path = None
if portfile:
portfile_path = pathlib.Path(portfile[-1][0])

return portfile_path


class OMCProcessPort(OMCProcess):

Expand Down Expand Up @@ -574,11 +585,11 @@ def _omc_port_get(self) -> str:
# See if the omc server is running
attempts = 0
while True:
omc_file_port = self._temp_dir / self._omc_file_port
omc_portfile_path = self._get_portfile_path()

if omc_file_port.is_file():
if omc_portfile_path is not None and omc_portfile_path.is_file():
# Read the port file
with open(file=omc_file_port, mode='r', encoding="utf-8") as f_p:
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
port = f_p.readline()
break

Expand All @@ -588,7 +599,7 @@ def _omc_port_get(self) -> str:
attempts += 1
if attempts == 80.0:
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
f"Could not open file {omc_file_port}. "
f"Could not open file {omc_portfile_path}. "
f"Log-file says:\n{self.get_log()}")
time.sleep(self._timeout / 80.0)

Expand Down Expand Up @@ -742,7 +753,7 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
raise OMCSessionException(f'dockerNetwork was set to {self._dockerNetwork}, '
'but only \"host\" or \"separate\" is allowed')

self._dockerCidFile = self._temp_dir / (self._omc_file_port + ".docker.cid")
self._dockerCidFile = self._temp_dir / (self._omc_filebase + ".docker.cid")

if isinstance(self._interactivePort, int):
extraFlags = extraFlags + [f"--interactivePort={int(self._interactivePort)}"]
Expand All @@ -761,7 +772,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
return omc_command

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

if not isinstance(self._dockerCid, str):
Expand All @@ -770,22 +780,24 @@ def _omc_port_get(self) -> str:
# See if the omc server is running
attempts = 0
while True:
try:
output = subprocess.check_output(args=["docker",
"exec", self._dockerCid,
"cat", omc_file_port],
stderr=subprocess.DEVNULL)
port = output.decode().strip()
except subprocess.CalledProcessError:
pass
omc_portfile_path = self._get_portfile_path()
if omc_portfile_path is not None:
try:
output = subprocess.check_output(args=["docker",
"exec", self._dockerCid,
"cat", omc_portfile_path.as_posix()],
stderr=subprocess.DEVNULL)
port = output.decode().strip()
except subprocess.CalledProcessError:
pass

if port is not None:
break

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

Expand Down Expand Up @@ -903,7 +915,6 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
return omc_command

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

if not isinstance(self._dockerCid, str):
Expand All @@ -912,22 +923,24 @@ def _omc_port_get(self) -> str:
# See if the omc server is running
attempts = 0
while True:
try:
output = subprocess.check_output(args=["docker",
"exec", self._dockerCid,
"cat", omc_file_port],
stderr=subprocess.DEVNULL)
port = output.decode().strip()
except subprocess.CalledProcessError:
pass
omc_portfile_path = self._get_portfile_path()
if omc_portfile_path is not None:
try:
output = subprocess.check_output(args=["docker",
"exec", self._dockerCid,
"cat", omc_portfile_path.as_posix()],
stderr=subprocess.DEVNULL)
port = output.decode().strip()
except subprocess.CalledProcessError:
pass

if port is not None:
break

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

Expand Down
Loading