File size: 2,484 Bytes
6b41115 | 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 | """Atomic downloads with bounded retries, checksum and optional ephemeral cache."""
from pathlib import Path
import hashlib
import os
import tempfile
import time
import fsspec
from tqdm.auto import tqdm
def sha256_file(path):
digest = hashlib.sha256()
path = Path(path)
with open(path, "rb") as handle:
with tqdm(
total=path.stat().st_size,
desc=f"Verifying {path.name}",
unit="B",
unit_scale=True,
unit_divisor=1024,
leave=False,
) as progress:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
progress.update(len(chunk))
return digest.hexdigest()
def cached_file(url, cache_dir, sha256=None, size=None, retries=3):
cache_dir = Path(cache_dir)
cache_dir.mkdir(parents=True, exist_ok=True)
dest = cache_dir / (sha256 or hashlib.sha256(url.encode()).hexdigest())
def valid(path):
return (
path.exists()
and (size is None or path.stat().st_size == size)
and (sha256 is None or sha256_file(path) == sha256)
)
if valid(dest):
return dest
error = None
for attempt in range(retries):
fd, temporary = tempfile.mkstemp(dir=cache_dir, suffix=".part")
try:
with os.fdopen(fd, "wb") as out, fsspec.open(url, "rb").open() as source:
total = size or getattr(source, "size", None)
with tqdm(
total=total,
desc=f"Downloading {Path(url).name or 'dataset shard'}",
unit="B",
unit_scale=True,
unit_divisor=1024,
leave=False,
) as progress:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
out.write(chunk)
progress.update(len(chunk))
if not valid(Path(temporary)):
raise ValueError("Downloaded file failed size/SHA256 validation")
os.replace(temporary, dest)
return dest
except Exception as exc:
error = exc
Path(temporary).unlink(missing_ok=True)
if attempt + 1 < retries:
time.sleep(min(2**attempt, 8))
raise RuntimeError(
f"Download failed after {retries} attempts; check source permissions, quota and manifest"
) from error
|