Coverage for scripts/ml_manifest.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""SHA-256 manifest of the INSTALLED model files (ml/ top level and ml/expert_models), identical output on every machine. 

2 

3 python scripts/ml_manifest.py [--ml-dir ml] -> "<sha256> <relative path>" per file, sorted 

4 

5Compare the local and the Codespace manifest with `diff` before trusting an evaluation that ran on the other side; 

6datasets, versions, backups and evaluation reports are not installed models and are left out. 

7""" 

8 

9from __future__ import annotations 

10 

11import argparse 

12import hashlib 

13from pathlib import Path 

14 

15INSTALLED_SUFFIXES = {".json", ".pt", ".pth", ".npz", ".npy", ".onnx", ".bin"} 

16SKIP_TOP_LEVEL = {"datasets", "versions", "evaluation", "expert_datasets", "cache"} 

17 

18 

19def installed_files(ml_dir: Path) -> list[Path]: 

20 files = [path for path in ml_dir.iterdir() if path.is_file() and path.suffix in INSTALLED_SUFFIXES] 

21 experts = ml_dir / "expert_models" 

22 if experts.is_dir(): 

23 files.extend(path for path in experts.rglob("*") if path.is_file() and path.suffix in INSTALLED_SUFFIXES) 

24 return sorted(files, key=lambda path: path.relative_to(ml_dir).as_posix()) 

25 

26 

27def manifest_lines(ml_dir: Path) -> list[str]: 

28 lines = [] 

29 for path in installed_files(ml_dir): 

30 digest = hashlib.sha256(path.read_bytes()).hexdigest() 

31 lines.append(f"{digest} {path.relative_to(ml_dir).as_posix()}") 

32 return lines 

33 

34 

35def main() -> int: 

36 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 

37 parser.add_argument("--ml-dir", default=str(Path(__file__).resolve().parents[1] / "ml")) 

38 print("\n".join(manifest_lines(Path(parser.parse_args().ml_dir)))) 

39 return 0 

40 

41 

42if __name__ == "__main__": 

43 raise SystemExit(main())