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

Modify atomic_contents_add to accept a filter function #71

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion securetar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _is_excluded_by_filter(path: PurePath, exclude_list: list[str]) -> bool:
"""Filter to filter excludes."""

for exclude in exclude_list:
if not path.match(exclude):
if not path.full_match(exclude):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That not work as we work with PurePath (without any blocking stats). That is the nice thing on that function to keep it fast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the full_match method with improved glob support was added in Python 3.13: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.full_match

continue
_LOGGER.debug("Ignoring %s because of %s", path, exclude)
return True
Expand Down
36 changes: 14 additions & 22 deletions tests/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,24 @@ def test_not_secure_path() -> None:
assert [] == list(secure_path(test_list))


def test_is_excluded_by_filter_good() -> None:
def test_is_excluded_by_filter() -> None:
"""Test exclude filter."""
filter_list = ["not/match", "/dev/xy"]
filter_list = ["not/match", "/dev/xy", "tts/**", "**/.DS_Store"]
test_list = [
PurePath("test.txt"),
PurePath("data/xy.blob"),
PurePath("bla/blu/ble"),
PurePath("data/../xy.blob"),
(PurePath("test.txt"), False),
(PurePath("data/xy.blob"), False),
(PurePath("bla/blu/ble"), False),
(PurePath("data/../xy.blob"), False),
(PurePath("tts"), False),
(PurePath("tts/blah"), True),
(PurePath("tts/blah/bleh"), True),
(PurePath("data/tts"), False),
(PurePath(".DS_Store"), True),
(PurePath("data/.DS_Store"), True),
]

for path_object in test_list:
assert _is_excluded_by_filter(path_object, filter_list) is False


def test_is_exclude_by_filter_bad() -> None:
"""Test exclude filter."""
filter_list = ["*.txt", "data/*", "bla/blu/ble"]
test_list = [
PurePath("test.txt"),
PurePath("data/xy.blob"),
PurePath("bla/blu/ble"),
PurePath("data/test_files/kk.txt"),
]

for path_object in test_list:
assert _is_excluded_by_filter(path_object, filter_list) is True
for path_object, expect_filtered in test_list:
assert _is_excluded_by_filter(path_object, filter_list) == expect_filtered


@pytest.mark.parametrize("bufsize", [10240, 4 * 2**20])
Expand Down
Loading