Skip to content

Commit

Permalink
Allow for xattr that cannot be removed on certain setups (#57)
Browse files Browse the repository at this point in the history
Some extend attributes cannot be removed in certain setups on macOS, try to remove, and warn if they remain.

---------
Co-authored-by: Stefanie Molin <[email protected]>
  • Loading branch information
Lee-W authored Nov 13, 2024
1 parent 8c88889 commit 908af8b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/exif_stripper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ def process_image(filename: str | os.PathLike) -> bool:
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(
f'Extended attributes {xattr_list_str} in {filename} cannot be removed.',
stacklevel=2,
)

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

RUNNING_ON = platform.system()
RUNNING_ON_WINDOWS = RUNNING_ON == 'Windows'
RUNNING_ON_MACOS = RUNNING_ON == 'Darwin'

if not RUNNING_ON_WINDOWS:
from xattr import xattr
Expand Down Expand Up @@ -51,7 +52,16 @@ def has_metadata(filepath, on_windows):
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):
Expand All @@ -68,15 +78,24 @@ def assert_metadata_stripped(filepath, on_windows=RUNNING_ON_WINDOWS):


@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) # pragma: no cover
assert message.startswith('Extended attributes') # pragma: no cover
assert message.endswith('cannot be removed.') # pragma: no cover


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)


Expand Down

0 comments on commit 908af8b

Please sign in to comment.