Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions dlccore/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

from dlccore.io.errors import FileChangedError, UnreadableFileError
from dlccore.io.file_stat import FileStat
from dlccore.io.fingerprints import FileFingerprintResult, fingerprint_file

__all__ = [
"FileChangedError",
"FileFingerprintResult",
"FileStat",
"UnreadableFileError",
"fingerprint_file",
]
92 changes: 92 additions & 0 deletions dlccore/io/fingerprints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

"""File fingerprints: SHA-256 of whole files."""

from __future__ import annotations

import hashlib
import os
import stat
from dataclasses import dataclass
from pathlib import Path

from dlccore.io.errors import UnreadableFileError
from dlccore.io.file_stat import FileStat

_CHUNK_BYTES = 1 << 20


@dataclass(frozen=True)
class FileFingerprintResult:
"""The file fingerprint of one file.

Attributes:
path: The path as given; never resolved.
sha256: Hex SHA-256 of the full bytes.
size_bytes: Bytes hashed.
"""

path: Path
sha256: str
size_bytes: int


def fingerprint_file(path: Path) -> FileFingerprintResult:
"""Hash one file's full bytes, through a symlink.

Args:
path: The file to hash.

Raises:
UnreadableFileError: If the file is missing, is not a regular file, cannot be read, or changed while it
was read.
"""

try:
before = _signature(path)
except OSError as error:
raise UnreadableFileError(f"{path}: cannot be inspected: {error}") from error

if before is None:
raise UnreadableFileError(f"{path}: is not a regular file.")

hasher = hashlib.sha256()
size = 0

try:
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(_CHUNK_BYTES), b""):
hasher.update(chunk)
size += len(chunk)
except OSError as error:
raise UnreadableFileError(f"{path}: cannot be read: {error}") from error

try:
after = _signature(path)
except OSError as error:
raise UnreadableFileError(f"{path}: changed while it was read: {error}") from error

if after != before or size != before.size_bytes:
raise UnreadableFileError(f"{path}: changed while it was read.")

return FileFingerprintResult(path=path, sha256=hasher.hexdigest(), size_bytes=size)


def _signature(path: Path) -> FileStat | None:
"""Return the stat of the regular file at ``path``, through symlinks, or ``None`` if it is not one."""

status = os.stat(path)

if not stat.S_ISREG(status.st_mode):
return None

return FileStat.from_os_stat(status)
42 changes: 42 additions & 0 deletions tests/io/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

from collections.abc import Callable
from pathlib import Path

import pytest


def _create_symlink_or_skip(
link: Path,
target: Path,
*,
target_is_directory: bool = False,
) -> None:
"""Create a symlink or skip when the platform disallows it."""

try:
link.symlink_to(
target,
target_is_directory=target_is_directory,
)
except (
NotImplementedError,
OSError,
) as error:
pytest.skip(f"Symlink creation is unavailable: {error}")


@pytest.fixture
def create_symlink_or_skip() -> Callable[..., None]:
"""Return a helper that creates a symlink or skips the test cleanly."""

return _create_symlink_or_skip
158 changes: 158 additions & 0 deletions tests/io/test_file_fingerprints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

import hashlib
import os
from collections.abc import Callable
from pathlib import Path

import pytest

from dlccore.io import fingerprints
from dlccore.io.errors import UnreadableFileError
from dlccore.io.file_stat import FileStat
from dlccore.io.fingerprints import FileFingerprintResult, fingerprint_file


def test_a_file_fingerprint_is_the_sha256_and_size_of_the_full_bytes(tmp_path: Path) -> None:
content = b"labels" * 500_000 # spans several read chunks
path = tmp_path / "CollectedData_x.h5"
path.write_bytes(content)

result = fingerprint_file(path)

assert result == FileFingerprintResult(
path=path, sha256=hashlib.sha256(content).hexdigest(), size_bytes=len(content)
)


def test_different_bytes_give_different_file_fingerprints(tmp_path: Path) -> None:
first, second = tmp_path / "a", tmp_path / "b"
first.write_bytes(b"labels v1")
second.write_bytes(b"labels v2")

assert fingerprint_file(first).sha256 != fingerprint_file(second).sha256


def test_the_path_is_returned_as_given(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
(tmp_path / "config.yaml").write_text("Task: t\n")
monkeypatch.chdir(tmp_path)

assert fingerprint_file(Path("config.yaml")).path == Path("config.yaml")


def test_a_missing_file_raises_from_the_original(tmp_path: Path) -> None:
with pytest.raises(UnreadableFileError, match=r"gone.h5") as raised:
fingerprint_file(tmp_path / "gone.h5")

assert isinstance(raised.value.__cause__, FileNotFoundError)


def test_a_directory_is_not_a_regular_file(tmp_path: Path) -> None:
with pytest.raises(UnreadableFileError, match="not a regular file"):
fingerprint_file(tmp_path)


def test_a_read_error_raises_from_the_original(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
path = tmp_path / "locked.h5"
path.write_bytes(b"x")

def refuse(*_args: object, **_kwargs: object) -> None:
raise PermissionError("denied")

monkeypatch.setattr(Path, "open", refuse)

with pytest.raises(UnreadableFileError, match="denied") as raised:
fingerprint_file(path)

assert isinstance(raised.value.__cause__, PermissionError)


def test_a_file_that_changes_while_read_has_no_file_fingerprint(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
path = tmp_path / "growing.h5"
path.write_bytes(b"x")
signatures = iter([FileStat(1, 100, 7, 1), FileStat(1, 200, 7, 1)])
monkeypatch.setattr(fingerprints, "_signature", lambda _path: next(signatures))

with pytest.raises(UnreadableFileError, match="changed while it was read"):
fingerprint_file(path)


def test_a_file_whose_size_disagrees_with_the_bytes_read_has_no_file_fingerprint(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
path = tmp_path / "truncated.h5"
path.write_bytes(b"xyz")
monkeypatch.setattr(fingerprints, "_signature", lambda _path: FileStat(99, 100, 7, 1))

with pytest.raises(UnreadableFileError, match="changed while it was read"):
fingerprint_file(path)


def test_a_file_removed_while_read_raises_from_the_original(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
path = tmp_path / "removed.h5"
path.write_bytes(b"x")
signature = fingerprints._signature
calls = 0

def removed_after_the_read(target: Path) -> FileStat | None:
nonlocal calls
calls += 1
if calls == 2:
target.unlink()
return signature(target)

monkeypatch.setattr(fingerprints, "_signature", removed_after_the_read)

with pytest.raises(UnreadableFileError, match="changed while it was read") as raised:
fingerprint_file(path)

assert isinstance(raised.value.__cause__, FileNotFoundError)


def test_a_file_replaced_while_read_has_no_file_fingerprint(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
path = tmp_path / "replaced.h5"
path.write_bytes(b"x")
signatures = iter([FileStat(1, 100, 7, 1), FileStat(1, 100, 8, 1)]) # same size and mtime, another file
monkeypatch.setattr(fingerprints, "_signature", lambda _path: next(signatures))

with pytest.raises(UnreadableFileError, match="changed while it was read"):
fingerprint_file(path)


def test_the_signature_tells_a_replacement_with_the_same_size_and_mtime(tmp_path: Path) -> None:
# Control for the test above: the platform's inode and device really do change on an atomic replacement.
path, replacement = tmp_path / "labels.h5", tmp_path / "labels.h5.tmp"
path.write_bytes(b"one")
replacement.write_bytes(b"two")
mtime_ns = path.stat().st_mtime_ns
os.utime(replacement, ns=(mtime_ns, mtime_ns))
before = fingerprints._signature(path)

os.replace(replacement, path)

after = fingerprints._signature(path)
assert before is not None and after is not None
assert (before.size_bytes, before.mtime_ns) == (after.size_bytes, after.mtime_ns)
assert before != after


def test_a_symlink_is_hashed_through(tmp_path: Path, create_symlink_or_skip: Callable[..., None]) -> None:
target = tmp_path / "target.h5"
target.write_bytes(b"labels")
link = tmp_path / "link.h5"
create_symlink_or_skip(link, target)

result = fingerprint_file(link)

assert (result.path, result.sha256) == (link, fingerprint_file(target).sha256)
Loading