|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""The run-me script in a "make <step>_issue" archive runs the step from |
| 3 | +wherever the archive is unpacked, whatever WORK_HOME the flow used: the |
| 4 | +default ".", a directory relative to the flow directory, or an absolute |
| 5 | +one. |
| 6 | +
|
| 7 | +makeIssue.sh runs as the flow's Makefile runs it, from the flow |
| 8 | +directory, on a stub flow: a scripts directory with the step's script, |
| 9 | +a platform and a design config. openroad is stubbed by a script on PATH |
| 10 | +that records whether the step's script and the step's input in |
| 11 | +RESULTS_DIR are readable where run-me started it, so the test needs |
| 12 | +nothing installed. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import subprocess |
| 18 | +import tarfile |
| 19 | +import tempfile |
| 20 | +import textwrap |
| 21 | +import unittest |
| 22 | + |
| 23 | +UTILS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util") |
| 24 | +MAKE_ISSUE = os.path.join(UTILS_DIR, "makeIssue.sh") |
| 25 | + |
| 26 | +STEP = "detail_place" |
| 27 | +TAG = "test" |
| 28 | +NICKNAME = "stub" |
| 29 | +PLATFORM = "stubpdk" |
| 30 | +VARIANT = "base" |
| 31 | + |
| 32 | +FAKE_OPENROAD = textwrap.dedent("""\ |
| 33 | + #!/usr/bin/env bash |
| 34 | + # Records whether the script it was given and the step's input are |
| 35 | + # readable from the directory run-me started it in. |
| 36 | + script="${@: -1}" |
| 37 | + { |
| 38 | + echo "script=$script" |
| 39 | + [ -r "$script" ] && echo "script_readable=1" || echo "script_readable=0" |
| 40 | + [ -r "$RESULTS_DIR/3_place.odb" ] && echo "input_readable=1" \\ |
| 41 | + || echo "input_readable=0" |
| 42 | + } > "$OPENROAD_REPORT" |
| 43 | + """) |
| 44 | + |
| 45 | + |
| 46 | +class TestMakeIssueRunMe(unittest.TestCase): |
| 47 | + def setUp(self): |
| 48 | + self.tmp_dir = tempfile.TemporaryDirectory() |
| 49 | + tmp = self.tmp_dir.name |
| 50 | + self.flow = os.path.join(tmp, "flow") |
| 51 | + self.scripts = os.path.join(self.flow, "scripts") |
| 52 | + self.platform_dir = os.path.join(self.flow, "platforms", PLATFORM) |
| 53 | + self.design_config = os.path.join( |
| 54 | + self.flow, "designs", PLATFORM, NICKNAME, "config.mk" |
| 55 | + ) |
| 56 | + for path, text in [ |
| 57 | + (os.path.join(self.scripts, STEP + ".tcl"), "puts step\n"), |
| 58 | + (os.path.join(self.platform_dir, "config.mk"), "\n"), |
| 59 | + (self.design_config, "\n"), |
| 60 | + ]: |
| 61 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 62 | + with open(path, "w") as f: |
| 63 | + f.write(text) |
| 64 | + |
| 65 | + self.bin = os.path.join(tmp, "bin") |
| 66 | + os.makedirs(self.bin) |
| 67 | + openroad = os.path.join(self.bin, "openroad") |
| 68 | + with open(openroad, "w") as f: |
| 69 | + f.write(FAKE_OPENROAD) |
| 70 | + os.chmod(openroad, 0o755) |
| 71 | + |
| 72 | + def tearDown(self): |
| 73 | + self.tmp_dir.cleanup() |
| 74 | + |
| 75 | + def make_issue(self, work_home): |
| 76 | + """Runs makeIssue.sh as `make <step>_issue` does, from the flow |
| 77 | + directory, and returns the archive's path.""" |
| 78 | + dirs = { |
| 79 | + name: f"{work_home}/{name.lower()}/{PLATFORM}/{NICKNAME}/{VARIANT}" |
| 80 | + for name in ["LOG_DIR", "OBJECTS_DIR", "REPORTS_DIR", "RESULTS_DIR"] |
| 81 | + } |
| 82 | + for path in dirs.values(): |
| 83 | + os.makedirs(os.path.join(self.flow, path), exist_ok=True) |
| 84 | + with open(os.path.join(self.flow, dirs["RESULTS_DIR"], "3_place.odb"), "w"): |
| 85 | + pass |
| 86 | + |
| 87 | + variables = dict( |
| 88 | + dirs, |
| 89 | + DESIGN_NICKNAME=NICKNAME, |
| 90 | + PLATFORM=PLATFORM, |
| 91 | + FLOW_VARIANT=VARIANT, |
| 92 | + PLATFORM_DIR=self.platform_dir, |
| 93 | + DESIGN_CONFIG=self.design_config, |
| 94 | + SCRIPTS_DIR=self.scripts, |
| 95 | + UTILS_DIR=UTILS_DIR, |
| 96 | + WORK_HOME=work_home, |
| 97 | + ) |
| 98 | + env = dict( |
| 99 | + os.environ, |
| 100 | + FLOW_HOME=self.flow, |
| 101 | + ISSUE_TAG=TAG, |
| 102 | + ISSUE_VARIABLES="\n".join(f"{k}={v}" for k, v in variables.items()), |
| 103 | + **variables, |
| 104 | + ) |
| 105 | + result = subprocess.run( |
| 106 | + [MAKE_ISSUE, f"{work_home}/{STEP}"], |
| 107 | + cwd=self.flow, |
| 108 | + env=env, |
| 109 | + capture_output=True, |
| 110 | + text=True, |
| 111 | + ) |
| 112 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 113 | + return os.path.join(self.flow, work_home, f"{STEP}_{TAG}.tar.gz") |
| 114 | + |
| 115 | + def run_me(self, archive): |
| 116 | + """Unpacks the archive away from the flow and runs its run-me |
| 117 | + script; returns what the stub openroad recorded.""" |
| 118 | + unpacked = os.path.join(self.tmp_dir.name, "unpacked") |
| 119 | + shutil.rmtree(unpacked, ignore_errors=True) |
| 120 | + with tarfile.open(archive) as tar: |
| 121 | + tar.extractall(unpacked) |
| 122 | + root = os.path.join(unpacked, f"{STEP}_{TAG}") |
| 123 | + name = f"run-me-{NICKNAME}-{PLATFORM}-{VARIANT}.sh" |
| 124 | + found = [ |
| 125 | + os.path.join(d, name) for d, _, files in os.walk(root) if name in files |
| 126 | + ] |
| 127 | + self.assertEqual(len(found), 1, f"{name} in {root}") |
| 128 | + |
| 129 | + report = os.path.join(self.tmp_dir.name, "openroad_report") |
| 130 | + env = dict( |
| 131 | + os.environ, |
| 132 | + PATH=self.bin + os.pathsep + os.environ["PATH"], |
| 133 | + OPENROAD_REPORT=report, |
| 134 | + ) |
| 135 | + env.pop("FLOW_HOME", None) |
| 136 | + result = subprocess.run( |
| 137 | + [found[0]], cwd=self.tmp_dir.name, env=env, capture_output=True, text=True |
| 138 | + ) |
| 139 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 140 | + self.assertTrue(os.path.exists(report), "run-me did not start openroad") |
| 141 | + with open(report) as f: |
| 142 | + recorded = dict(line.split("=", 1) for line in f.read().splitlines()) |
| 143 | + return recorded |
| 144 | + |
| 145 | + def assert_runs(self, work_home): |
| 146 | + recorded = self.run_me(self.make_issue(work_home)) |
| 147 | + self.assertEqual(os.path.basename(recorded["script"]), STEP + ".tcl") |
| 148 | + self.assertEqual(recorded["script_readable"], "1", recorded) |
| 149 | + self.assertEqual(recorded["input_readable"], "1", recorded) |
| 150 | + |
| 151 | + def test_work_home_default(self): |
| 152 | + self.assert_runs(".") |
| 153 | + |
| 154 | + def test_work_home_relative(self): |
| 155 | + self.assert_runs("work/stub") |
| 156 | + |
| 157 | + def test_work_home_absolute(self): |
| 158 | + self.assert_runs(os.path.join(self.tmp_dir.name, "work")) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + unittest.main() |
0 commit comments