From adf015c46bea42b597355721974cbe1b967d93e8 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Tue, 17 Nov 2026 23:56:24 +0100 Subject: [PATCH 1/2] Select one of several installed OpenModelica versions The Windows installer is changed to allow several OpenModelica versions side by side (OpenModelica/OpenModelica#5809, OpenModelica/OpenModelicaSetup#96). OPENMODELICAHOME then points to the default one and each version sets OPENMODELICAHOME_, with the characters of the version that are not letters or digits replaced by underscores (OPENMODELICAHOME_1_27_0). OMCSessionLocal and ModelicaSystemOMC get a version argument and look for OpenModelica in this order: 1. the omhome argument 2. the version argument: OPENMODELICAHOME_, or the newest installed version that starts with it ("1.27" for the newest 1.27.x) 3. OPENMODELICAHOME 4. the newest OPENMODELICAHOME_ 5. omc on the PATH Without OPENMODELICAHOME_ variables nothing changes. With them, an OPENMODELICAHOME without an omc executable (left behind by an uninstalled version) is skipped. A version that is not installed raises OMSessionException with the list of the installed ones. Co-Authored-By: Claude Fable 5.2 --- OMPython/modelica_system_omc.py | 5 +- OMPython/om_session_omc.py | 134 +++++++++++++++++++++++++++++--- README.md | 16 ++++ tests/test_omc_home.py | 126 ++++++++++++++++++++++++++++++ 4 files changed, 271 insertions(+), 10 deletions(-) create mode 100644 tests/test_omc_home.py diff --git a/OMPython/modelica_system_omc.py b/OMPython/modelica_system_omc.py index d7c4bf1a..bb16b7b1 100644 --- a/OMPython/modelica_system_omc.py +++ b/OMPython/modelica_system_omc.py @@ -40,6 +40,7 @@ def __init__( omhome: Optional[str] = None, session: Optional[OMSessionABC] = None, use_default_command_line_options: bool = True, + version: Optional[str] = None, ) -> None: """Create a ModelicaSystem instance. To define the model use model() or convertFmu2Mo(). @@ -52,12 +53,14 @@ def __init__( files like the model executable. If left unspecified, a tmp directory will be created. omhome: path to OMC to be used when creating the OMC session (see OMCSession). + version: OpenModelica version to be used when creating the OMC session if several versions are + installed, e.g. "1.27.0" (see OMCSessionLocal). session: definition of a (local) OMC session to be used. If unspecified, a new local session will be created. """ if session is None: - session = OMCSessionLocal(omhome=omhome) + session = OMCSessionLocal(omhome=omhome, version=version) super().__init__( session=session, diff --git a/OMPython/om_session_omc.py b/OMPython/om_session_omc.py index de128913..892bf2c2 100644 --- a/OMPython/om_session_omc.py +++ b/OMPython/om_session_omc.py @@ -18,7 +18,7 @@ import sys import tempfile import time -from typing import Any, Optional, Tuple +from typing import Any, Mapping, Optional, Tuple import uuid import psutil @@ -1110,6 +1110,76 @@ def _docker_omc_start( return omc_process, docker_process, docker_cid +OMHOME_ENV = "OPENMODELICAHOME" +OMHOME_ENV_VERSION_PREFIX = OMHOME_ENV + "_" + + +def omc_version_env_name(version: str) -> str: + """Name of the environment variable that holds the installation directory of an OpenModelica version. + + The Windows installer sets ``OPENMODELICAHOME_`` for each installed version, where every character + of the version that is not a letter or a digit is replaced by an underscore: version ``1.27.0`` is found via + ``OPENMODELICAHOME_1_27_0``. A leading ``v`` (as in the git tag ``v1.27.0``) is ignored. + """ + version = version.strip() + if version[:1] in ("v", "V") and version[1:2].isdigit(): + version = version[1:] + return OMHOME_ENV_VERSION_PREFIX + re.sub(r"[^0-9A-Za-z]", "_", version) + + +def _omc_version_sort_key(name: str) -> tuple: + """Sort key for the names ``OPENMODELICAHOME_``; the newest version has the highest key. + + The version is ``__`` followed by nothing for a release (1_27_0), by a pre-release part + for a build before that release (1_28_0_dev_1962_g6e348e217c) or by the number of commits for a build after + that release (1_27_0_5_g0123456789). For the same ``__`` a pre-release is older than the + release, which is older than a build after it; the numbers that follow (e.g. the number of commits) decide + between two builds of the same kind. + """ + parts = name[len(OMHOME_ENV_VERSION_PREFIX):].split("_") + numbers: list[int] = [] + while parts and parts[0].isdigit() and len(numbers) < 3: + numbers.append(int(parts.pop(0))) + numbers += [0] * (3 - len(numbers)) + + if not parts: + kind = 1 # release + elif parts[0].isdigit(): + kind = 2 # commits after the release + else: + kind = 0 # dev, alpha, beta, rc, ... + return tuple(numbers), kind, tuple(int(part) for part in parts if part.isdigit()) + + +def omc_home_versions(environ: Optional[Mapping[str, str]] = None) -> dict[str, pathlib.Path]: + """The OpenModelica installations registered as ``OPENMODELICAHOME_``, newest version first. + + Args: + environ: The environment to look at, ``os.environ`` if None. + + Returns: + A dictionary from the name of the environment variable to the installation directory. Variables which + point to a directory without an omc executable are left out. + """ + if environ is None: + environ = os.environ + + homes = {} + for name, value in environ.items(): + # the names of environment variables are case-insensitive on Windows + if not name.upper().startswith(OMHOME_ENV_VERSION_PREFIX) or not value: + continue + home = pathlib.Path(value) + if _omc_home_has_omc(home): + homes[name] = home + + return dict(sorted(homes.items(), key=lambda item: _omc_version_sort_key(item[0]), reverse=True)) + + +def _omc_home_has_omc(omhome: pathlib.Path) -> bool: + return (omhome / "bin" / "omc").is_file() or (omhome / "bin" / "omc.exe").is_file() + + class OMCSessionLocal(OMCSessionABC): """ OMCSession implementation which runs the OMC server locally on the machine (Linux / Windows). @@ -1119,32 +1189,52 @@ def __init__( self, timeout: Optional[float] = None, omhome: Optional[str | os.PathLike] = None, + version: Optional[str] = None, ) -> None: """Start and connect to a local OMC server instance via ZeroMQ. Args: timeout: Communication timeout in seconds with the OMC server. omhome: Path to the OpenModelica installation directory. If None, - it is resolved from OPENMODELICAHOME or the system PATH. + it is resolved from the environment, see ``version``. + version: The OpenModelica version to use if several are installed, e.g. "1.27.0" or "1.27" for the + newest 1.27.x. It is looked up in the environment variables ``OPENMODELICAHOME_`` which + the Windows installer sets for each installed version. If ``omhome`` and ``version`` are None, + ``OPENMODELICAHOME``, the newest ``OPENMODELICAHOME_`` and the system PATH are tried + in this order. """ super().__init__(timeout=timeout) self.model_execution_local = True # where to find OpenModelica - self._omhome = self._omc_home_get(omhome=omhome) + self._omhome = self._omc_home_get(omhome=omhome, version=version) # start up omc executable, which is waiting for the ZMQ connection self._omc_process = self._omc_process_get() # connect to the running omc instance using ZMQ self._omc_port = self._omc_port_get() @staticmethod - def _omc_home_get(omhome: Optional[str | os.PathLike] = None) -> pathlib.Path: + def _omc_home_get( + omhome: Optional[str | os.PathLike] = None, + version: Optional[str] = None, + environ: Optional[Mapping[str, str]] = None, + ) -> pathlib.Path: """Resolve the OpenModelica installation directory. + The first one that applies is used: + + 1. ``omhome`` + 2. ``version``: ``OPENMODELICAHOME_``, or the newest ``OPENMODELICAHOME_...`` if only + the start of a version is given ("1.27" for the newest 1.27.x) + 3. ``OPENMODELICAHOME``, if there is an omc executable in it + 4. the newest ``OPENMODELICAHOME_`` + 5. the location of ``omc`` on PATH + Args: - omhome: Explicit OpenModelica home path. If None, ``OPENMODELICAHOME`` - or the location of ``omc`` on PATH is used. + omhome: Explicit OpenModelica home path. + version: The OpenModelica version to use, one of the installed ones. + environ: The environment to look at, ``os.environ`` if None. Returns: Path to the OpenModelica installation directory. @@ -1152,17 +1242,43 @@ def _omc_home_get(omhome: Optional[str | os.PathLike] = None) -> pathlib.Path: Raises: OMSessionException: If OpenModelica cannot be located. """ + if environ is None: + environ = os.environ + # use the provided path if omhome is not None: return pathlib.Path(omhome) + versions = omc_home_versions(environ=environ) + + # use the requested version + if version is not None: + name = omc_version_env_name(version) + for name_installed, home in versions.items(): + if name_installed.upper() == name.upper(): + return home + # versions are sorted, newest first + for name_installed, home in versions.items(): + if name_installed.upper().startswith(name.upper() + "_"): + return home + + installed = ", ".join(versions) if versions else "none" + raise OMSessionException(f"Cannot find OpenModelica version {version!r}: no environment variable " + f"{name} or {name}_*. Installed versions: {installed}") + # check the environment variable - omhome = os.environ.get('OPENMODELICAHOME') - if omhome is not None: + omhome = environ.get(OMHOME_ENV) + if omhome and (not versions or _omc_home_has_omc(pathlib.Path(omhome))): + # Without versioned installations the path is used as it is, as it always was. With them, a path + # left behind by an uninstalled version is skipped. return pathlib.Path(omhome) + # use the newest of the installed versions + if versions: + return next(iter(versions.values())) + # Get the path to the OMC executable, if not installed this will be None - path_to_omc = shutil.which("omc") + path_to_omc = shutil.which("omc", path=environ.get("PATH")) if path_to_omc is not None: return pathlib.Path(path_to_omc).parents[1] diff --git a/README.md b/README.md index 3e85d6f4..2e17c636 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,22 @@ OMPython is a Python interface that uses ZeroMQ to communicate with OpenModelica - Python >= 3.10; Python >= 3.12 for `OMPath` functionality (handle file system access via OMC using a `pathlib.Path` compatible abstraction layer) - Additional packages: numpy, psutil, pyparsing and pyzmq +## Selecting the OpenModelica installation + +`OMCSessionLocal` and `ModelicaSystemOMC` look for OpenModelica in this order: + +1. the `omhome` argument: `OMPython.OMCSessionLocal(omhome="C:/Program Files/OpenModelica1.27.0-64bit")` +2. the `version` argument: `OMPython.OMCSessionLocal(version="1.27.0")`, or `version="1.27"` for the newest + installed 1.27.x +3. the environment variable `OPENMODELICAHOME` +4. the newest version found in the environment variables `OPENMODELICAHOME_` +5. `omc` on the `PATH` + +The Windows installer of OpenModelica allows several versions to be installed side by side and sets +`OPENMODELICAHOME_` for each of them, with every character of the version that is not a letter or a +digit replaced by an underscore: `OPENMODELICAHOME_1_27_0` for version 1.27.0. `OPENMODELICAHOME` points to the +version selected as the default one. + ## Installation Installation using `pip` is recommended. diff --git a/tests/test_omc_home.py b/tests/test_omc_home.py new file mode 100644 index 00000000..9c9e05f3 --- /dev/null +++ b/tests/test_omc_home.py @@ -0,0 +1,126 @@ +""" +Where OMCSessionLocal looks for OpenModelica: omhome, version, OPENMODELICAHOME, OPENMODELICAHOME_, PATH. +No omc is started, the installations are directories with an empty bin/omc. +""" + +import pathlib + +import pytest + +import OMPython +from OMPython.om_session_omc import omc_home_versions, omc_version_env_name + +RELEASE = "OPENMODELICAHOME_1_27_0" +MAINTENANCE = "OPENMODELICAHOME_1_27_0_5_g0123456789" +NIGHTLY_OLD = "OPENMODELICAHOME_1_28_0_dev_900_gaaaaaaaaaa" +NIGHTLY = "OPENMODELICAHOME_1_28_0_dev_1962_g6e348e217c" + + +def omc_home_get(environ, omhome=None, version=None): + # staticmethod, no session is created + return OMPython.OMCSessionLocal._omc_home_get(omhome=omhome, version=version, environ=environ) + + +def install(tmp_path: pathlib.Path, name: str) -> str: + home = tmp_path / name + (home / "bin").mkdir(parents=True) + (home / "bin" / "omc").touch() + return str(home) + + +@pytest.fixture +def environ(tmp_path): + return {name: install(tmp_path, name) for name in (NIGHTLY_OLD, RELEASE, NIGHTLY, MAINTENANCE)} + + +def test_version_env_name(): + assert omc_version_env_name("1.27.0") == RELEASE + assert omc_version_env_name("v1.27.0") == RELEASE + assert omc_version_env_name("1.28.0-dev-1962-g6e348e217c") == NIGHTLY + assert omc_version_env_name("1.27") == "OPENMODELICAHOME_1_27" + + +def test_versions_newest_first(environ, tmp_path): + environ["OPENMODELICAHOME"] = environ[RELEASE] + environ["OPENMODELICAHOME_1_30_0"] = str(tmp_path / "uninstalled") + environ["PATH"] = "/usr/bin" + assert list(omc_home_versions(environ=environ)) == [NIGHTLY, NIGHTLY_OLD, MAINTENANCE, RELEASE] + + +def test_versions_number_not_string_order(tmp_path): + names = ["OPENMODELICAHOME_1_9_0", "OPENMODELICAHOME_1_27_0", "OPENMODELICAHOME_1_100_0"] + environ = {name: install(tmp_path, name) for name in names} + assert list(omc_home_versions(environ=environ)) == list(reversed(names)) + + +def test_omhome_wins(environ): + assert omc_home_get(environ, omhome="/some/where", version="1.27.0") == pathlib.Path("/some/where") + + +def test_version(environ): + assert omc_home_get(environ, version="1.27.0") == pathlib.Path(environ[RELEASE]) + assert omc_home_get(environ, version="v1.27.0") == pathlib.Path(environ[RELEASE]) + assert omc_home_get(environ, version="1.28.0-dev-900-gaaaaaaaaaa") == pathlib.Path(environ[NIGHTLY_OLD]) + + +def test_version_start(environ): + # the newest one that starts with the given version + assert omc_home_get(environ, version="1.28") == pathlib.Path(environ[NIGHTLY]) + assert omc_home_get(environ, version="1.28.0-dev") == pathlib.Path(environ[NIGHTLY]) + assert omc_home_get(environ, version="1") == pathlib.Path(environ[NIGHTLY]) + # the given version is used if it is installed, 1.27.0-5-g0123456789 is not picked + assert omc_home_get(environ, version="1.27") == pathlib.Path(environ[MAINTENANCE]) + assert omc_home_get(environ, version="1.27.0") == pathlib.Path(environ[RELEASE]) + + +def test_version_start_is_not_a_string_prefix(environ): + # 1.2 is not 1.27 + with pytest.raises(OMPython.OMSessionException, match="OPENMODELICAHOME_1_2"): + omc_home_get(environ, version="1.2") + + +def test_version_not_installed(environ): + with pytest.raises(OMPython.OMSessionException) as excinfo: + omc_home_get(environ, version="1.26.0") + assert "OPENMODELICAHOME_1_26_0" in str(excinfo.value) + # the installed versions are listed + assert RELEASE in str(excinfo.value) and NIGHTLY in str(excinfo.value) + + with pytest.raises(OMPython.OMSessionException, match="Installed versions: none"): + omc_home_get({}, version="1.26.0") + + +def test_default_openmodelicahome(environ): + environ["OPENMODELICAHOME"] = environ[RELEASE] + assert omc_home_get(environ) == pathlib.Path(environ[RELEASE]) + + +def test_default_openmodelicahome_only(): + # no versioned installations: the path is used as it is, as before + assert omc_home_get({"OPENMODELICAHOME": "/opt/openmodelica"}) == pathlib.Path("/opt/openmodelica") + + +def test_default_openmodelicahome_of_uninstalled_version(environ, tmp_path): + environ["OPENMODELICAHOME"] = str(tmp_path / "uninstalled") + assert omc_home_get(environ) == pathlib.Path(environ[NIGHTLY]) + + +def test_default_newest_version(environ): + assert omc_home_get(environ) == pathlib.Path(environ[NIGHTLY]) + + +def test_default_path(tmp_path): + home = install(tmp_path, "onpath") + (pathlib.Path(home) / "bin" / "omc").chmod(0o755) + assert omc_home_get({"PATH": str(pathlib.Path(home) / "bin")}) == pathlib.Path(home) + + +def test_not_found(tmp_path): + with pytest.raises(OMPython.OMSessionException, match="Cannot find OpenModelica executable"): + omc_home_get({"PATH": str(tmp_path)}) + + +def test_names_are_case_insensitive(tmp_path): + # Windows keeps the case the variable was created with + home = install(tmp_path, "home") + assert omc_home_get({"OpenModelicaHome_1_27_0": home}, version="1.27.0") == pathlib.Path(home) From 3c33dc5373df2e5bbd26434550f8cc42b16af670 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Wed, 18 Nov 2026 01:00:20 +0100 Subject: [PATCH 2/2] Test the PATH lookup with omc.exe on Windows shutil.which only finds a file with an executable extension on Windows, so the test that puts an empty omc on the PATH failed there. Co-Authored-By: Claude Fable 5.2 --- tests/test_omc_home.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_omc_home.py b/tests/test_omc_home.py index 9c9e05f3..4a3faece 100644 --- a/tests/test_omc_home.py +++ b/tests/test_omc_home.py @@ -3,6 +3,7 @@ No omc is started, the installations are directories with an empty bin/omc. """ +import os import pathlib import pytest @@ -111,7 +112,10 @@ def test_default_newest_version(environ): def test_default_path(tmp_path): home = install(tmp_path, "onpath") - (pathlib.Path(home) / "bin" / "omc").chmod(0o755) + # an executable that is found on the PATH: Windows looks for omc.exe + omc = pathlib.Path(home) / "bin" / ("omc.exe" if os.name == "nt" else "omc") + omc.touch() + omc.chmod(0o755) assert omc_home_get({"PATH": str(pathlib.Path(home) / "bin")}) == pathlib.Path(home)