| 1 | import hashlib |
| 2 | import json |
| 3 | from pathlib import Path |
| 4 | import re |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import tomllib |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parents[2] |
| 10 | HERE = Path(__file__).resolve().parent |
| 11 | MANIFEST = ROOT / "experiments/rust-ui-memory-app/Cargo.toml" |
| 12 | TARGETS = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc"] |
| 13 | |
| 14 | |
| 15 | def sha(data): |
| 16 | return hashlib.sha256(data).hexdigest() |
| 17 | |
| 18 | |
| 19 | def cargo(*args): |
| 20 | return subprocess.check_output(["cargo", "+1.99.1", *args, "--locked", "--offline", "--manifest-path", str(MANIFEST)], cwd=ROOT, text=True) |
| 21 | |
| 22 | |
| 23 | def main(): |
| 24 | if sys.flags.optimize: |
| 25 | raise RuntimeError("최적화 모드에서는 감사할 수 없음") |
| 26 | locks = [tomllib.loads((ROOT / path).read_text())["package"] for path in ["tests/rust-ui-memory-app/reference-ui.lock", "experiments/rust-app-core-memory/Cargo.lock"]] |
| 27 | allowed = {(p["name"], p["version"], p.get("checksum")) for lock in locks for p in lock if p.get("source")} |
| 28 | current = tomllib.loads(MANIFEST.with_name("Cargo.lock").read_text())["package"] |
| 29 | records = {(p["name"], p["version"], p.get("checksum")) for p in current if p.get("source")} |
| 30 | if not records <= allowed: |
| 31 | raise RuntimeError("기존 lock 외 registry 항목") |
| 32 | results = [] |
| 33 | for target in TARGETS: |
| 34 | for ui in [False, True]: |
| 35 | flags = ["--features", "ui"] if ui else ["--no-default-features"] |
| 36 | metadata = json.loads(cargo("metadata", "--format-version", "1", "--filter-platform", target, *flags)) |
| 37 | packages = {(p["name"], p["version"]): p for p in metadata["packages"]} |
| 38 | tree = cargo("tree", "--target", target, "--prefix", "none", "--format", "{p}|{f}", "-e", "normal,build", *flags) |
| 39 | active = {} |
| 40 | for line in tree.splitlines(): |
| 41 | match = re.fullmatch(r"([^ ]+) v([^ ]+)(?: \([^|]*\))?\|([^|]*?)(?: \(\*\))?", line) |
| 42 | if match is None: |
| 43 | raise RuntimeError("Cargo tree 행 해석 실패: " + line) |
| 44 | name, version, features = match.groups() |
| 45 | active.setdefault((name, version), set()).update(filter(None, features.split(","))) |
| 46 | selected = [] |
| 47 | for key, features in sorted(active.items()): |
| 48 | package = packages[key] |
| 49 | item = {"name": key[0], "version": key[1], "features": sorted(features), "license": package["license"], "source": package["source"]} |
| 50 | if package["source"]: |
| 51 | lock = next(p for p in current if (p["name"], p["version"]) == key) |
| 52 | source = Path(package["manifest_path"]).parent |
| 53 | registry = source.parent.parent.parent |
| 54 | archive = registry / "cache" / source.parent.name / (key[0] + "-" + key[1] + ".crate") |
| 55 | if sha(archive.read_bytes()) != lock["checksum"]: |
| 56 | raise RuntimeError("캐시 checksum 불일치: " + str(key)) |
| 57 | item["checksum"] = lock["checksum"] |
| 58 | item["license_files"] = [{"path": str(p.relative_to(source)), "sha256": sha(p.read_bytes())} for p in sorted(source.rglob("*")) if p.is_file() and not p.is_symlink() and p.name.lower().startswith(("license", "licence", "copying", "notice"))] |
| 59 | else: |
| 60 | item["source_path"] = str(Path(package["manifest_path"]).parent.relative_to(ROOT)) |
| 61 | selected.append(item) |
| 62 | results.append({"target": target, "ui": ui, "selected_packages": len(selected), "packages": selected}) |
| 63 | report = {"scope": "플랫폼별 locked/offline 선택 분석임. Linux 외 빌드나 실행 검증이 아님", "original_ui_head": "63b80e3158c08b71ec70666a2a39216cd36752b1", "core_head": "23b8d1c615f24f42469138fcc63e7fce7cda4f24", "registry_records": len(records), "new_registry_outside_original_union": 0, "selections": results} |
| 64 | (HERE / "dependencies.json").write_text(json.dumps(report, ensure_ascii=False, indent=4) + "\n") |
| 65 | print(json.dumps([{k: x[k] for k in ["target", "ui", "selected_packages"]} for x in results])) |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |