Compute SHA256 hash with automatic optimization for large files. For file paths larger than async_threshold_mb, uses async double-buffered I/O to overlap disk reads with hash computation. For smaller files or non-path inputs (bytes, BinaryIO), falls back to synchronous hashing. Arg
(
file_path_or_obj: Union[str, Path, bytes, BinaryIO],
buffer_size_mb: Optional[int] = 16,
async_threshold_mb: int = 32,
tqdm_desc: Optional[str] = '[Calculating]',
disable_tqdm: Optional[bool] = True,
)
| 379 | |
| 380 | |
| 381 | def compute_file_hash( |
| 382 | file_path_or_obj: Union[str, Path, bytes, BinaryIO], |
| 383 | buffer_size_mb: Optional[int] = 16, |
| 384 | async_threshold_mb: int = 32, |
| 385 | tqdm_desc: Optional[str] = '[Calculating]', |
| 386 | disable_tqdm: Optional[bool] = True, |
| 387 | ) -> dict: |
| 388 | """Compute SHA256 hash with automatic optimization for large files. |
| 389 | |
| 390 | For file paths larger than async_threshold_mb, uses async double-buffered |
| 391 | I/O to overlap disk reads with hash computation. For smaller files or |
| 392 | non-path inputs (bytes, BinaryIO), falls back to synchronous hashing. |
| 393 | |
| 394 | Args: |
| 395 | file_path_or_obj: File path, bytes, or file-like object. |
| 396 | buffer_size_mb: Read buffer size in MB. Default 16MB for NAS optimization. |
| 397 | async_threshold_mb: File size threshold for async mode. Default 32MB. |
| 398 | tqdm_desc: Progress bar description. |
| 399 | disable_tqdm: Whether to disable progress bar. |
| 400 | |
| 401 | Returns: |
| 402 | dict with keys: file_path_or_obj, file_hash, file_size. |
| 403 | """ |
| 404 | # Use async mode for large files accessed by path |
| 405 | if isinstance(file_path_or_obj, (str, Path)): |
| 406 | file_size = os.path.getsize(str(file_path_or_obj)) |
| 407 | if file_size >= async_threshold_mb * 1024 * 1024: |
| 408 | return _get_file_hash_async( |
| 409 | file_path_or_obj, |
| 410 | buffer_size_mb=buffer_size_mb, |
| 411 | tqdm_desc=tqdm_desc, |
| 412 | disable_tqdm=disable_tqdm, |
| 413 | ) |
| 414 | |
| 415 | # Fallback to synchronous hashing |
| 416 | return get_file_hash( |
| 417 | file_path_or_obj, |
| 418 | buffer_size_mb=buffer_size_mb, |
| 419 | tqdm_desc=tqdm_desc, |
| 420 | disable_tqdm=disable_tqdm, |
| 421 | ) |
| 422 | |
| 423 | |
| 424 | def is_relative_path(url_or_filename: str) -> bool: |
no test coverage detected
searching dependent graphs…