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

fix: check whether xattr is changed before and after clear #57

Merged
Merged
Changes from 2 commits
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
20 changes: 18 additions & 2 deletions src/exif_stripper/__init__.py
Original file line number Diff line number Diff line change
@@ -37,12 +37,28 @@
else:
# remove extended attributes (Unix only)
if platform.system() != 'Windows':
import warnings

from xattr import xattr

xattr_obj = xattr(filename)
if xattr_obj.list():
xattr_list = xattr_obj.list()

if xattr_list:
original_xattr_list = xattr_list[:]

xattr_obj.clear()
has_changed = True

xattr_obj = xattr(filename)
xattr_list = xattr_obj.list()

has_changed |= set(xattr_list) != set(original_xattr_list)
if xattr_list:
xattr_list_str = ', '.join(xattr_list)
warnings.warn(

Check warning on line 58 in src/exif_stripper/__init__.py

Codecov / codecov/patch

src/exif_stripper/__init__.py#L57-L58

Added lines #L57 - L58 were not covered by tests
Lee-W marked this conversation as resolved.
Show resolved Hide resolved
f'Extended attributes {xattr_list_str} in {filename} cannot be removed.',
stacklevel=2,
)

if has_changed:
print(f'Stripped metadata from {filename}')
23 changes: 21 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@

RUNNING_ON = platform.system()
RUNNING_ON_WINDOWS = RUNNING_ON == 'Windows'
RUNNING_ON_MACOS = RUNNING_ON == 'Darwin'
Lee-W marked this conversation as resolved.
Show resolved Hide resolved

if not RUNNING_ON_WINDOWS:
from xattr import xattr
@@ -51,7 +52,16 @@
has_exif = dict(im.getexif()) != {}
if on_windows:
return has_exif
return has_exif or xattr(filepath).list()

xattr_list = xattr(filepath).list()
if RUNNING_ON_MACOS:
has_removable_xattr = any(
not attr.startswith('com.apple.') for attr in xattr_list
)
else:
has_removable_xattr = bool(xattr_list)

return has_exif or has_removable_xattr


def assert_metadata_stripped(filepath, on_windows=RUNNING_ON_WINDOWS):
@@ -68,15 +78,24 @@


@pytest.mark.skipif(RUNNING_ON_WINDOWS, reason='xattr does not work on Windows')
def test_process_image_full(image_with_metadata, monkeypatch):
def test_process_image_full(image_with_metadata, monkeypatch, recwarn):
"""Test that cli.process_image() removes EXIF and extended attributes."""

assert_metadata_stripped(image_with_metadata)

# Unremovable attributes may not be present in all system setups.
# This is to assert the warning message if the user has such system configurations.
if recwarn:
message = str(recwarn[0].message)
assert message.startswith('Extended attributes')
assert message.endswith('cannot be removed.')

Check warning on line 91 in tests/test_cli.py

Codecov / codecov/patch

tests/test_cli.py#L89-L91

Added lines #L89 - L91 were not covered by tests
stefmolin marked this conversation as resolved.
Show resolved Hide resolved


def test_process_image_exif_only(image_with_exif_data, monkeypatch):
"""Test that cli.process_image() removes EXIF only (Windows version)."""
if not RUNNING_ON_WINDOWS:
monkeypatch.setattr(platform, 'system', lambda: 'Windows')

assert_metadata_stripped(image_with_exif_data, on_windows=True)