Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler file chunking #7673

Merged
merged 4 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions packaging/wheel/relocate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import glob
import hashlib
import io

# Standard library imports
import os
Expand Down Expand Up @@ -65,21 +64,12 @@
PYTHON_VERSION = sys.version_info


def read_chunks(file, size=io.DEFAULT_BUFFER_SIZE):
"""Yield pieces of data from a file-like object until EOF."""
while True:
chunk = file.read(size)
if not chunk:
break
yield chunk


def rehash(path, blocksize=1 << 20):
"""Return (hash, length) for path using hashlib.sha256()"""
h = hashlib.sha256()
length = 0
with open(path, "rb") as f:
for block in read_chunks(f, size=blocksize):
while block := f.read(blocksize):
length += len(block)
h.update(block)
digest = "sha256=" + urlsafe_b64encode(h.digest()).decode("latin1").rstrip("=")
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def calculate_md5(fpath: str, chunk_size: int = 1024 * 1024) -> str:
else:
md5 = hashlib.md5()
with open(fpath, "rb") as f:
for chunk in iter(lambda: f.read(chunk_size), b""):
while chunk := f.read(chunk_size):
md5.update(chunk)
return md5.hexdigest()

Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datasets/_builtin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import hashlib
def sha256sum(path, chunk_size=1024 * 1024):
checksum = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(chunk_size), b""):
while chunk := f.read(chunk_size):
checksum.update(chunk)
print(checksum.hexdigest())
```
Expand Down
2 changes: 1 addition & 1 deletion torchvision/prototype/datasets/utils/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def download(self, root: Union[str, pathlib.Path], *, skip_integrity_check: bool
def _check_sha256(self, path: pathlib.Path, *, chunk_size: int = 1024 * 1024) -> None:
hash = hashlib.sha256()
with open(path, "rb") as file:
for chunk in iter(lambda: file.read(chunk_size), b""):
while chunk := file.read(chunk_size):
hash.update(chunk)
sha256 = hash.hexdigest()
if sha256 != self.sha256:
Expand Down