| 341 | |
| 342 | |
| 343 | def get_file_checksum(fpath: pathlib.Path, hash_name: str) -> str: |
| 344 | with fpath.open("rb") as rfh: |
| 345 | try: |
| 346 | digest = hashlib.file_digest(rfh, hash_name) # type: ignore[attr-defined] |
| 347 | except AttributeError: |
| 348 | # Python < 3.14 |
| 349 | buf = bytearray(2**18) # Reusable buffer to reduce allocations. |
| 350 | view = memoryview(buf) |
| 351 | digest = getattr(hashlib, hash_name)() |
| 352 | while True: |
| 353 | size = rfh.readinto(buf) |
| 354 | if size == 0: |
| 355 | break # EOF |
| 356 | digest.update(view[:size]) |
| 357 | hexdigest: str = digest.hexdigest() |
| 358 | return hexdigest |
| 359 | |
| 360 | |
| 361 | def download_file( |