Skip to content

Commit

Permalink
[#54111] Create module with env variables used by pyrenode3
Browse files Browse the repository at this point in the history
Signed-off-by: Franciszek Zdobylak <fzdobylak@antmicro.com>
  • Loading branch information
fzdobylak committed Feb 14, 2024
1 parent 3603758 commit 02d9559
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
27 changes: 14 additions & 13 deletions src/pyrenode3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import importlib
import logging
import os

from pyrenode3.loader import RenodeLoader
from pyrenode3 import env

if "PYRENODE_SKIP_LOAD" not in os.environ:
runtime = os.environ.get("PYRENODE_RUNTIME", "mono")

if not env.pyrenode_skip_load:
runtime = env.pyrenode_runtime

if runtime not in ["mono", "coreclr"]:
raise ImportError(f"Runtime '{runtime}' not supported")

if os.environ.get("PYRENODE_PKG") and os.environ.get("PYRENODE_BUILD_DIR"):
if env.pyrenode_pkg and env.pyrenode_build_dir:
raise ImportError(
"Both PYRENODE_PKG and PYRENODE_BUILD_DIR are set. Please unset one of them."
f"Both {env.PYRENODE_PKG} and {env.PYRENODE_BUILD_DIR} are set. "
"Please unset one of them."
)

if package := os.environ.get("PYRENODE_PKG"):
if env.pyrenode_pkg:
if runtime == "mono":
RenodeLoader.from_mono_arch_pkg(package)
RenodeLoader.from_mono_arch_pkg(env.pyrenode_pkg)
elif runtime == "coreclr":
raise ImportError("Using dotnet package is not supported.")

elif build_dir := os.environ.get("PYRENODE_BUILD_DIR"):
elif env.pyrenode_build_dir:
if runtime == "mono":
logging.warning("Using mono with Renode built from sources might not work correctly.")
RenodeLoader.from_mono_build(build_dir)
RenodeLoader.from_mono_build(env.pyrenode_build_dir)
elif runtime == "coreclr":
RenodeLoader.from_net_build(build_dir)
RenodeLoader.from_net_build(env.pyrenode_build_dir)

if not RenodeLoader().is_initialized:
msg = (
"Renode not found. Please set PYRENODE_ARCH_PKG to the location of Renode Arch package."
"Set PYRENODE_PKG to the location of Renode package or PYRENODE_BUILD_DIR to the"
"location of Renode build directory."
f"Renode not found. Please set {env.PYRENODE_PKG} to the location of Renode package or "
f"{env.PYRENODE_BUILD_DIR} to the location of Renode build directory."
)
raise ImportError(msg)

Expand Down
15 changes: 15 additions & 0 deletions src/pyrenode3/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

# Env variable names
PYRENODE_BUILD_DIR = "PYRENODE_BUILD_DIR"
PYRENODE_BUILD_OUTPUT = "PYRENODE_BUILD_OUTPUT"
PYRENODE_PKG = "PYRENODE_PKG"
PYRENODE_RUNTIME = "PYRENODE_RUNTIME"
PYRENODE_SKIP_LOAD = "PYRENODE_SKIP_LOAD"

# Values of env variables
pyrenode_build_dir = os.environ.get(PYRENODE_BUILD_DIR)
pyrenode_build_output = os.environ.get(PYRENODE_BUILD_OUTPUT)
pyrenode_pkg = os.environ.get(PYRENODE_PKG)
pyrenode_runtime = os.environ.get(PYRENODE_RUNTIME, "mono")
pyrenode_skip_load = os.environ.get(PYRENODE_SKIP_LOAD)
8 changes: 4 additions & 4 deletions src/pyrenode3/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from pythonnet import load as pythonnet_load

from pyrenode3 import env
from pyrenode3.singleton import MetaSingleton


Expand Down Expand Up @@ -71,9 +72,8 @@ def from_mono_arch_pkg(cls, path: "Union[str, pathlib.Path]"):

@staticmethod
def discover_bin_dir(renode_dir, runtime):
if "PYRENODE_BUILD_OUTPUT" in os.environ:
build_out = os.environ["PYRENODE_BUILD_OUTPUT"]
renode_build_dir = renode_dir / build_out
if env.pyrenode_build_output:
renode_build_dir = renode_dir / env.pyrenode_build_output

if not renode_build_dir.exists():
logging.critical(f"{renode_build_dir} doesn't exist.")
Expand All @@ -89,7 +89,7 @@ def discover_bin_dir(renode_dir, runtime):
logging.critical(
f"Can't determine Renode directory using the '{renode_dir / default}' pattern. "
f"Please specify its path (relative to {renode_dir}) in the "
"PYRENODE_BUILD_OUTPUT variable."
f"{env.PYRENODE_BUILD_OUTPUT} variable."
)
sys.exit(1)

Expand Down

0 comments on commit 02d9559

Please sign in to comment.