-
-
Notifications
You must be signed in to change notification settings - Fork 203
Segment large files for upload without needing tons of RAM #551
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
Open
Starblade42
wants to merge
1
commit into
pycontribs:master
Choose a base branch
from
Starblade42:working
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -422,6 +422,54 @@ def test_update_exc(self): | |
| ret = utils.update_exc(err, msg2, before=False, separator=sep) | ||
| self.assertEqual(ret.message, exp) | ||
|
|
||
| def test_read_in_chunks(self): | ||
| # create junk file to test size. | ||
| source_file = "source_file.dat" | ||
| target_file = "target_file.dat" | ||
| def compare_contents(source_file_name, target_file_name): | ||
| # compare whatever is contained in the target file to the first part of the source file | ||
| with open(source_file_name, "rb") as source_handle, open(target_file_name, "rb") as target_handle: | ||
| read_size = os.path.getsize(target_file_name) | ||
| target_contents = target_handle.read(read_size) | ||
| source_contents = source_handle.read(read_size) | ||
| self.assertEqual(target_contents, source_contents) | ||
| # Try block is just to make sure we delete the files. | ||
| # Using only main python libraries to hopefully improve test reliability. | ||
| try: | ||
| # Write something into the source file | ||
| with open(source_file, "wb") as source: | ||
| source.write(os.urandom(1024)) | ||
| # Make sure it's the size we're expecting | ||
| self.assertEqual(1024, os.path.getsize(source_file)) | ||
| # Now test different sizing cases for file consistency. | ||
| # test max_size smaller than chunk size | ||
| with open(target_file, "wb") as target, open(source_file, "rb") as source: | ||
| for chunk in utils.read_in_chunks(source, max_size=1, chunk_size=1024): | ||
| target.write(chunk) | ||
| compare_contents(source_file, target_file) | ||
| os.unlink(target_file) | ||
| # test max_size larger than chunk size | ||
| with open(target_file, "wb") as target, open(source_file, "rb") as source: | ||
| for chunk in utils.read_in_chunks(source, max_size=512, chunk_size=64): | ||
| target.write(chunk) | ||
| compare_contents(source_file, target_file) | ||
| os.unlink(target_file) | ||
| # test max_size equal with chunk size | ||
|
Collaborator
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.
|
||
| with open(target_file, "wb") as target, open(source_file, "rb") as source: | ||
| for chunk in utils.read_in_chunks(source, max_size=512, chunk_size=512): | ||
| target.write(chunk) | ||
| compare_contents(source_file, target_file) | ||
| os.unlink(target_file) | ||
| os.unlink(source_file) | ||
|
|
||
| except: | ||
| raise | ||
| finally: | ||
| # Remove the test files we made! | ||
| if os.path.exists(source_file): | ||
| os.unlink(source_file) | ||
| if os.path.exists(target_file): | ||
| os.unlink(target_file) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
Instead of hard-coding the temporary file names, why not use TemporaryFile?