-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Delete extracted files when loading dataset #2631
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1d82a20
Test load_dataset deletes extracted files
albertvillanova 7512fa8
Create patching submodule
albertvillanova dfc151b
Implement patching for deleting file on close
albertvillanova 174ca18
Use patching for deleting file on close in ArrowBasedBuilder
albertvillanova 9aeb283
Merge remote-tracking branch 'upstream/master' into gh-2481-2604
albertvillanova 891492b
Implement delete_extracted_paths in DownloadManager
albertvillanova fc5e7f7
Delete extracted files with DownloadManager instead of patching
albertvillanova 6217fea
Add dummy delete_extracted_paths to MockDownloadManager
albertvillanova 8b2cffd
Rename delete_extracted_paths to delete_extracted_files
albertvillanova 55f4421
Add option to keep extracted files
albertvillanova 4a2c529
Remove patching
albertvillanova afa07da
Test option to keep extracted files
albertvillanova 903fee9
Filter extracted paths to files
albertvillanova 3e19c8d
Set delete_extracted=False by default
albertvillanova 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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import importlib | ||
| import os | ||
| from contextlib import contextmanager | ||
|
|
||
| from .logging import get_logger | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class _PatchedModuleObj: | ||
| """Set all the modules components as attributes of the _PatchedModuleObj object""" | ||
|
|
||
| def __init__(self, module): | ||
| if module is not None: | ||
| for key in getattr(module, "__all__", module.__dict__): | ||
| if not key.startswith("__"): | ||
| setattr(self, key, getattr(module, key)) | ||
|
|
||
|
|
||
| class patch_submodule: | ||
| """ | ||
| Patch a submodule attribute of an object, by keeping all other submodules intact at all levels. | ||
|
|
||
| Example:: | ||
|
|
||
| >>> import importlib | ||
| >>> from datasets.load import prepare_module | ||
| >>> from datasets.streaming import patch_submodule, xjoin | ||
| >>> | ||
| >>> snli_module_path, _ = prepare_module("snli") | ||
| >>> snli_module = importlib.import_module(snli_module_path) | ||
| >>> patcher = patch_submodule(snli_module, "os.path.join", xjoin) | ||
| >>> patcher.start() | ||
| >>> assert snli_module.os.path.join is xjoin | ||
| """ | ||
|
|
||
| _active_patches = [] | ||
|
|
||
| def __init__(self, obj, target: str, new): | ||
| self.obj = obj | ||
| self.target = target | ||
| self.new = new | ||
| self.key = target.split(".")[0] | ||
| self.original = getattr(obj, self.key, None) | ||
|
|
||
| def __enter__(self): | ||
| *submodules, attr = self.target.split(".") | ||
| current = self.obj | ||
| for key in submodules: | ||
| setattr(current, key, _PatchedModuleObj(getattr(current, key, None))) | ||
| current = getattr(current, key) | ||
| setattr(current, attr, self.new) | ||
|
|
||
| def __exit__(self, *exc_info): | ||
| setattr(self.obj, self.key, self.original) | ||
|
|
||
| def start(self): | ||
| """Activate a patch.""" | ||
| self.__enter__() | ||
| self._active_patches.append(self) | ||
|
|
||
| def stop(self): | ||
| """Stop an active patch.""" | ||
| try: | ||
| self._active_patches.remove(self) | ||
| except ValueError: | ||
| # If the patch hasn't been started this will fail | ||
| return None | ||
|
|
||
| return self.__exit__() | ||
|
|
||
|
|
||
| @contextmanager | ||
| def extend_module_for_deleting_file_on_close(module_path): | ||
| @contextmanager | ||
| def open_and_delete_on_close(file, *args, **kwargs): | ||
| f = open(file, *args, **kwargs) | ||
| try: | ||
| yield f | ||
| finally: | ||
| f.close() | ||
| os.remove(file) | ||
|
|
||
| module = importlib.import_module(module_path) | ||
| patch = patch_submodule(module, "open", open_and_delete_on_close) | ||
| patch.start() | ||
| try: | ||
| yield patch | ||
| finally: | ||
| patch.stop() | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.