33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
import os
|
|
import tempfile
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
|
|
def download_file_atomic(url: str, destination: Path, *, timeout: int = 180, chunk_size: int = 1024 * 1024) -> None:
|
|
"""Download URL to destination atomically.
|
|
|
|
Writes to a temp file in the same directory and then uses os.replace()
|
|
so readers never observe partial artifacts.
|
|
"""
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
fd, tmp_name = tempfile.mkstemp(prefix=f".{destination.name}.", suffix=".part", dir=str(destination.parent))
|
|
os.close(fd)
|
|
tmp_path = Path(tmp_name)
|
|
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=timeout) as resp, tmp_path.open("wb") as out:
|
|
while True:
|
|
chunk = resp.read(chunk_size)
|
|
if not chunk:
|
|
break
|
|
out.write(chunk)
|
|
os.replace(tmp_path, destination)
|
|
finally:
|
|
try:
|
|
if tmp_path.exists():
|
|
tmp_path.unlink()
|
|
except Exception:
|
|
pass
|