File size: 3,355 Bytes
249e7fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Check a restored tree against MANIFEST.json (no content hashes).

    python3 verify_restore.py <downloaded repo dir> <ROOT>

Links (weights, annotation archives): the downloaded file exists with the manifest size. Shards: under
<ROOT>/<extract_to>, 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()