#!/usr/bin/env python3 """Check a restored tree against MANIFEST.json (no content hashes). python3 verify_restore.py Links (weights, annotation archives): the downloaded file exists with the manifest size. Shards: under /, their entries hold exactly the recorded non-directory file count and bytes (symlinks count as files, as in ceph rstats). Annotation archives: per train part / validation / test, the regular files outside `cameras/` match the counts in data/annotations/COMPLETE.json. """ import concurrent.futures import json import os import sys from pathlib import Path def count(path, skip_cameras=False, regular_only=False): if path.is_symlink() or not path.is_dir(): return 1, path.lstat().st_size files = size = 0 for directory, subdirs, names in os.walk(path): if skip_cameras and 'cameras' in subdirs: subdirs.remove('cameras') if not regular_only: names = names + [d for d in subdirs if os.path.islink(os.path.join(directory, d))] for name in names: stat = os.lstat(os.path.join(directory, name)) if regular_only and not os.path.isfile(os.path.join(directory, name)): continue files += 1 size += stat.st_size return files, size def check_shard(args): root, shard = args files = size = 0 for entry in shard['entries']: path = root / shard['extract_to'] / entry if not os.path.lexists(path): return f"{shard['path']}: missing {path}" n, b = count(path) files += n size += b if (files, size) != (shard['files'], shard['bytes']): return f"{shard['path']}: {files} files / {size} bytes, expected {shard['files']} / {shard['bytes']}" return None def check_annotations(args): data, record = args split, _, part = record['archive'][:-len('.tar')].partition('_') files = size = 0 for clip in sorted((data / split / part).iterdir()): n, b = count(clip, skip_cameras=True, regular_only=True) files += n size += b if (files, size) != (record['files'], record['source_bytes']): return f"{record['archive']}: {files} files / {size} bytes, expected {record['files']} / {record['source_bytes']}" return None def main(): repo, root = Path(sys.argv[1]).resolve(), Path(sys.argv[2]).resolve() manifest = json.loads((repo / 'MANIFEST.json').read_text()) errors = [f"{l['path']}: missing or size differs" for l in manifest['links'] if not (repo / l['path']).is_file() or (repo / l['path']).stat().st_size != l['bytes']] complete = json.loads((repo / 'data/annotations/COMPLETE.json').read_text()) with concurrent.futures.ProcessPoolExecutor(16) as executor: errors += [e for e in executor.map(check_shard, [(root, s) for s in manifest['shards']]) if e] errors += [e for e in executor.map(check_annotations, [(root / 'dataset/data', r) for r in complete['archives']]) if e] print('\n'.join(errors) or f"OK: {len(manifest['links'])} files, {len(manifest['shards'])} shards, " f"{len(complete['archives'])} annotation archives") sys.exit(1 if errors else 0) if __name__ == '__main__': main()