Skip to content

Commit

Permalink
Make check_media_integrity import optional
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkollasch committed Aug 20, 2022
1 parent f3976b8 commit 2681d54
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ If the photos are stored on an SSD or RAID array, use
``--storage-type SSD`` or ``--storage-type RAID`` and
checksum and EXIF checks will be performed by multiple workers.

To check the integrity of media files before indexing them,
use the ``--check-integrity`` flag.
Integrity checking has optional dependencies; install them with
``pip install .[check-mi]``

Collect files into a storage folder
-----------------------------------

Expand Down
40 changes: 24 additions & 16 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ install_requires =
zstandard>=0.15.2
xxhash>=2.0.2
blake3~=0.3.0
ffmpeg-python
Pillow-SIMD ; platform_machine=="i386"
Pillow-SIMD ; platform_machine=="x86"
Pillow-SIMD ; platform_machine=="x64"
Pillow-SIMD ; platform_machine=="x86_64"
Pillow ; platform_machine=="arm"
Pillow ; platform_machine=="armv7l"
Pillow ; platform_machine=="aarch64"
Pillow ; platform_machine=="arm64"
PyPDF2
Wand
filetype
pyheif
psutil
python_requires = >=3.8
package_dir =
= src
Expand All @@ -70,6 +56,24 @@ test =
pytest
pytest-datafiles
coverage
check-mi =
ffmpeg-python
Pillow-SIMD ; platform_machine=="i386"
Pillow-SIMD ; platform_machine=="x86"
Pillow-SIMD ; platform_machine=="x64"
Pillow-SIMD ; platform_machine=="x86_64"
Pillow ; platform_machine=="arm"
Pillow ; platform_machine=="arm32"
Pillow ; platform_machine=="armv7l"
Pillow ; platform_machine=="aarch64"
Pillow ; platform_machine=="armv8b"
Pillow ; platform_machine=="armv8l"
Pillow ; platform_machine=="arm64"
PyPDF2
Wand
filetype
pyheif
psutil

# content of: tox.ini , put in same dir as setup.py
[tox:tox]
Expand All @@ -92,6 +96,8 @@ deps =
conda_deps =
imagemagick
ffmpeg
extras =
check-mi
;install_command = pip install --no-compile {opts} {packages}
commands =
coverage run -m pytest
Expand All @@ -105,7 +111,7 @@ deps =
commands =
black --check --diff .
isort --check --diff .
flake8 --count src tests benchmarks --extend-exclude 'src/photomanager/check_media_integrity'
flake8 --count src tests benchmarks

[testenv:twine]
deps =
Expand Down Expand Up @@ -135,7 +141,9 @@ per-file-ignores = __init__.py:F401
extend-ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,W503
exclude = .*/, build, __pycache__, *.egg, src/photomanager/_version.py
extend-exclude =
src/photomanager/_version.py
src/photomanager/check_media_integrity

[coverage:run]
branch = True
Expand Down
10 changes: 9 additions & 1 deletion src/photomanager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@

from photomanager import version
from photomanager.actions import actions, fileops
from photomanager.check_media_integrity.check_mi import check_files
from photomanager.database import Database, sizeof_fmt
from photomanager.hasher import DEFAULT_HASH_ALGO, HASH_ALGORITHMS, HashAlgorithm

try:
from photomanager.check_media_integrity.check_mi import check_files
except ImportError as e:
check_files_message = str(e)

def check_files(*_, **__):
raise Exception("check-media-integrity not available: " + check_files_message)


DEFAULT_DB = "photos.json"


Expand Down
68 changes: 68 additions & 0 deletions tests/unit_tests/test_cli_no_cmi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import importlib
import logging
import sys
from pathlib import Path
from typing import cast

import pytest
from click import Group
from click.testing import CliRunner

FIXTURE_DIR = Path(__file__).resolve().parent.parent / "test_files"


def check_dir_empty(dir_path):
cwd_files = list(Path(dir_path).glob("*"))
print(cwd_files)
assert len(cwd_files) == 0


@pytest.fixture(params=(False, True, False))
def hide_cmi(request, monkeypatch):
if request.param:
monkeypatch.setitem(
sys.modules, "photomanager.check_media_integrity.check_mi", None
)
return request.param


@pytest.mark.datafiles(
FIXTURE_DIR / "A" / "img1.png",
FIXTURE_DIR / "A" / "img1.jpg",
)
def test_cli_check_integrity_not_available(hide_cmi, datafiles, caplog):
"""
If importing check_media_integrity gives an ImportError
and the --check-integrity flag is provided, raise an Exception.
However, no Exception is raised if --check-integrity is absent.
"""
caplog.set_level(logging.DEBUG)
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=datafiles) as fs:
from photomanager import cli

importlib.reload(cli)

result = runner.invoke(
cast(Group, cli.main),
["index", "--dump", str(datafiles)],
)
print(result.output)
print(result)
print(result.exception)
assert not result.exception
caplog.clear()

result = runner.invoke(
cast(Group, cli.main),
["index", "--dump", "--check-integrity", str(datafiles)],
)
print(result.output)
print(result)
print(result.exception)
if hide_cmi:
assert result.exception
assert "check-media-integrity not available:" in str(result.exception)
else:
assert not result.exception
check_dir_empty(fs)

0 comments on commit 2681d54

Please sign in to comment.