|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import os |
| 6 | +import stat |
| 7 | +import sys |
| 8 | +import zipfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def parse_args() -> argparse.Namespace: |
| 13 | + parser = argparse.ArgumentParser() |
| 14 | + parser.add_argument("--build-dir", required=True) |
| 15 | + parser.add_argument("--cache-root", required=True) |
| 16 | + parser.add_argument("--expected-mode", required=True, choices=("symlink", "hardlink", "copy")) |
| 17 | + return parser.parse_args() |
| 18 | + |
| 19 | + |
| 20 | +def iter_files(root: Path): |
| 21 | + for path in root.rglob("*"): |
| 22 | + if path.is_symlink() or path.is_file(): |
| 23 | + yield path |
| 24 | + |
| 25 | + |
| 26 | +def classify_materialization(path: Path) -> tuple[str, int, int]: |
| 27 | + link_info = path.lstat() |
| 28 | + if stat.S_ISLNK(link_info.st_mode): |
| 29 | + target_size = path.stat().st_size |
| 30 | + return ("symlink", target_size, link_info.st_size) |
| 31 | + |
| 32 | + direct_info = path.stat(follow_symlinks=False) |
| 33 | + if direct_info.st_nlink > 1: |
| 34 | + return ("hardlink", direct_info.st_size, 0) |
| 35 | + |
| 36 | + return ("copy", direct_info.st_size, direct_info.st_size) |
| 37 | + |
| 38 | + |
| 39 | +def format_bytes(value: int) -> str: |
| 40 | + units = ("B", "KiB", "MiB", "GiB") |
| 41 | + size = float(value) |
| 42 | + unit = units[0] |
| 43 | + for unit in units: |
| 44 | + if size < 1024.0 or unit == units[-1]: |
| 45 | + break |
| 46 | + size /= 1024.0 |
| 47 | + if unit == "B": |
| 48 | + return f"{int(size)} {unit}" |
| 49 | + return f"{size:.2f} {unit}" |
| 50 | + |
| 51 | + |
| 52 | +def main() -> int: |
| 53 | + args = parse_args() |
| 54 | + build_dir = Path(args.build_dir).resolve() |
| 55 | + cache_root = Path(args.cache_root).resolve() |
| 56 | + media_root = build_dir / "media" |
| 57 | + if not media_root.exists(): |
| 58 | + raise SystemExit(f"Missing materialized tree: {media_root}") |
| 59 | + |
| 60 | + bunny_path = media_root / "assets/mesh/standalone/stl/Stanford_Bunny.stl" |
| 61 | + yellowflower_path = media_root / "assets/mesh/bundles/obj/yellowflower.zip" |
| 62 | + required_paths = (bunny_path, yellowflower_path) |
| 63 | + for path in required_paths: |
| 64 | + if not path.exists(): |
| 65 | + raise SystemExit(f"Missing required file: {path}") |
| 66 | + |
| 67 | + files = list(iter_files(media_root)) |
| 68 | + if not files: |
| 69 | + raise SystemExit(f"No files found under {media_root}") |
| 70 | + |
| 71 | + counts = {"symlink": 0, "hardlink": 0, "copy": 0} |
| 72 | + logical_size = 0 |
| 73 | + estimated_extra_size = 0 |
| 74 | + for path in files: |
| 75 | + materialization, file_size, extra_size = classify_materialization(path) |
| 76 | + counts[materialization] += 1 |
| 77 | + logical_size += file_size |
| 78 | + estimated_extra_size += extra_size |
| 79 | + |
| 80 | + sample_modes: dict[str, str] = {} |
| 81 | + for path in required_paths: |
| 82 | + materialization, _, _ = classify_materialization(path) |
| 83 | + sample_modes[str(path.relative_to(build_dir))] = materialization |
| 84 | + if materialization != args.expected_mode: |
| 85 | + raise SystemExit( |
| 86 | + f"Expected `{args.expected_mode}` for {path.name} but found `{materialization}`" |
| 87 | + ) |
| 88 | + |
| 89 | + if not zipfile.is_zipfile(yellowflower_path): |
| 90 | + raise SystemExit(f"Expected a zip payload at {yellowflower_path}") |
| 91 | + |
| 92 | + summary = { |
| 93 | + "build_dir": str(build_dir), |
| 94 | + "cache_root": str(cache_root), |
| 95 | + "expected_mode": args.expected_mode, |
| 96 | + "file_count": len(files), |
| 97 | + "counts": counts, |
| 98 | + "logical_size_bytes": logical_size, |
| 99 | + "logical_size_human": format_bytes(logical_size), |
| 100 | + "estimated_extra_size_bytes": estimated_extra_size, |
| 101 | + "estimated_extra_size_human": format_bytes(estimated_extra_size), |
| 102 | + "sample_modes": sample_modes, |
| 103 | + } |
| 104 | + |
| 105 | + print(json.dumps(summary, indent=2, sort_keys=True)) |
| 106 | + |
| 107 | + step_summary = os.environ.get("GITHUB_STEP_SUMMARY") |
| 108 | + if step_summary: |
| 109 | + lines = [ |
| 110 | + "## Smoke materialization summary", |
| 111 | + "", |
| 112 | + f"- Expected mode: `{args.expected_mode}`", |
| 113 | + f"- Files: `{len(files)}`", |
| 114 | + f"- Counts: `symlink={counts['symlink']}` `hardlink={counts['hardlink']}` `copy={counts['copy']}`", |
| 115 | + f"- Logical size: `{format_bytes(logical_size)}`", |
| 116 | + f"- Estimated extra size in build tree: `{format_bytes(estimated_extra_size)}`", |
| 117 | + f"- Stanford_Bunny.stl: `{sample_modes[str(bunny_path.relative_to(build_dir))]}`", |
| 118 | + f"- yellowflower.zip: `{sample_modes[str(yellowflower_path.relative_to(build_dir))]}`", |
| 119 | + ] |
| 120 | + Path(step_summary).write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 121 | + |
| 122 | + return 0 |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + sys.exit(main()) |
0 commit comments