File size: 606 Bytes
3a2e5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""File-hashing helper used by the paper-notebook freeze CI check."""

from __future__ import annotations

import hashlib
from pathlib import Path

_CHUNK = 64 * 1024


def sha256_file(path: str | Path) -> str:
    """Return the hex-digest SHA-256 of a file, streaming 64KB chunks.

    Streaming (rather than ``open(...).read()``) keeps memory bounded for
    notebooks with embedded image outputs that can hit hundreds of MB.
    """
    h = hashlib.sha256()
    path = Path(path)
    with path.open("rb") as f:
        while chunk := f.read(_CHUNK):
            h.update(chunk)
    return h.hexdigest()