generated from pyiron/pyiron_module_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement compress #54
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5dbbb5b
Implement compress
samwaseda f8dea65
Add tests for exclude files
samwaseda 3c4253f
black
samwaseda 656a738
ruff and mypy
samwaseda d3fafc7
forgot to add cast
samwaseda 92cbe4c
other tricks
samwaseda 38189fa
correct tests?
samwaseda cc85284
delete archive first
samwaseda fa9a337
add decompress
samwaseda 0e60d49
Add filter because it issues a warning
samwaseda f34e492
Merge branch 'directory' into compress
samwaseda 4ff35e8
Merge branch 'directory' into compress
samwaseda 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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||
from __future__ import annotations | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
import tarfile | ||||||||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||||||||
from typing import cast | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def delete_files_and_directories_recursively(path): | ||||||||||||||||||||||||||||
|
@@ -44,11 +46,12 @@ def __init__( | |||||||||||||||||||||||||||
self, directory: str | Path | DirectoryObject, protected: bool = False | ||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||
if isinstance(directory, str): | ||||||||||||||||||||||||||||
self.path = Path(directory) | ||||||||||||||||||||||||||||
path = Path(directory) | ||||||||||||||||||||||||||||
elif isinstance(directory, Path): | ||||||||||||||||||||||||||||
self.path = directory | ||||||||||||||||||||||||||||
path = directory | ||||||||||||||||||||||||||||
elif isinstance(directory, DirectoryObject): | ||||||||||||||||||||||||||||
self.path = directory.path | ||||||||||||||||||||||||||||
path = directory.path | ||||||||||||||||||||||||||||
self.path: Path = path | ||||||||||||||||||||||||||||
self.create() | ||||||||||||||||||||||||||||
self._protected = protected | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -97,3 +100,35 @@ def remove_files(self, *files: str): | |||||||||||||||||||||||||||
path = self.get_path(file) | ||||||||||||||||||||||||||||
if path.is_file(): | ||||||||||||||||||||||||||||
path.unlink() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def compress(self, exclude_files: list[str | Path] | None = None): | ||||||||||||||||||||||||||||
directory = self.path.resolve() | ||||||||||||||||||||||||||||
output_tar_path = directory.with_suffix(".tar.gz") | ||||||||||||||||||||||||||||
if output_tar_path.exists(): | ||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||
Comment on lines
+104
to
+108
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. The method silently returns without indication when the target file already exists. Consider raising an exception, returning a boolean status, or providing an overwrite parameter to make this behavior explicit to callers.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
if exclude_files is None: | ||||||||||||||||||||||||||||
exclude_files = [] | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
exclude_files = [Path(f) for f in exclude_files] | ||||||||||||||||||||||||||||
exclude_set = { | ||||||||||||||||||||||||||||
f.resolve() if f.is_absolute() else (directory / f).resolve() | ||||||||||||||||||||||||||||
for f in cast(list[Path], exclude_files) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
files_to_delete = [] | ||||||||||||||||||||||||||||
with tarfile.open(output_tar_path, "w:gz") as tar: | ||||||||||||||||||||||||||||
for file in directory.rglob("*"): | ||||||||||||||||||||||||||||
if file.is_file() and file.resolve() not in exclude_set: | ||||||||||||||||||||||||||||
arcname = file.relative_to(directory) | ||||||||||||||||||||||||||||
tar.add(file, arcname=arcname) | ||||||||||||||||||||||||||||
files_to_delete.append(file) | ||||||||||||||||||||||||||||
for file in files_to_delete: | ||||||||||||||||||||||||||||
file.unlink() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def decompress(self): | ||||||||||||||||||||||||||||
directory = self.path.resolve() | ||||||||||||||||||||||||||||
tar_path = directory.with_suffix(".tar.gz") | ||||||||||||||||||||||||||||
if not tar_path.exists(): | ||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||
with tarfile.open(tar_path, "r:gz") as tar: | ||||||||||||||||||||||||||||
tar.extractall(path=directory, filter="fully_trusted") | ||||||||||||||||||||||||||||
tar_path.unlink() |
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
Oops, something went wrong.
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.
The compress method lacks docstring documentation. It should document the purpose, parameters (especially the exclude_files behavior), return value, and the silent behavior when target file exists.
Copilot uses AI. Check for mistakes.