Linwei-Chen commited on
Commit
f6fbcae
·
verified ·
1 Parent(s): a77c812

CASID: richer dataset card (paper info, usage, verification), add verify script

Browse files
Files changed (1) hide show
  1. scripts/verify_download.py +78 -0
scripts/verify_download.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """Verify a downloaded CASID release against release_file_hashes.csv.
4
+
5
+ Checks, for every row in the manifest:
6
+ 1. the file exists
7
+ 2. its size matches
8
+ 3. its MD5 matches (full read)
9
+
10
+ Usage:
11
+ python verify_download.py --root CASID-dataset [--workers 8]
12
+ """
13
+ import argparse
14
+ import csv
15
+ import hashlib
16
+ import os
17
+ import sys
18
+ from multiprocessing import Pool
19
+
20
+ CHUNK = 1 << 20
21
+
22
+
23
+ def check(task):
24
+ root, rel, size, md5 = task
25
+ path = os.path.join(root, rel)
26
+ if not os.path.isfile(path):
27
+ return rel, "MISSING"
28
+ if os.path.getsize(path) != int(size):
29
+ return rel, "SIZE_MISMATCH"
30
+ h = hashlib.md5()
31
+ try:
32
+ with open(path, "rb") as f:
33
+ for chunk in iter(lambda: f.read(CHUNK), b""):
34
+ h.update(chunk)
35
+ except OSError as e:
36
+ return rel, "READ_ERROR(%s)" % e
37
+ return rel, "OK" if h.hexdigest() == md5 else "MD5_MISMATCH"
38
+
39
+
40
+ def main():
41
+ ap = argparse.ArgumentParser()
42
+ ap.add_argument("--root", required=True, help="downloaded dataset root")
43
+ ap.add_argument("--workers", type=int, default=8)
44
+ args = ap.parse_args()
45
+
46
+ manifest = os.path.join(args.root, "release_file_hashes.csv")
47
+ if not os.path.isfile(manifest):
48
+ sys.exit("ERROR: %s not found (download incomplete?)" % manifest)
49
+
50
+ tasks = []
51
+ with open(manifest) as f:
52
+ for row in csv.reader(f):
53
+ if row and row[0] != "path":
54
+ tasks.append((args.root, row[0], row[1], row[2]))
55
+ print("verifying %d files ..." % len(tasks))
56
+
57
+ n_ok = n_bad = 0
58
+ bad = []
59
+ with Pool(args.workers) as pool:
60
+ for rel, status in pool.imap_unordered(check, tasks, chunksize=8):
61
+ if status == "OK":
62
+ n_ok += 1
63
+ else:
64
+ n_bad += 1
65
+ bad.append((rel, status))
66
+ if (n_ok + n_bad) % 500 == 0:
67
+ print(" %d/%d" % (n_ok + n_bad, len(tasks)), flush=True)
68
+
69
+ print("RESULT: %d OK, %d bad (total %d)" % (n_ok, n_bad, len(tasks)))
70
+ for rel, status in bad[:20]:
71
+ print(" %s: %s" % (status, rel))
72
+ if n_bad:
73
+ sys.exit(1)
74
+ print("ALL FILES VERIFIED ✔")
75
+
76
+
77
+ if __name__ == "__main__":
78
+ main()