From 980651b6fe1e631002e03ee304b6867d9d8cb53f Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Wed, 1 Dec 2021 17:50:41 -0800 Subject: [PATCH] add nmigen/_toolchain/__init__ as toolchain.py to avoid depending on internal API --- src/nmutil/formaltest.py | 2 +- src/nmutil/toolchain.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/nmutil/toolchain.py diff --git a/src/nmutil/formaltest.py b/src/nmutil/formaltest.py index 5defaaa..7244a3a 100644 --- a/src/nmutil/formaltest.py +++ b/src/nmutil/formaltest.py @@ -6,7 +6,7 @@ import unittest from nmigen.hdl.ast import Statement from nmigen.hdl.ir import Fragment from nmigen.back import rtlil -from nmigen._toolchain import require_tool +from nmutil.toolchain import require_tool from nmutil.get_test_path import get_test_path diff --git a/src/nmutil/toolchain.py b/src/nmutil/toolchain.py new file mode 100644 index 0000000..123c4f0 --- /dev/null +++ b/src/nmutil/toolchain.py @@ -0,0 +1,37 @@ +import os +import shutil + + +__all__ = ["ToolNotFound", "tool_env_var", "has_tool", "require_tool"] + + +class ToolNotFound(Exception): + pass + + +def tool_env_var(name): + return name.upper().replace("-", "_").replace("+", "X") + + +def _get_tool(name): + return os.environ.get(tool_env_var(name), name) + + +def has_tool(name): + return shutil.which(_get_tool(name)) is not None + + +def require_tool(name): + env_var = tool_env_var(name) + path = _get_tool(name) + if shutil.which(path) is None: + if env_var in os.environ: + raise ToolNotFound("Could not find required tool {} in {} as " + "specified via the {} environment variable". + format(name, path, env_var)) + else: + raise ToolNotFound("Could not find required tool {} in PATH. Place " + "it directly in PATH or specify path explicitly " + "via the {} environment variable". + format(name, env_var)) + return path -- 2.30.2