refactor: share the git blob check of downloads

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:01 +01:00
1 parent 8d58a9edfd
commit fd0f6c7798
6 files changed
+55 -48

No files matched your search

+21
View File
@@ -18,6 +18,7 @@ from typing import Any, Callable, NoReturn, TypeVar
from core.github import github_token
from core.artifacts import copy_stream_atomic, write_text_atomic
from core.hashing import git_blob_sha
from core.upstream import (
MAX_RESPONSE_SIZE,
RateLimitError,
@@ -259,6 +260,26 @@ def download(
return _open(urllib.request.Request(url, headers=headers or {}), save, timeout)
class BlobMismatchError(ValueError):
"""A downloaded file is not the blob the repository listing names."""
def fetch_verified_blob(url: str, destination: Path, blob_sha: str, context: str = "") -> Path:
"""`destination`, downloaded from `url` unless the cached copy already is
git blob `blob_sha`, and checked against it.
The cache is addressed by content: a file already there is kept only
while its blob sha is the one the repository lists.
"""
if destination.is_file() and git_blob_sha(destination.read_bytes()) != blob_sha:
destination.unlink()
download(url, destination, skip_existing=True)
if git_blob_sha(destination.read_bytes()) != blob_sha:
destination.unlink()
raise BlobMismatchError(f"{url} is not blob {blob_sha}{context}")
return destination
def github_json(
url: str, failure: str, timeout: float = 30, error: type[Exception] = RuntimeError
) -> Any: