build.run: extract from build.plat.
authorwhitequark <cz@m-labs.hk>
Tue, 4 Jun 2019 07:53:34 +0000 (07:53 +0000)
committerwhitequark <cz@m-labs.hk>
Tue, 4 Jun 2019 07:53:34 +0000 (07:53 +0000)
nmigen/build/plat.py
nmigen/build/run.py [new file with mode: 0644]

index fe8d32b3140963f536618de0356a1f6c5764bfed..82508e784abeb6c774f513465c6cd55a7b197bc8 100644 (file)
@@ -1,11 +1,8 @@
 from collections import OrderedDict
 from abc import ABCMeta, abstractmethod, abstractproperty
 import os
-import sys
-import subprocess
 import textwrap
 import re
-import zipfile
 import jinja2
 
 from .. import __version__
@@ -13,66 +10,13 @@ from ..hdl.ast import *
 from ..hdl.dsl import *
 from ..hdl.ir import *
 from ..back import rtlil, verilog
-from .res import ConstraintManager
+from .res import *
+from .run import *
 
 
 __all__ = ["Platform", "TemplatedPlatform"]
 
 
-class BuildPlan:
-    def __init__(self, script):
-        self.script = script
-        self.files  = OrderedDict()
-
-    def add_file(self, filename, content):
-        assert isinstance(filename, str) and filename not in self.files
-        # Just to make sure we don't accidentally overwrite anything.
-        assert not os.path.normpath(filename).startswith("..")
-        self.files[filename] = content
-
-    def execute(self, root="build", run_script=True):
-        os.makedirs(root, exist_ok=True)
-        cwd = os.getcwd()
-        try:
-            os.chdir(root)
-
-            for filename, content in self.files.items():
-                dirname = os.path.dirname(filename)
-                if dirname:
-                    os.makedirs(dirname, exist_ok=True)
-
-                mode = "wt" if isinstance(content, str) else "wb"
-                with open(filename, mode) as f:
-                    f.write(content)
-
-            if run_script:
-                if sys.platform.startswith("win32"):
-                    subprocess.run(["cmd", "/c", "{}.bat".format(self.script)], check=True)
-                else:
-                    subprocess.run(["sh", "{}.sh".format(self.script)], check=True)
-
-                return BuildProducts(os.getcwd())
-
-        finally:
-            os.chdir(cwd)
-
-    def archive(self, file):
-        with zipfile.ZipFile(file, "w") as archive:
-            # Write archive members in deterministic order and with deterministic timestamp.
-            for filename in sorted(self.files):
-                archive.writestr(zipfile.ZipInfo(filename), self.files[filename])
-
-
-class BuildProducts:
-    def __init__(self, root):
-        self._root = root
-
-    def get(self, filename, mode="b"):
-        assert mode in "bt"
-        with open(os.path.join(self._root, filename), "r" + mode) as f:
-            return f.read()
-
-
 class Platform(ConstraintManager, metaclass=ABCMeta):
     resources  = abstractproperty()
     connectors = abstractproperty()
diff --git a/nmigen/build/run.py b/nmigen/build/run.py
new file mode 100644 (file)
index 0000000..d4cc6dc
--- /dev/null
@@ -0,0 +1,62 @@
+from collections import OrderedDict
+import os
+import sys
+import subprocess
+import zipfile
+
+
+__all__ = ["BuildPlan", "BuildProducts"]
+
+
+class BuildPlan:
+    def __init__(self, script):
+        self.script = script
+        self.files  = OrderedDict()
+
+    def add_file(self, filename, content):
+        assert isinstance(filename, str) and filename not in self.files
+        # Just to make sure we don't accidentally overwrite anything.
+        assert not os.path.normpath(filename).startswith("..")
+        self.files[filename] = content
+
+    def execute(self, root="build", run_script=True):
+        os.makedirs(root, exist_ok=True)
+        cwd = os.getcwd()
+        try:
+            os.chdir(root)
+
+            for filename, content in self.files.items():
+                dirname = os.path.dirname(filename)
+                if dirname:
+                    os.makedirs(dirname, exist_ok=True)
+
+                mode = "wt" if isinstance(content, str) else "wb"
+                with open(filename, mode) as f:
+                    f.write(content)
+
+            if run_script:
+                if sys.platform.startswith("win32"):
+                    subprocess.run(["cmd", "/c", "{}.bat".format(self.script)], check=True)
+                else:
+                    subprocess.run(["sh", "{}.sh".format(self.script)], check=True)
+
+                return BuildProducts(os.getcwd())
+
+        finally:
+            os.chdir(cwd)
+
+    def archive(self, file):
+        with zipfile.ZipFile(file, "w") as archive:
+            # Write archive members in deterministic order and with deterministic timestamp.
+            for filename in sorted(self.files):
+                archive.writestr(zipfile.ZipInfo(filename), self.files[filename])
+
+
+class BuildProducts:
+    def __init__(self, root):
+        self._root = root
+
+    def get(self, filename, mode="b"):
+        assert mode in "bt"
+        with open(os.path.join(self._root, filename), "r" + mode) as f:
+            return f.read()