-
Notifications
You must be signed in to change notification settings - Fork 657
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
✨ Add hash check to data download #284
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
"""Helper to show progress bars with `urlretrieve`. | ||
|
||
Based on https://stackoverflow.com/a/53877507 | ||
""" | ||
"""Helper to show progress bars with `urlretrieve`, check hash of file.""" | ||
|
||
# Copyright (C) 2020 Intel Corporation | ||
# | ||
|
@@ -17,7 +14,9 @@ | |
# See the License for the specific language governing permissions | ||
# and limitations under the License. | ||
|
||
import hashlib | ||
import io | ||
from pathlib import Path | ||
from typing import Dict, Iterable, Optional, Union | ||
|
||
from tqdm import tqdm | ||
|
@@ -146,7 +145,7 @@ def __init__( | |
colour: Optional[str] = None, | ||
delay: Optional[float] = 0, | ||
gui: Optional[bool] = False, | ||
**kwargs | ||
**kwargs, | ||
): | ||
super().__init__( | ||
iterable=iterable, | ||
|
@@ -175,13 +174,14 @@ def __init__( | |
colour=colour, | ||
delay=delay, | ||
gui=gui, | ||
**kwargs | ||
**kwargs, | ||
) | ||
self.total: Optional[Union[int, float]] | ||
|
||
def update_to(self, chunk_number: int = 1, max_chunk_size: int = 1, total_size=None): | ||
"""Progress bar hook for tqdm. | ||
|
||
Based on https://stackoverflow.com/a/53877507 | ||
The implementor does not have to bother about passing parameters to this as it gets them from urlretrieve. | ||
However the context needs a few parameters. Refer to the example. | ||
|
||
|
@@ -193,3 +193,16 @@ def update_to(self, chunk_number: int = 1, max_chunk_size: int = 1, total_size=N | |
if total_size is not None: | ||
self.total = total_size | ||
self.update(chunk_number * max_chunk_size - self.n) | ||
|
||
|
||
def hash_check(file_path: Path, expected_hash: str): | ||
"""Raise assert error if hash does not match the calculated hash of the file. | ||
|
||
Args: | ||
file_path (Path): Path to file. | ||
expected_hash (str): Expected hash of the file. | ||
""" | ||
with open(file_path, "rb") as hash_file: | ||
assert ( | ||
hashlib.md5(hash_file.read()).hexdigest() == expected_hash | ||
), f"Downloaded file {file_path} does match the required hash." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be "does not match" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙃 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quite minor, but btech has
zip_filename
, would be good if the two has consistent implementation, (ie., both haszip_filename
or path)