|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regression check for loading a true RGB input image in imiv.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import os |
| 9 | +import shlex |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +ERROR_PATTERNS = ( |
| 17 | + "error: imiv exited with code", |
| 18 | + "OpenGL texture upload failed", |
| 19 | + "OpenGL preview draw failed", |
| 20 | + "OpenGL OCIO preview draw failed", |
| 21 | + "failed to create Metal source texture", |
| 22 | + "failed to create Metal upload pipeline", |
| 23 | + "failed to create Metal source upload buffer", |
| 24 | + "Metal source upload compute dispatch failed", |
| 25 | + "failed to create Metal preview texture", |
| 26 | + "Metal preview render failed", |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def _repo_root() -> Path: |
| 31 | + return Path(__file__).resolve().parents[3] |
| 32 | + |
| 33 | + |
| 34 | +def _default_binary(repo_root: Path) -> Path: |
| 35 | + candidates = [ |
| 36 | + repo_root / "build_u" / "bin" / "imiv", |
| 37 | + repo_root / "build" / "bin" / "imiv", |
| 38 | + repo_root / "build_u" / "src" / "imiv" / "imiv", |
| 39 | + repo_root / "build" / "src" / "imiv" / "imiv", |
| 40 | + repo_root / "build" / "Debug" / "imiv.exe", |
| 41 | + repo_root / "build" / "Release" / "imiv.exe", |
| 42 | + ] |
| 43 | + for candidate in candidates: |
| 44 | + if candidate.exists(): |
| 45 | + return candidate |
| 46 | + return candidates[0] |
| 47 | + |
| 48 | + |
| 49 | +def _default_oiiotool(repo_root: Path) -> Path: |
| 50 | + candidates = [ |
| 51 | + repo_root / "build_u" / "bin" / "oiiotool", |
| 52 | + repo_root / "build" / "bin" / "oiiotool", |
| 53 | + repo_root / "build_u" / "src" / "oiiotool" / "oiiotool", |
| 54 | + repo_root / "build" / "src" / "oiiotool" / "oiiotool", |
| 55 | + repo_root / "build" / "Debug" / "oiiotool.exe", |
| 56 | + repo_root / "build" / "Release" / "oiiotool.exe", |
| 57 | + ] |
| 58 | + for candidate in candidates: |
| 59 | + if candidate.exists(): |
| 60 | + return candidate |
| 61 | + which = shutil.which("oiiotool") |
| 62 | + return Path(which) if which else candidates[0] |
| 63 | + |
| 64 | + |
| 65 | +def _default_env_script(repo_root: Path, exe: Path | None = None) -> Path: |
| 66 | + candidates: list[Path] = [] |
| 67 | + if exe is not None: |
| 68 | + exe = exe.resolve() |
| 69 | + candidates.extend([exe.parent / "imiv_env.sh", exe.parent.parent / "imiv_env.sh"]) |
| 70 | + candidates.extend([repo_root / "build" / "imiv_env.sh", repo_root / "build_u" / "imiv_env.sh"]) |
| 71 | + for candidate in candidates: |
| 72 | + if candidate.exists(): |
| 73 | + return candidate |
| 74 | + return candidates[0] |
| 75 | + |
| 76 | + |
| 77 | +def _load_env_from_script(script_path: Path) -> dict[str, str]: |
| 78 | + env = dict(os.environ) |
| 79 | + if not script_path.exists() or shutil.which("bash") is None: |
| 80 | + return env |
| 81 | + |
| 82 | + quoted = shlex.quote(str(script_path)) |
| 83 | + proc = subprocess.run( |
| 84 | + ["bash", "-lc", f"source {quoted} >/dev/null 2>&1; env -0"], |
| 85 | + check=True, |
| 86 | + stdout=subprocess.PIPE, |
| 87 | + ) |
| 88 | + for item in proc.stdout.split(b"\0"): |
| 89 | + if not item: |
| 90 | + continue |
| 91 | + key, _, value = item.partition(b"=") |
| 92 | + if not key: |
| 93 | + continue |
| 94 | + env[key.decode("utf-8", errors="ignore")] = value.decode( |
| 95 | + "utf-8", errors="ignore" |
| 96 | + ) |
| 97 | + return env |
| 98 | + |
| 99 | + |
| 100 | +def _run_checked(cmd: list[str], *, cwd: Path) -> None: |
| 101 | + print("run:", " ".join(cmd)) |
| 102 | + subprocess.run(cmd, cwd=str(cwd), check=True) |
| 103 | + |
| 104 | + |
| 105 | +def _generate_rgb_fixture(oiiotool: Path, source_path: Path, out_path: Path) -> None: |
| 106 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 107 | + _run_checked( |
| 108 | + [ |
| 109 | + str(oiiotool), |
| 110 | + str(source_path), |
| 111 | + "--ch", |
| 112 | + "R,G,B", |
| 113 | + "-d", |
| 114 | + "uint8", |
| 115 | + "-o", |
| 116 | + str(out_path), |
| 117 | + ], |
| 118 | + cwd=out_path.parent, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +def main() -> int: |
| 123 | + repo_root = _repo_root() |
| 124 | + runner = repo_root / "src" / "imiv" / "tools" / "imiv_gui_test_run.py" |
| 125 | + default_source = repo_root / "ASWF" / "logos" / "openimageio-stacked-gradient.png" |
| 126 | + |
| 127 | + ap = argparse.ArgumentParser(description=__doc__) |
| 128 | + ap.add_argument("--bin", default=str(_default_binary(repo_root)), help="imiv executable") |
| 129 | + ap.add_argument("--cwd", default="", help="Working directory for imiv") |
| 130 | + ap.add_argument( |
| 131 | + "--backend", |
| 132 | + default="", |
| 133 | + help="Optional runtime backend override passed through to imiv", |
| 134 | + ) |
| 135 | + ap.add_argument( |
| 136 | + "--oiiotool", default=str(_default_oiiotool(repo_root)), help="oiiotool executable" |
| 137 | + ) |
| 138 | + ap.add_argument("--env-script", default="", help="Optional shell env setup script") |
| 139 | + ap.add_argument("--out-dir", default="", help="Output directory") |
| 140 | + ap.add_argument( |
| 141 | + "--source-image", |
| 142 | + default=str(default_source), |
| 143 | + help="Source image used to generate a 3-channel RGB fixture", |
| 144 | + ) |
| 145 | + ap.add_argument("--trace", action="store_true", help="Enable runner tracing") |
| 146 | + args = ap.parse_args() |
| 147 | + |
| 148 | + exe = Path(args.bin).resolve() |
| 149 | + if not exe.exists(): |
| 150 | + print(f"error: binary not found: {exe}", file=sys.stderr) |
| 151 | + return 2 |
| 152 | + |
| 153 | + oiiotool = Path(args.oiiotool).resolve() |
| 154 | + if not oiiotool.exists(): |
| 155 | + print(f"error: oiiotool not found: {oiiotool}", file=sys.stderr) |
| 156 | + return 2 |
| 157 | + |
| 158 | + cwd = Path(args.cwd).resolve() if args.cwd else exe.parent.resolve() |
| 159 | + if args.out_dir: |
| 160 | + out_dir = Path(args.out_dir).resolve() |
| 161 | + else: |
| 162 | + out_dir = exe.parent.parent / "imiv_captures" / "rgb_input_regression" |
| 163 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 164 | + |
| 165 | + source_path = Path(args.source_image).resolve() |
| 166 | + if not source_path.exists(): |
| 167 | + print(f"error: source image not found: {source_path}", file=sys.stderr) |
| 168 | + return 2 |
| 169 | + |
| 170 | + env_script = ( |
| 171 | + Path(args.env_script).resolve() |
| 172 | + if args.env_script |
| 173 | + else _default_env_script(repo_root, exe) |
| 174 | + ) |
| 175 | + env = _load_env_from_script(env_script) |
| 176 | + env["IMIV_CONFIG_HOME"] = str(out_dir / "cfg") |
| 177 | + |
| 178 | + rgb_fixture = out_dir / "rgb_input_fixture_u8.tif" |
| 179 | + _generate_rgb_fixture(oiiotool, source_path, rgb_fixture) |
| 180 | + |
| 181 | + layout_path = out_dir / "rgb_input.layout.json" |
| 182 | + state_path = out_dir / "rgb_input.state.json" |
| 183 | + log_path = out_dir / "rgb_input.log" |
| 184 | + |
| 185 | + cmd = [ |
| 186 | + sys.executable, |
| 187 | + str(runner), |
| 188 | + "--bin", |
| 189 | + str(exe), |
| 190 | + "--cwd", |
| 191 | + str(cwd), |
| 192 | + ] |
| 193 | + if args.backend: |
| 194 | + cmd.extend(["--backend", args.backend]) |
| 195 | + cmd.extend( |
| 196 | + [ |
| 197 | + "--open", |
| 198 | + str(rgb_fixture), |
| 199 | + "--layout-json-out", |
| 200 | + str(layout_path), |
| 201 | + "--layout-items", |
| 202 | + "--state-json-out", |
| 203 | + str(state_path), |
| 204 | + "--post-action-delay-frames", |
| 205 | + "2", |
| 206 | + ] |
| 207 | + ) |
| 208 | + if args.trace: |
| 209 | + cmd.append("--trace") |
| 210 | + |
| 211 | + with log_path.open("w", encoding="utf-8") as log_handle: |
| 212 | + proc = subprocess.run( |
| 213 | + cmd, |
| 214 | + cwd=str(repo_root), |
| 215 | + env=env, |
| 216 | + check=False, |
| 217 | + stdout=log_handle, |
| 218 | + stderr=subprocess.STDOUT, |
| 219 | + timeout=90, |
| 220 | + ) |
| 221 | + if proc.returncode != 0: |
| 222 | + print(f"error: runner exited with code {proc.returncode}", file=sys.stderr) |
| 223 | + return 1 |
| 224 | + |
| 225 | + for required in (layout_path, state_path): |
| 226 | + if not required.exists(): |
| 227 | + print(f"error: missing output: {required}", file=sys.stderr) |
| 228 | + return 1 |
| 229 | + |
| 230 | + log_text = log_path.read_text(encoding="utf-8", errors="ignore") |
| 231 | + for pattern in ERROR_PATTERNS: |
| 232 | + if pattern in log_text: |
| 233 | + print(f"error: found runtime error pattern: {pattern}", file=sys.stderr) |
| 234 | + return 1 |
| 235 | + |
| 236 | + layout = json.loads(layout_path.read_text(encoding="utf-8")) |
| 237 | + if not any(window.get("name") == "Image" for window in layout.get("windows", [])): |
| 238 | + print("error: layout dump missing Image window", file=sys.stderr) |
| 239 | + return 1 |
| 240 | + |
| 241 | + state = json.loads(state_path.read_text(encoding="utf-8")) |
| 242 | + if not state.get("image_loaded"): |
| 243 | + print("error: state dump says image is not loaded", file=sys.stderr) |
| 244 | + return 1 |
| 245 | + |
| 246 | + current_path = state.get("image_path") or "" |
| 247 | + if not current_path: |
| 248 | + print("error: state dump missing image_path", file=sys.stderr) |
| 249 | + return 1 |
| 250 | + try: |
| 251 | + current_resolved = Path(current_path).resolve() |
| 252 | + except Exception: |
| 253 | + current_resolved = Path(current_path) |
| 254 | + if current_resolved != rgb_fixture.resolve(): |
| 255 | + print( |
| 256 | + f"error: loaded path mismatch: expected {rgb_fixture}, got {current_path}", |
| 257 | + file=sys.stderr, |
| 258 | + ) |
| 259 | + return 1 |
| 260 | + |
| 261 | + image_size = state.get("image_size", [0, 0]) |
| 262 | + if ( |
| 263 | + not isinstance(image_size, list) |
| 264 | + or len(image_size) != 2 |
| 265 | + or int(image_size[0]) <= 0 |
| 266 | + or int(image_size[1]) <= 0 |
| 267 | + ): |
| 268 | + print(f"error: invalid image_size in state dump: {image_size}", file=sys.stderr) |
| 269 | + return 1 |
| 270 | + |
| 271 | + return 0 |
| 272 | + |
| 273 | + |
| 274 | +if __name__ == "__main__": |
| 275 | + raise SystemExit(main()) |
0 commit comments