From 89504dc33c8207aa49740e7d994e0e1116d08a46 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Thu, 21 Nov 2024 13:04:30 +0100 Subject: [PATCH] exit the interactive OMPython sessions --- testmodel.py | 57 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/testmodel.py b/testmodel.py index b6e2979..802f2e4 100755 --- a/testmodel.py +++ b/testmodel.py @@ -54,7 +54,21 @@ def writeResult(): startJob=monotonic() -def writeResultAndExit(exitStatus, useOsExit=False): +def quit_omc(omc): + if omc is None: + return omc + try: + omc.sendExpression("quit()") + except: + pass + try: + del omc + except: + pass + omc = None + return omc + +def writeResultAndExit(exitStatus, useOsExit=False, omc=None, omc_new=None): writeResult() print("Calling exit ...") with open(errFile, 'a+') as fp: @@ -65,6 +79,8 @@ def writeResultAndExit(exitStatus, useOsExit=False): fp.write(msg % (exitStatus, monotonic()-startJob)) fp.flush() sys.stdout.flush() + omc = quit_omc(omc) + omc_new = quit(omc_new) if useOsExit: os._exit(exitStatus) else: @@ -117,7 +133,7 @@ def target(res): pass with open(errFile, 'a+') as fp: fp.write("Aborted the command.\n") - writeResultAndExit(0, True) + writeResultAndExit(0, True, omc, omc_new) if res[1] is None: res[1] = "" if res[1] is not None: @@ -311,7 +327,7 @@ def createOmcSessionNew(): def loadModels(omc, conf): for f in conf["loadFiles"]: if not sendExpressionTimeout(omc, 'loadFile("%s", uses=false)' % f, conf["ulimitLoadModel"]): - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) loadedFiles = sorted(omc.sendExpression("{getSourceFile(cl) for cl in getClassNames()}")) if sorted(conf["loadFiles"]) != loadedFiles: print("Loaded the wrong files. Expected:\n%s\nActual:\n%s" % ("\n".join(sorted(conf["loadFiles"])), "\n".join(loadedFiles))) @@ -333,7 +349,7 @@ def loadLibraryInNewOM(): execstat["parsing"]=monotonic()-start with open(errFile, 'a+') as fp: fp.write("Timeout error for cmd: %s\n%s"%(cmd,str(e))) - writeResultAndExit(0, True) + writeResultAndExit(0, True, omc, omc_new) execstat["parsing"]=monotonic()-start try: @@ -399,7 +415,7 @@ def sendExpressionOldOrNew(cmd): except: pass - writeResultAndExit(0) + writeResultAndExit(0, omc, omc_new) # See which translateModel phases completed @@ -413,14 +429,7 @@ def sendExpressionOldOrNew(cmd): frontend = omc.sendExpression("OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_FRONTEND)") writeResult() -try: - omc.sendExpression("quit()") -except: - pass -try: - del omc -except: - pass +omc = quit_omc(omc) print(execTimeTranslateModel,frontend,backend) if backend != -1: @@ -452,7 +461,7 @@ def sendExpressionOldOrNew(cmd): fp.write(err) if execstat["phase"] < 4: - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) start=monotonic() try: @@ -463,7 +472,7 @@ def sendExpressionOldOrNew(cmd): if not os.path.exists(os.path.normpath(fmuExpectedLocation)): err += "\nFMU was not generated in the expected location: %s" % fmuExpectedLocation execstat["phase"]=4 - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) execstat["phase"] = 5 else: if isWin: @@ -477,7 +486,7 @@ def sendExpressionOldOrNew(cmd): execstat["build"] = monotonic()-start with open(errFile, 'a+') as fp: fp.write(str(e)) - writeResultAndExit(0, True) + writeResultAndExit(0, True, omc, omc_new) writeResult() # Do the simulation @@ -493,7 +502,7 @@ def sendExpressionOldOrNew(cmd): if not conf.get("fmisimulator"): with open(simFile,"w") as fp: fp.write("OMSimulator not available\n") - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) fmitmpdir = "temp_%s_fmu" % conf["fileName"].replace(".","_") with open("%s.tmpfiles" % conf["fileName"], "a+") as fp: fp.write("%s\n" % fmitmpdir) @@ -527,20 +536,19 @@ def sendExpressionOldOrNew(cmd): execstat["phase"] = 6 except TimeoutError as e: execstat["sim"] = monotonic()-start - writeResultAndExit(0, True) + writeResultAndExit(0, True, omc, omc_new) if referenceFile=="": - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) if len(referenceVars)==0: execstat["diff"] = {"time":0.0, "vars":[], "numCompared":0} execstat["phase"]=7 - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) # Check the reference file... prefix = os.path.abspath("../files/%s.diff" % conf["fileName"]).replace('\\','/') - if not os.path.exists(os.path.normpath(resFile)): with open(errFile, 'a+') as fp: fp.write("TODO: How the !@#!# did the simulation report success but simulation result %s does not exist to compare? outputFormat=%s" % (resFile,outputFormat)) @@ -551,7 +559,7 @@ def sendExpressionOldOrNew(cmd): if not sendExpressionTimeout(omc_new, 'filterSimulationResults("%s", "updated%s", vars={%s}, removeDescription=false, hintReadAllVars=false)' % (resFile, resFile, ", ".join(['"%s"' % s for s in referenceVars])), conf["ulimitOmc"]): with open(errFile, 'a+') as fp: fp.write("Failed to filter simulation results. Took time: %.2f\n" % (monotonic()-start)) - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) os.remove(resFile) os.rename("updated" + resFile, resFile) with open(errFile, 'a+') as fp: @@ -563,7 +571,7 @@ def sendExpressionOldOrNew(cmd): except TimeoutError as e: with open(errFile, 'a+') as fp: fp.write("Timeout error for diffSimulationResults") - writeResultAndExit(0) + writeResultAndExit(0, False, omc, omc_new) execstat["diff"] = {"time":monotonic()-start, "vars":[], "numCompared":len(referenceVars)} if len(diffVars)==0 and referenceOK: @@ -635,4 +643,7 @@ def sendExpressionOldOrNew(cmd): """ % (tolerance, conf["reference_reltolDiffMinMax"], conf["reference_rangeDelta"], os.path.basename(prefix + "." + var + ".csv"), var)) +# quit omc_new +omc_new = quit_omc(omc_new) + writeResultAndExit(0)