Datasets:
File size: 2,225 Bytes
f6fbcae | 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 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Verify a downloaded CASID release against release_file_hashes.csv.
Checks, for every row in the manifest:
1. the file exists
2. its size matches
3. its MD5 matches (full read)
Usage:
python verify_download.py --root CASID-dataset [--workers 8]
"""
import argparse
import csv
import hashlib
import os
import sys
from multiprocessing import Pool
CHUNK = 1 << 20
def check(task):
root, rel, size, md5 = task
path = os.path.join(root, rel)
if not os.path.isfile(path):
return rel, "MISSING"
if os.path.getsize(path) != int(size):
return rel, "SIZE_MISMATCH"
h = hashlib.md5()
try:
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(CHUNK), b""):
h.update(chunk)
except OSError as e:
return rel, "READ_ERROR(%s)" % e
return rel, "OK" if h.hexdigest() == md5 else "MD5_MISMATCH"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--root", required=True, help="downloaded dataset root")
ap.add_argument("--workers", type=int, default=8)
args = ap.parse_args()
manifest = os.path.join(args.root, "release_file_hashes.csv")
if not os.path.isfile(manifest):
sys.exit("ERROR: %s not found (download incomplete?)" % manifest)
tasks = []
with open(manifest) as f:
for row in csv.reader(f):
if row and row[0] != "path":
tasks.append((args.root, row[0], row[1], row[2]))
print("verifying %d files ..." % len(tasks))
n_ok = n_bad = 0
bad = []
with Pool(args.workers) as pool:
for rel, status in pool.imap_unordered(check, tasks, chunksize=8):
if status == "OK":
n_ok += 1
else:
n_bad += 1
bad.append((rel, status))
if (n_ok + n_bad) % 500 == 0:
print(" %d/%d" % (n_ok + n_bad, len(tasks)), flush=True)
print("RESULT: %d OK, %d bad (total %d)" % (n_ok, n_bad, len(tasks)))
for rel, status in bad[:20]:
print(" %s: %s" % (status, rel))
if n_bad:
sys.exit(1)
print("ALL FILES VERIFIED ✔")
if __name__ == "__main__":
main()
|