add nmigen/_toolchain/__init__ as toolchain.py to avoid depending on internal API
authorJacob Lifshay <programmerjake@gmail.com>
Thu, 2 Dec 2021 01:50:41 +0000 (17:50 -0800)
committerJacob Lifshay <programmerjake@gmail.com>
Thu, 2 Dec 2021 01:50:41 +0000 (17:50 -0800)
src/nmutil/formaltest.py
src/nmutil/toolchain.py [new file with mode: 0644]

index 5defaaa9e5a596ba28f447b0d0c72d1596141c08..7244a3a3a50cdb3a64008a0a75a7bfbd53ad7dfa 100644 (file)
@@ -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 (file)
index 0000000..123c4f0
--- /dev/null
@@ -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