diff --git a/conversionscript.py b/conversionscript.py index c2b0fc4..e572cf6 100755 --- a/conversionscript.py +++ b/conversionscript.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from OMPython import OMCSessionZMQ -import pyparsing +from OMPython import OMCSessionZMQ, pyparsing import argparse import glob import json @@ -13,6 +12,7 @@ from multiprocessing import Pool import time + parser = argparse.ArgumentParser(description='OpenModelica library testing tool') parser.add_argument('libdir', nargs=1) parser.add_argument('--diff', action="store_true") @@ -32,6 +32,17 @@ os.mkdir("converted-libraries/.openmodelica") os.mkdir("converted-libraries/.openmodelica/libraries") +def omcAssert(omc, cmd, extra=""): + res = omc.sendExpression(cmd) + if not res: + raise Exception(cmd + "\n" + extra + "\n" + (omc.sendExpression("getErrorString()") or "")) + +def omcSendExpression(omc, cmd, extra=""): + try: + return omc.sendExpression(cmd) + except pyparsing.ParseException as e: + raise Exception(str(e) + "\n" + cmd + "\n" + extra + "\n" + (omc.sendExpression("getErrorString()") or "")) + mslPath = "%s/Modelica 4.0.0+maint.om/" % libdir with open("%s/openmodelica.metadata.json" % mslPath) as f: mslData = json.load(f) @@ -72,7 +83,7 @@ def convertPackage(p): print("Start working on %s" % libnameOnFile) omc = OMCSessionZMQ() libnameOnFileFullPath = "converted-libraries/.openmodelica/libraries/%s/package.mo" % libnameOnFile - omc.sendExpression('loadFile("%s", uses=false)' % libnameOnFileFullPath) + omcAssert(omc, 'loadFile("%s", uses=false)' % libnameOnFileFullPath) errString = omc.sendExpression("getErrorString()") if errString: print(errString) @@ -81,12 +92,12 @@ def convertPackage(p): raise Exception("Expected to have loaded %s but got %s" % (libnameOnFileFullPath, loadedFilePath)) gcProfStatsBeforeConversion = omc.sendExpression("GC_get_prof_stats()") timeBeforeConvert = time.time() - omc.sendExpression('runConversionScript(%s, "%s")' % (libname, conversionScript)) + omcAssert(omc, 'runConversionScript(%s, "%s")' % (libname, conversionScript)) print("runConversionScript(%s, %s) OK" % (libnameOnFile, conversionScript)) uses = data["uses"] for (n,v) in data["uses"].items(): if n in ["Modelica", "ModelicaServices", "Complex"]: - omc.sendExpression('addClassAnnotation(%s, annotate=$annotation(uses(%s(version="4.0.0"))))' % (libname, n)) + omcAssert(omc, 'addClassAnnotation(%s, annotate=$annotation(uses(%s(version="4.0.0"))))' % (libname, n)) data["uses"][n] = "4.0.0" names = omc.sendExpression('getClassNames(%s, sort=true, recursive=true)' % libname) names = list(names) @@ -138,7 +149,7 @@ def convertPackage(p): print(errStr) raise Exception('--allowErrorsInDiff is not active:\necho(false);before:=readFile("%s");\nafter:=readFile("%s");echo(true);\ndiffModelicaFileListings(before, after, OpenModelica.Scripting.DiffFormat.plain, failOnSemanticsChange=true);\ngetErrorString();' % (oldFile, newFile)) else: - omc.sendExpression('writeFile("%s", res)' % (newFile)) + omcAssert(omc, 'writeFile("%s", res)' % (newFile)) isDiff = before != res if before != res: nDiff += 1 diff --git a/requirements.txt b/requirements.txt index 306e8e7..7c5e0e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,6 @@ juliacall matplotlib monotonic natsort -OMPython==4.0 +ompython==3.6 psutil simplejson diff --git a/shared.py b/shared.py index f0eeeab..2f9b7a3 100644 --- a/shared.py +++ b/shared.py @@ -74,9 +74,6 @@ def getReferenceFileName(conf): else: modelName += "."+conf["referenceFileNameExtraName"] referenceFile = conf["referenceFiles"]+"/"+modelName.replace(".",conf["referenceFileNameDelimiter"])+(conf.get("referenceFinalDot") or ".")+conf["referenceFileExtension"] - if os.path.exists(referenceFile): - referenceFile = os.path.abspath(referenceFile) - if not os.path.exists(referenceFile) and not os.path.isdir(referenceFile): if conf.get("allReferenceFilesExist"): raise Exception("Missing reference file %s for config %s" % (referenceFile,conf)) @@ -108,3 +105,5 @@ def isFMPy(fmisimulator): return 'fmpy' in fmisimulator else: return False + + diff --git a/single-model.py b/single-model.py index c3cd0aa..d72e769 100755 --- a/single-model.py +++ b/single-model.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import argparse +import sys, argparse +import simplejson as json import shared parser = argparse.ArgumentParser(description='OpenModelica model testing report generation tool') @@ -20,7 +21,8 @@ libs = {} -import sqlite3, datetime +import cgi, sqlite3, time, datetime +from omcommon import friendlyStr, multiple_replace conn = sqlite3.connect('sqlite3.db') cursor = conn.cursor() diff --git a/test.py b/test.py index 66c235a..5ee14df 100755 --- a/test.py +++ b/test.py @@ -8,10 +8,11 @@ if (sys.version_info < (3, 0)): raise Exception("Python2 is no longer supported") -import html, shutil, os, re, glob, time, argparse, sqlite3, datetime, math +import html, shutil, os, re, glob, time, argparse, sqlite3, datetime, math, platform from joblib import Parallel, delayed import simplejson as json import psutil, subprocess, threading, hashlib +from subprocess import call from monotonic import monotonic from omcommon import friendlyStr, multiple_replace from natsort import natsorted @@ -259,19 +260,14 @@ def target(): print("Error: Expected at least one configuration file to start the library test") sys.exit(1) -from OMPython import OMCSessionZMQ, OMCProcessDocker, OMCSessionException +from OMPython import OMCSession, OMCSessionZMQ # Try to make the processes a bit nicer... os.environ["GC_MARKERS"]="1" print("Start OMC version") -if docker: - omc = OMCProcessDocker(docker=docker, dockerExtraArgs=dockerExtraArgs) - omhome=omc.sendExpression('getInstallationDirectoryPath()') - omc_version=omc.sendExpression('getVersion()') - ompython_omc_version=omc_version -elif ompython_omhome != "": +if ompython_omhome != "": # Use a different OMC for running OMPython than for running the tests omhome = os.environ["OPENMODELICAHOME"] omc_version = check_output_log(omc_cmd + ["--version"], stderr=subprocess.STDOUT).decode("ascii").strip() @@ -280,11 +276,10 @@ def target(): ompython_omc_version=omc.sendExpression('getVersion()') os.environ["OPENMODELICAHOME"] = omhome else: - omc = OMCSessionZMQ() + omc = OMCSessionZMQ(docker=docker, dockerExtraArgs=dockerExtraArgs) omhome=omc.sendExpression('getInstallationDirectoryPath()') omc_version=omc.sendExpression('getVersion()') ompython_omc_version=omc_version - ompython_omc_version=ompython_omc_version.replace("OMCompiler","").strip() def timeSeconds(f): @@ -663,12 +658,11 @@ def hashReferenceFiles(s): raise Exception("Library %s has both libraryVersionLatestInPackageManager:true and libraryVersionExactMatch:true! Make up your mind." % libName) exactMatch=', requireExactVersion=true' - try: - omc.sendExpression('loadModel(%s,%s%s)' % (lib,versions,exactMatch)) - except OMCSessionException as e: - print("Failed to load library %s %s" % (library,versions)) - print(e) - + if not omc.sendExpression('loadModel(%s,%s%s)' % (lib,versions,exactMatch)): + try: + print("Failed to load library %s %s: %s" % (library,versions,omc.sendExpression('OpenModelica.Scripting.getErrorString()'))) + except: + print("Failed to load library %s %s. OpenModelica.Scripting.getErrorString() failed..." % (library,conf["libraryVersion"])) # adrpo: do not sort the top level names as sometimes that loads a bad MSL version # conf["loadFiles"] = sorted(omc.sendExpression("{getSourceFile(cl) for cl in getClassNames()}")) conf["loadFiles"] = omc.sendExpression("{getSourceFile(cl) for cl in getClassNames()}") @@ -727,13 +721,7 @@ def hashReferenceFiles(s): if conf.get("fmi") and fmisimulatorversion: conf["libraryVersionRevision"] = conf["libraryVersionRevision"] + " " + fmisimulatorversion.decode("ascii") conf["libraryLastChange"] = conf["libraryLastChange"] + " " + fmisimulatorversion.decode("ascii") - res = [] - try: - res = omc.sendExpression('{c for c guard isExperiment(c) and not regexBool(typeNameString(x), "^Modelica_Synchronous\\\\.WorkInProgress") in getClassNames(%s, recursive=true)}' % library) - except OMCSessionException as e: - print("Failed to get class names of library %s", library) - print(e) - + res=omc.sendExpression('{c for c guard isExperiment(c) and not regexBool(typeNameString(x), "^Modelica_Synchronous\\.WorkInProgress") in getClassNames(%s, recursive=true)}' % library) if conf.get("ignoreModelPrefix"): if isinstance(conf["ignoreModelPrefix"], list): prefixes = conf["ignoreModelPrefix"] diff --git a/testmodel.py b/testmodel.py index e7a0a0b..4d16ba8 100755 --- a/testmodel.py +++ b/testmodel.py @@ -2,9 +2,10 @@ # -*- coding: utf-8 -*- import argparse, os, sys, signal, threading, psutil, subprocess, shutil +from asyncio.subprocess import STDOUT import simplejson as json from monotonic import monotonic -from OMPython import OMCSessionZMQ, OMCProcessDocker +from OMPython import FindBestOMCSession, OMCSession, OMCSessionZMQ import shared, glob parser = argparse.ArgumentParser(description='OpenModelica library testing tool helper (single model)') @@ -57,7 +58,7 @@ def writeResult(): startJob=monotonic() -def quit_omc(omc: OMCSessionZMQ | None): +def quit_omc(omc): if omc is None: return omc try: @@ -71,7 +72,7 @@ def quit_omc(omc: OMCSessionZMQ | None): omc = None return omc -def writeResultAndExit(exitStatus: int, useOsExit: bool = False, omc: OMCSessionZMQ | None = None, omc_new: OMCSessionZMQ | None = None): +def writeResultAndExit(exitStatus, useOsExit=False, omc=None, omc_new=None): writeResult() print("Calling exit ...") with open(errFile, 'a+') as fp: @@ -89,7 +90,7 @@ def writeResultAndExit(exitStatus: int, useOsExit: bool = False, omc: OMCSession else: sys.exit(exitStatus) -def sendExpressionTimeout(omc: OMCSessionZMQ, cmd: str, timeout: int): +def sendExpressionTimeout(omc, cmd, timeout): with open(errFile, 'a+') as fp: fp.write("%s [Timeout %s]\n" % (cmd, timeout)) def target(res): @@ -250,11 +251,7 @@ def target(res): os.environ["OPENMODELICAHOME"] = omhome def createOmcSession(): - if docker: - return OMCProcessDocker(docker=docker, dockerExtraArgs=dockerExtraArgs, timeout=5) - else: - return OMCSessionZMQ(timeout=5) - + return OMCSession(docker=docker, dockerExtraArgs=dockerExtraArgs, timeout=5) if corbaStyle else OMCSessionZMQ(docker=docker, dockerExtraArgs=dockerExtraArgs, timeout=5) def createOmcSessionNew(): if ompython_omhome != "": os.environ["OPENMODELICAHOME"] = ompython_omhome