forked from OpenModelica/OMPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOMCSession.py
More file actions
560 lines (467 loc) · 23.5 KB
/
OMCSession.py
File metadata and controls
560 lines (467 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# -*- coding: utf-8 -*-
"""
Definition of an OMC session.
"""
from __future__ import annotations
__license__ = """
This file is part of OpenModelica.
Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
c/o Linköpings universitet, Department of Computer and Information Science,
SE-58183 Linköping, Sweden.
All rights reserved.
THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
ACCORDING TO RECIPIENTS CHOICE.
The OpenModelica software and the OSMC (Open Source Modelica Consortium)
Public License (OSMC-PL) are obtained from OSMC, either from the above
address, from the URLs: http://www.openmodelica.org or
http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
distribution. GNU version 3 is obtained from:
http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
http://www.opensource.org/licenses/BSD-3-Clause.
This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
CONDITIONS OF OSMC-PL.
"""
import shutil
import getpass
import logging
import json
import os
import pathlib
import psutil
import signal
import subprocess
import sys
import tempfile
import time
from typing import Optional
import uuid
import pyparsing
import zmq
import warnings
# TODO: replace this with the new parser
from OMPython.OMTypedParser import parseString as om_parser_typed
from OMPython.OMParser import om_parser_basic
# define logger using the current module name as ID
logger = logging.getLogger(__name__)
class DummyPopen():
def __init__(self, pid):
self.pid = pid
self.process = psutil.Process(pid)
self.returncode = 0
def poll(self):
return None if self.process.is_running() else True
def kill(self):
return os.kill(self.pid, signal.SIGKILL)
def wait(self, timeout):
return self.process.wait(timeout=timeout)
class OMCSessionException(Exception):
pass
class OMCSessionCmd:
def __init__(self, session: OMCSessionZMQ, readonly: Optional[bool] = False):
if not isinstance(session, OMCSessionZMQ):
raise OMCSessionException("Invalid session definition!")
self._session = session
self._readonly = readonly
self._omc_cache = {}
def _ask(self, question: str, opt: Optional[list[str]] = None, parsed: Optional[bool] = True):
if opt is None:
expression = question
elif isinstance(opt, list):
expression = f"{question}({','.join([str(x) for x in opt])})"
else:
raise OMCSessionException(f"Invalid definition of options for {repr(question)}: {repr(opt)}")
p = (expression, parsed)
if self._readonly and question != 'getErrorString':
# can use cache if readonly
if p in self._omc_cache:
return self._omc_cache[p]
logger.debug('OMC ask: %s (parsed=%s)', expression, parsed)
try:
res = self._session.sendExpression(expression, parsed=parsed)
except OMCSessionException as ex:
raise OMCSessionException("OMC _ask() failed: %s (parsed=%s)", expression, parsed) from ex
# save response
self._omc_cache[p] = res
return res
# TODO: Open Modelica Compiler API functions. Would be nice to generate these.
def loadFile(self, filename):
return self._ask(question='loadFile', opt=[f'"{filename}"'])
def loadModel(self, className):
return self._ask(question='loadModel', opt=[className])
def isModel(self, className):
return self._ask(question='isModel', opt=[className])
def isPackage(self, className):
return self._ask(question='isPackage', opt=[className])
def isPrimitive(self, className):
return self._ask(question='isPrimitive', opt=[className])
def isConnector(self, className):
return self._ask(question='isConnector', opt=[className])
def isRecord(self, className):
return self._ask(question='isRecord', opt=[className])
def isBlock(self, className):
return self._ask(question='isBlock', opt=[className])
def isType(self, className):
return self._ask(question='isType', opt=[className])
def isFunction(self, className):
return self._ask(question='isFunction', opt=[className])
def isClass(self, className):
return self._ask(question='isClass', opt=[className])
def isParameter(self, className):
return self._ask(question='isParameter', opt=[className])
def isConstant(self, className):
return self._ask(question='isConstant', opt=[className])
def isProtected(self, className):
return self._ask(question='isProtected', opt=[className])
def getPackages(self, className="AllLoadedClasses"):
return self._ask(question='getPackages', opt=[className])
def getClassRestriction(self, className):
return self._ask(question='getClassRestriction', opt=[className])
def getDerivedClassModifierNames(self, className):
return self._ask(question='getDerivedClassModifierNames', opt=[className])
def getDerivedClassModifierValue(self, className, modifierName):
return self._ask(question='getDerivedClassModifierValue', opt=[className, modifierName])
def typeNameStrings(self, className):
return self._ask(question='typeNameStrings', opt=[className])
def getComponents(self, className):
return self._ask(question='getComponents', opt=[className])
def getClassComment(self, className):
try:
return self._ask(question='getClassComment', opt=[className])
except pyparsing.ParseException as ex:
logger.warning("Method 'getClassComment(%s)' failed; OMTypedParser error: %s",
className, ex.msg)
return 'No description available'
except OMCSessionException:
raise
def getNthComponent(self, className, comp_id):
""" returns with (type, name, description) """
return self._ask(question='getNthComponent', opt=[className, comp_id])
def getNthComponentAnnotation(self, className, comp_id):
return self._ask(question='getNthComponentAnnotation', opt=[className, comp_id])
def getImportCount(self, className):
return self._ask(question='getImportCount', opt=[className])
def getNthImport(self, className, importNumber):
# [Path, id, kind]
return self._ask(question='getNthImport', opt=[className, importNumber])
def getInheritanceCount(self, className):
return self._ask(question='getInheritanceCount', opt=[className])
def getNthInheritedClass(self, className, inheritanceDepth):
return self._ask(question='getNthInheritedClass', opt=[className, inheritanceDepth])
def getParameterNames(self, className):
try:
return self._ask(question='getParameterNames', opt=[className])
except KeyError as ex:
logger.warning('OMPython error: %s', ex)
# FIXME: OMC returns with a different structure for empty parameter set
return []
except OMCSessionException:
raise
def getParameterValue(self, className, parameterName):
try:
return self._ask(question='getParameterValue', opt=[className, parameterName])
except pyparsing.ParseException as ex:
logger.warning("Method 'getParameterValue(%s, %s)' failed; OMTypedParser error: %s",
className, parameterName, ex.msg)
return ""
except OMCSessionException:
raise
def getComponentModifierNames(self, className, componentName):
return self._ask(question='getComponentModifierNames', opt=[className, componentName])
def getComponentModifierValue(self, className, componentName):
return self._ask(question='getComponentModifierValue', opt=[className, componentName])
def getExtendsModifierNames(self, className, componentName):
return self._ask(question='getExtendsModifierNames', opt=[className, componentName])
def getExtendsModifierValue(self, className, extendsName, modifierName):
return self._ask(question='getExtendsModifierValue', opt=[className, extendsName, modifierName])
def getNthComponentModification(self, className, comp_id):
# FIXME: OMPython exception Results KeyError exception
# get {$Code(....)} field
# \{\$Code\((\S*\s*)*\)\}
value = self._ask(question='getNthComponentModification', opt=[className, comp_id], parsed=False)
value = value.replace("{$Code(", "")
return value[:-3]
# return self.re_Code.findall(value)
# function getClassNames
# input TypeName class_ = $Code(AllLoadedClasses);
# input Boolean recursive = false;
# input Boolean qualified = false;
# input Boolean sort = false;
# input Boolean builtin = false "List also builtin classes if true";
# input Boolean showProtected = false "List also protected classes if true";
# output TypeName classNames[:];
# end getClassNames;
def getClassNames(self, className=None, recursive=False, qualified=False, sort=False, builtin=False,
showProtected=False):
opt = [className] if className else [] + [f'recursive={str(recursive).lower()}',
f'qualified={str(qualified).lower()}',
f'sort={str(sort).lower()}',
f'builtin={str(builtin).lower()}',
f'showProtected={str(showProtected).lower()}']
return self._ask(question='getClassNames', opt=opt)
class OMCSessionZMQ:
def __init__(self, timeout=10.00,
docker=None, dockerContainer=None, dockerExtraArgs=None, dockerOpenModelicaPath="omc",
dockerNetwork=None, port=None, omhome: str = None):
if dockerExtraArgs is None:
dockerExtraArgs = []
self.omhome = self._get_omhome(omhome=omhome)
self._omc_process = None
self._omc_command = None
self._omc = None
self._dockerCid = None
self._serverIPAddress = "127.0.0.1"
self._interactivePort = None
# FIXME: this code is not well written... need to be refactored
self._temp_dir = pathlib.Path(tempfile.gettempdir())
# generate a random string for this session
self._random_string = uuid.uuid4().hex
# omc log file
self._omc_log_file = None
try:
self._currentUser = getpass.getuser()
if not self._currentUser:
self._currentUser = "nobody"
except KeyError:
# We are running as a uid not existing in the password database... Pretend we are nobody
self._currentUser = "nobody"
# Locating and using the IOR
if sys.platform != 'win32' or docker or dockerContainer:
self._port_file = "openmodelica." + self._currentUser + ".port." + self._random_string
else:
self._port_file = "openmodelica.port." + self._random_string
self._docker = docker
self._dockerContainer = dockerContainer
self._dockerExtraArgs = dockerExtraArgs
self._dockerOpenModelicaPath = dockerOpenModelicaPath
self._dockerNetwork = dockerNetwork
self._create_omc_log_file("port")
self._timeout = timeout
self._port_file = ((pathlib.Path("/tmp") if docker else self._temp_dir) / self._port_file).as_posix()
self._interactivePort = port
# set omc executable path and args
self._set_omc_command([
"--interactive=zmq",
"--locale=C",
f"-z={self._random_string}"
])
# start up omc executable, which is waiting for the ZMQ connection
self._start_omc_process(timeout)
# connect to the running omc instance using ZMQ
self._connect_to_omc(timeout)
def __del__(self):
try:
self.sendExpression("quit()")
except OMCSessionException:
pass
self._omc_log_file.close()
try:
self._omc_process.wait(timeout=2.0)
except subprocess.TimeoutExpired:
if self._omc_process:
logger.warning("OMC did not exit after being sent the quit() command; "
"killing the process with pid=%s", self._omc_process.pid)
self._omc_process.kill()
self._omc_process.wait()
def _create_omc_log_file(self, suffix):
if sys.platform == 'win32':
log_filename = f"openmodelica.{suffix}.{self._random_string}.log"
else:
log_filename = f"openmodelica.{self._currentUser}.{suffix}.{self._random_string}.log"
# this file must be closed in the destructor
self._omc_log_file = open(self._temp_dir / log_filename, "w+")
def _start_omc_process(self, timeout):
if sys.platform == 'win32':
omhome_bin = (self.omhome / "bin").as_posix()
my_env = os.environ.copy()
my_env["PATH"] = omhome_bin + os.pathsep + my_env["PATH"]
self._omc_process = subprocess.Popen(self._omc_command, stdout=self._omc_log_file,
stderr=self._omc_log_file, env=my_env)
else:
# set the user environment variable so omc running from wsgi has the same user as OMPython
my_env = os.environ.copy()
my_env["USER"] = self._currentUser
self._omc_process = subprocess.Popen(self._omc_command, stdout=self._omc_log_file,
stderr=self._omc_log_file, env=my_env)
if self._docker:
for i in range(0, 40):
try:
with open(self._dockerCidFile, "r") as fin:
self._dockerCid = fin.read().strip()
except IOError:
pass
if self._dockerCid:
break
time.sleep(timeout / 40.0)
try:
os.remove(self._dockerCidFile)
except FileNotFoundError:
pass
if self._dockerCid is None:
logger.error("Docker did not start. Log-file says:\n%s" % (open(self._omc_log_file.name).read()))
raise OMCSessionException("Docker did not start (timeout=%f might be too short especially if you did "
"not docker pull the image before this command)." % timeout)
dockerTop = None
if self._docker or self._dockerContainer:
if self._dockerNetwork == "separate":
self._serverIPAddress = json.loads(subprocess.check_output(["docker", "inspect", self._dockerCid]).decode().strip())[0]["NetworkSettings"]["IPAddress"]
for i in range(0, 40):
if sys.platform == 'win32':
break
dockerTop = subprocess.check_output(["docker", "top", self._dockerCid]).decode().strip()
self._omc_process = None
for line in dockerTop.split("\n"):
columns = line.split()
if self._random_string in line:
try:
self._omc_process = DummyPopen(int(columns[1]))
except psutil.NoSuchProcess:
raise OMCSessionException(
f"Could not find PID {dockerTop} - is this a docker instance spawned "
f"without --pid=host?\nLog-file says:\n{open(self._omc_log_file.name).read()}")
break
if self._omc_process is not None:
break
time.sleep(timeout / 40.0)
if self._omc_process is None:
raise OMCSessionException("Docker top did not contain omc process %s:\n%s\nLog-file says:\n%s"
% (self._random_string, dockerTop, open(self._omc_log_file.name).read()))
return self._omc_process
def _getuid(self):
"""
The uid to give to docker.
On Windows, volumes are mapped with all files are chmod ugo+rwx,
so uid does not matter as long as it is not the root user.
"""
return 1000 if sys.platform == 'win32' else os.getuid()
def _set_omc_command(self, omc_path_and_args_list):
"""Define the command that will be called by the subprocess module.
On Windows, use the list input style of the subprocess module to
avoid problems resulting from spaces in the path string.
Linux, however, only works with the string version.
"""
if (self._docker or self._dockerContainer) and sys.platform == "win32":
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
if not self._interactivePort:
raise OMCSessionException("docker on Windows requires knowing which port to connect to. For "
"dockerContainer=..., the container needs to have already manually exposed "
"this port when it was started (-p 127.0.0.1:n:n) or you get an error later.")
else:
extraFlags = []
if self._docker:
if sys.platform == "win32":
p = int(self._interactivePort)
dockerNetworkStr = ["-p", "127.0.0.1:%d:%d" % (p, p)]
elif self._dockerNetwork == "host" or self._dockerNetwork is None:
dockerNetworkStr = ["--network=host"]
elif self._dockerNetwork == "separate":
dockerNetworkStr = []
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
else:
raise OMCSessionException('dockerNetwork was set to %s, but only \"host\" or \"separate\" is allowed')
self._dockerCidFile = self._omc_log_file.name + ".docker.cid"
omcCommand = ["docker", "run", "--cidfile", self._dockerCidFile, "--rm", "--env", "USER=%s" % self._currentUser, "--user", str(self._getuid())] + self._dockerExtraArgs + dockerNetworkStr + [self._docker, self._dockerOpenModelicaPath]
elif self._dockerContainer:
omcCommand = ["docker", "exec", "--env", "USER=%s" % self._currentUser, "--user", str(self._getuid())] + self._dockerExtraArgs + [self._dockerContainer, self._dockerOpenModelicaPath]
self._dockerCid = self._dockerContainer
else:
omcCommand = [str(self._get_omc_path())]
if self._interactivePort:
extraFlags = extraFlags + ["--interactivePort=%d" % int(self._interactivePort)]
self._omc_command = omcCommand + omc_path_and_args_list + extraFlags
return self._omc_command
def _get_omhome(self, omhome: str = None):
# use the provided path
if omhome is not None:
return pathlib.Path(omhome)
# check the environment variable
omhome = os.environ.get('OPENMODELICAHOME')
if omhome is not None:
return pathlib.Path(omhome)
# Get the path to the OMC executable, if not installed this will be None
path_to_omc = shutil.which("omc")
if path_to_omc is not None:
return pathlib.Path(path_to_omc).parents[1]
raise OMCSessionException("Cannot find OpenModelica executable, please install from openmodelica.org")
def _get_omc_path(self) -> pathlib.Path:
return self.omhome / "bin" / "omc"
def _connect_to_omc(self, timeout):
self._omc_zeromq_uri = "file:///" + self._port_file
# See if the omc server is running
attempts = 0
self._port = None
while True:
if self._dockerCid:
try:
self._port = subprocess.check_output(["docker", "exec", self._dockerCid, "cat", self._port_file],
stderr=subprocess.DEVNULL).decode().strip()
break
except subprocess.CalledProcessError:
pass
else:
if os.path.isfile(self._port_file):
# Read the port file
with open(self._port_file, 'r') as f_p:
self._port = f_p.readline()
os.remove(self._port_file)
break
attempts += 1
if attempts == 80.0:
name = self._omc_log_file.name
self._omc_log_file.close()
logger.error("OMC Server did not start. Please start it! Log-file says:\n%s" % open(name).read())
raise OMCSessionException(f"OMC Server did not start (timeout={timeout}). "
"Could not open file {self._port_file}")
time.sleep(timeout / 80.0)
self._port = self._port.replace("0.0.0.0", self._serverIPAddress)
logger.info(f"OMC Server is up and running at {self._omc_zeromq_uri} pid={self._omc_process.pid} cid={self._dockerCid}")
# Create the ZeroMQ socket and connect to OMC server
context = zmq.Context.instance()
self._omc = context.socket(zmq.REQ)
self._omc.setsockopt(zmq.LINGER, 0) # Dismisses pending messages if closed
self._omc.setsockopt(zmq.IMMEDIATE, True) # Queue messages only to completed connections
self._omc.connect(self._port)
def execute(self, command):
warnings.warn("This function is depreciated and will be removed in future versions; "
"please use sendExpression() instead", DeprecationWarning, stacklevel=1)
return self.sendExpression(command, parsed=False)
def sendExpression(self, command, parsed=True):
p = self._omc_process.poll() # check if process is running
if p is not None:
raise OMCSessionException("Process Exited, No connection with OMC. Create a new instance of OMCSessionZMQ!")
attempts = 0
while True:
try:
self._omc.send_string(str(command), flags=zmq.NOBLOCK)
break
except zmq.error.Again:
pass
attempts += 1
if attempts >= 50:
self._omc_log_file.seek(0)
log = self._omc_log_file.read()
self._omc_log_file.close()
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}). Log-file says: \n{log}")
time.sleep(self._timeout / 50.0)
if command == "quit()":
self._omc.close()
self._omc = None
return None
else:
result = self._omc.recv_string()
if parsed is True:
try:
return om_parser_typed(result)
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s. Returning the basic parser result.', ex.msg)
try:
return om_parser_basic(result)
except (TypeError, UnboundLocalError) as ex:
logger.warning('OMParser error: %s. Returning the unparsed result.', ex)
return result
else:
return result