|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regression check for OpenGL OCIO preview with multiple startup images.""" |
| 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 preview draw failed", |
| 19 | + "OpenGL OCIO preview draw failed", |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def _repo_root() -> Path: |
| 24 | + return Path(__file__).resolve().parents[3] |
| 25 | + |
| 26 | + |
| 27 | +def _default_binary(repo_root: Path) -> Path: |
| 28 | + candidates = [ |
| 29 | + repo_root / "build_u" / "bin" / "imiv", |
| 30 | + repo_root / "build" / "bin" / "imiv", |
| 31 | + repo_root / "build_u" / "src" / "imiv" / "imiv", |
| 32 | + repo_root / "build" / "src" / "imiv" / "imiv", |
| 33 | + repo_root / "build" / "Debug" / "imiv.exe", |
| 34 | + repo_root / "build" / "Release" / "imiv.exe", |
| 35 | + ] |
| 36 | + for candidate in candidates: |
| 37 | + if candidate.exists(): |
| 38 | + return candidate |
| 39 | + return candidates[0] |
| 40 | + |
| 41 | + |
| 42 | +def _load_env_from_script(script_path: Path) -> dict[str, str]: |
| 43 | + env = dict(os.environ) |
| 44 | + if not script_path.exists() or shutil.which("bash") is None: |
| 45 | + return env |
| 46 | + |
| 47 | + quoted = shlex.quote(str(script_path)) |
| 48 | + proc = subprocess.run( |
| 49 | + ["bash", "-lc", f"source {quoted} >/dev/null 2>&1; env -0"], |
| 50 | + check=True, |
| 51 | + stdout=subprocess.PIPE, |
| 52 | + ) |
| 53 | + for item in proc.stdout.split(b"\0"): |
| 54 | + if not item: |
| 55 | + continue |
| 56 | + key, _, value = item.partition(b"=") |
| 57 | + if not key: |
| 58 | + continue |
| 59 | + env[key.decode("utf-8", errors="ignore")] = value.decode( |
| 60 | + "utf-8", errors="ignore" |
| 61 | + ) |
| 62 | + return env |
| 63 | + |
| 64 | + |
| 65 | +def main() -> int: |
| 66 | + repo_root = _repo_root() |
| 67 | + runner = repo_root / "src" / "imiv" / "tools" / "imiv_gui_test_run.py" |
| 68 | + default_env_script = repo_root / "build_u" / "imiv_env.sh" |
| 69 | + default_out_dir = ( |
| 70 | + repo_root / "build_u" / "imiv_captures" / "opengl_multiopen_ocio_regression" |
| 71 | + ) |
| 72 | + default_image = repo_root / "ASWF" / "logos" / "openimageio-stacked-gradient.png" |
| 73 | + |
| 74 | + ap = argparse.ArgumentParser(description=__doc__) |
| 75 | + ap.add_argument("--bin", default=str(_default_binary(repo_root)), help="imiv executable") |
| 76 | + ap.add_argument("--cwd", default="", help="Working directory for imiv") |
| 77 | + ap.add_argument("--backend", default="opengl", help="Runtime backend override") |
| 78 | + ap.add_argument( |
| 79 | + "--env-script", default=str(default_env_script), help="Optional shell env setup script" |
| 80 | + ) |
| 81 | + ap.add_argument("--out-dir", default=str(default_out_dir), help="Output directory") |
| 82 | + ap.add_argument("--open", default=str(default_image), help="Source image to duplicate") |
| 83 | + ap.add_argument("--trace", action="store_true", help="Enable runner tracing") |
| 84 | + args = ap.parse_args() |
| 85 | + |
| 86 | + exe = Path(args.bin).resolve() |
| 87 | + if not exe.exists(): |
| 88 | + print(f"error: binary not found: {exe}", file=sys.stderr) |
| 89 | + return 2 |
| 90 | + |
| 91 | + cwd = Path(args.cwd).resolve() if args.cwd else exe.parent.resolve() |
| 92 | + out_dir = Path(args.out_dir).resolve() |
| 93 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 94 | + |
| 95 | + image_a = Path(args.open).resolve() |
| 96 | + if not image_a.exists(): |
| 97 | + print(f"error: image not found: {image_a}", file=sys.stderr) |
| 98 | + return 2 |
| 99 | + image_b = out_dir / f"{image_a.stem}_copy{image_a.suffix}" |
| 100 | + shutil.copyfile(image_a, image_b) |
| 101 | + |
| 102 | + env = _load_env_from_script(Path(args.env_script).resolve()) |
| 103 | + env["IMIV_CONFIG_HOME"] = str(out_dir / "cfg") |
| 104 | + |
| 105 | + screenshot_path = out_dir / "opengl_multiopen_ocio.png" |
| 106 | + layout_path = out_dir / "opengl_multiopen_ocio.layout.json" |
| 107 | + state_path = out_dir / "opengl_multiopen_ocio.state.json" |
| 108 | + log_path = out_dir / "opengl_multiopen_ocio.log" |
| 109 | + |
| 110 | + cmd = [ |
| 111 | + sys.executable, |
| 112 | + str(runner), |
| 113 | + "--bin", |
| 114 | + str(exe), |
| 115 | + "--cwd", |
| 116 | + str(cwd), |
| 117 | + "--backend", |
| 118 | + args.backend, |
| 119 | + "--open", |
| 120 | + str(image_a), |
| 121 | + "--open", |
| 122 | + str(image_b), |
| 123 | + "--ocio-use", |
| 124 | + "true", |
| 125 | + "--screenshot-out", |
| 126 | + str(screenshot_path), |
| 127 | + "--layout-json-out", |
| 128 | + str(layout_path), |
| 129 | + "--layout-items", |
| 130 | + "--state-json-out", |
| 131 | + str(state_path), |
| 132 | + ] |
| 133 | + if args.trace: |
| 134 | + cmd.append("--trace") |
| 135 | + |
| 136 | + with log_path.open("w", encoding="utf-8") as log_handle: |
| 137 | + proc = subprocess.run( |
| 138 | + cmd, |
| 139 | + cwd=str(repo_root), |
| 140 | + env=env, |
| 141 | + check=False, |
| 142 | + stdout=log_handle, |
| 143 | + stderr=subprocess.STDOUT, |
| 144 | + timeout=90, |
| 145 | + ) |
| 146 | + if proc.returncode != 0: |
| 147 | + print(f"error: runner exited with code {proc.returncode}", file=sys.stderr) |
| 148 | + return 1 |
| 149 | + |
| 150 | + for required in (screenshot_path, layout_path, state_path): |
| 151 | + if not required.exists(): |
| 152 | + print(f"error: missing output: {required}", file=sys.stderr) |
| 153 | + return 1 |
| 154 | + |
| 155 | + log_text = log_path.read_text(encoding="utf-8", errors="ignore") |
| 156 | + for pattern in ERROR_PATTERNS: |
| 157 | + if pattern in log_text: |
| 158 | + print(f"error: found runtime error pattern: {pattern}", file=sys.stderr) |
| 159 | + return 1 |
| 160 | + |
| 161 | + layout = json.loads(layout_path.read_text(encoding="utf-8")) |
| 162 | + if not any(window.get("name") == "Image" for window in layout.get("windows", [])): |
| 163 | + print("error: layout dump missing Image window", file=sys.stderr) |
| 164 | + return 1 |
| 165 | + |
| 166 | + state = json.loads(state_path.read_text(encoding="utf-8")) |
| 167 | + if not state.get("image_path"): |
| 168 | + print("error: state dump missing image_path", file=sys.stderr) |
| 169 | + return 1 |
| 170 | + if int(state.get("loaded_image_count", 0)) < 2: |
| 171 | + print("error: expected at least 2 loaded images in session", file=sys.stderr) |
| 172 | + return 1 |
| 173 | + |
| 174 | + return 0 |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + raise SystemExit(main()) |
0 commit comments