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
12 changes: 12 additions & 0 deletions dlccore/project/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@
"""Public entry points of ``dlccore.project``; consumer helpers are in ``api.helpers``. Re-exports only."""

from dlccore.project.api import helpers
from dlccore.project.content import (
FailedRead,
FileFingerprint,
ResourcesFileFingerprint,
compare_file_fingerprints,
resources_file_fingerprint,
)
from dlccore.project.inspection import inspect_project, project_name
from dlccore.project.schemas.base.resources import FileResourceKind
from dlccore.project.schemas.scope import findings_in_scope

__all__ = [
"FailedRead",
"FileFingerprint",
"FileResourceKind",
"ResourcesFileFingerprint",
"compare_file_fingerprints",
"findings_in_scope",
"helpers",
"inspect_project",
"project_name",
"resources_file_fingerprint",
]
28 changes: 28 additions & 0 deletions dlccore/project/content/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# 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
#

"""The project layer's requests over the per-file I/O; the only package that imports ``dlccore.io``."""

from dlccore.project.content.fingerprints import (
FileFingerprint,
ResourcesFileFingerprint,
compare_file_fingerprints,
resources_file_fingerprint,
)
from dlccore.project.content.reads import FailedRead

__all__ = [
"FailedRead",
"FileFingerprint",
"ResourcesFileFingerprint",
"compare_file_fingerprints",
"resources_file_fingerprint",
]
206 changes: 206 additions & 0 deletions dlccore/project/content/fingerprints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#
# 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 of a snapshot's file resources, requested from ``dlccore.io`` and reported as findings."""

from __future__ import annotations

from collections import Counter
from collections.abc import Iterable
from pathlib import Path
from typing import Annotated

from pydantic import Field

from dlccore.io import UnreadableFileError, fingerprint_file
from dlccore.project.content.reads import FailedRead, covered, present_files_in_scope
from dlccore.project.schemas.base.core import FrozenModel, ProjectRelativePath, SchemaId, Sha256Hex
from dlccore.project.schemas.base.filesystem import FileSchema, LayoutIndex
from dlccore.project.schemas.base.finding_codes import FingerprintFindingCode
from dlccore.project.schemas.base.findings import Finding
from dlccore.project.schemas.base.observations import NodeStatus, walk_observed_nodes
from dlccore.project.schemas.locations import node_location
from dlccore.project.schemas.project_layout.project import PROJECT_INDEX
from dlccore.project.schemas.scope import Scope
from dlccore.project.schemas.snapshots import ProjectSnapshot
from dlccore.project.validation.results import ValidationResult


class FileFingerprint(FrozenModel):
"""The file fingerprint of one file resource: the SHA-256 of its full bytes.

Attributes:
schema_id: The layout node fingerprinted; ``node_location`` gives its finding anchor.
project_relative_path: The path as the snapshot observed it.
sha256: Hex SHA-256 of the bytes read.
size_bytes: Bytes read.
"""

schema_id: SchemaId
project_relative_path: ProjectRelativePath
sha256: Sha256Hex
size_bytes: Annotated[int, Field(strict=True, ge=0)]

@property
def posix_path(self) -> str:
"""The project-relative path with ``/`` separators, the same on every platform."""

return self.project_relative_path.as_posix()


class ResourcesFileFingerprint(FrozenModel):
"""The requested file fingerprints, and the snapshot with one finding per resource that could not be read.

Attributes:
snapshot: The input snapshot with ``FILE_FINGERPRINT_FAILED`` findings merged in.
fingerprints: One file fingerprint per present file node in scope that could be read.
failed: One entry per present file node in scope that could not be read.
"""

snapshot: ProjectSnapshot
fingerprints: tuple[FileFingerprint, ...] = ()
failed: tuple[FailedRead, ...] = ()


def resources_file_fingerprint(
snapshot: ProjectSnapshot,
scope: Scope,
*,
index: LayoutIndex = PROJECT_INDEX,
) -> ResourcesFileFingerprint:
"""File-fingerprint every present file node ``scope`` covers in ``snapshot``, without rediscovering the project.

Nodes not ``PRESENT`` are skipped: structure validation already reports them. Symlinks are hashed through;
paths are never resolved. Each such file is in ``fingerprints`` or in ``failed``, never both. A file that cannot
be read yields a ``FILE_FINGERPRINT_FAILED`` finding, carried by its ``FailedRead`` and merged into the returned
snapshot. The input snapshot is not modified.

Args:
snapshot: A snapshot from ``inspect_project``.
scope: The nodes whose files to fingerprint, each within its instance prefix; see ``Scope.covers``.
index: The layout the snapshot was observed against.

Raises:
ValueError: If ``scope`` names a node the layout does not declare.
"""

fingerprints: list[FileFingerprint] = []
failed: list[FailedRead] = []

for node in present_files_in_scope(snapshot, scope, scope.file_schema_ids(index=index), index=index):
path = snapshot.project_root / node.project_relative_path

try:
read = fingerprint_file(path)
except UnreadableFileError as error:
finding = Finding(
code=FingerprintFindingCode.FILE_FINGERPRINT_FAILED,
**node_location(node.schema_id, node.project_relative_path, index=index),
path=path,
evidence={
"resource_kind": node.kind.value,
"error_type": type(error.__cause__ or error).__name__,
"error_message": str(error),
},
)
failed.append(FailedRead(node.schema_id, node.project_relative_path, (finding,)))
continue

fingerprints.append(
FileFingerprint(
schema_id=node.schema_id,
project_relative_path=node.project_relative_path,
sha256=read.sha256,
size_bytes=read.size_bytes,
)
)

findings = tuple(finding for failure in failed for finding in failure.findings)
return ResourcesFileFingerprint(
snapshot=snapshot.with_validation_result(ValidationResult(findings=findings)),
fingerprints=tuple(fingerprints),
failed=tuple(failed),
)


def compare_file_fingerprints(
snapshot: ProjectSnapshot,
scope: Scope,
recorded: Iterable[FileFingerprint],
*,
index: LayoutIndex = PROJECT_INDEX,
) -> ResourcesFileFingerprint:
"""File-fingerprint ``scope`` as ``resources_file_fingerprint`` does, and compare recorded file fingerprints.

Matched by schema ID and path. A recorded file whose bytes differ, or that is not fingerprinted because it is
gone, yields a ``FILE_FINGERPRINT_CHANGED`` finding merged into the returned snapshot. A recorded file that
could not be read carries ``FILE_FINGERPRINT_FAILED`` only. One inspection found ``UNREADABLE``, or that sits
in an ``UNREADABLE`` directory, carries nothing here: structure validation reports it. A file fingerprinted but
not recorded is not reported.

A change is a ``WARNING``: a consumer that relies on the recorded bytes refuses it by passing the code to
``get_blocking_findings(..., block=...)``.

Raises:
ValueError: If a record's node is not a file the layout declares or its path does not match the layout, if a
file is recorded more than once, if ``scope`` does not cover a recorded file, or if ``scope`` names a node
the layout does not declare.
"""

recorded = tuple(recorded)
malformed = [r.posix_path for r in recorded if not _matches_layout(r, index)]
if malformed:
raise ValueError(f"{malformed} are recorded under a node that is not a declared file, or not matching it.")

counts = Counter((r.schema_id, r.project_relative_path) for r in recorded)
repeated = sorted({r.posix_path for r in recorded if counts[r.schema_id, r.project_relative_path] > 1})
if repeated:
raise ValueError(f"{repeated} are recorded more than once.")

outside = [r.posix_path for r in recorded if not covered(scope, r.schema_id, r.project_relative_path, index)]
if outside:
raise ValueError(f"{outside} are recorded but outside the scope compared.")

fingerprinted = resources_file_fingerprint(snapshot, scope, index=index)
observed = {(f.schema_id, f.project_relative_path): f.sha256 for f in fingerprinted.fingerprints}
failed = {(failure.schema_id, failure.project_relative_path) for failure in fingerprinted.failed}
unreadable = [
node.project_relative_path
for node in walk_observed_nodes(snapshot.project)
if node.status is NodeStatus.UNREADABLE
]
findings = tuple(
Finding(
code=FingerprintFindingCode.FILE_FINGERPRINT_CHANGED,
**node_location(record.schema_id, record.project_relative_path, index=index),
path=snapshot.project_root / record.project_relative_path,
evidence={"recorded_sha256": record.sha256, "observed_sha256": observed.get(key)},
)
for record in recorded
if (key := (record.schema_id, record.project_relative_path)) not in failed
and observed.get(key) != record.sha256
and not any(record.project_relative_path.is_relative_to(path) for path in unreadable)
)
return fingerprinted.model_copy(
update={"snapshot": fingerprinted.snapshot.with_validation_result(ValidationResult(findings=findings))}
)


def _matches_layout(record: FileFingerprint, index: LayoutIndex) -> bool:
"""Return whether the record's node is a declared file and each part of its path matches the node at that depth."""

if record.schema_id not in index.schema_ids_of(FileSchema):
return False
nodes = [index.schemas[schema_id] for schema_id in index.ancestry(record.schema_id)[:-1]]
parts = record.project_relative_path.parts[::-1]
return len(parts) == len(nodes) and all(
node.name.matches(Path(part)) for node, part in zip(nodes, parts, strict=True)
)
69 changes: 69 additions & 0 deletions dlccore/project/content/reads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# 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
#

"""What every data entry point shares: which observed files it reads, and how it reports one it could not."""

from __future__ import annotations

from collections.abc import Collection
from dataclasses import dataclass

from dlccore.project.schemas.base.core import ProjectRelativePath, SchemaId
from dlccore.project.schemas.base.filesystem import LayoutIndex
from dlccore.project.schemas.base.findings import Finding
from dlccore.project.schemas.base.observations import NodeStatus, ObservedNode, walk_observed_nodes
from dlccore.project.schemas.locations import node_location
from dlccore.project.schemas.scope import Scope
from dlccore.project.schemas.snapshots import ProjectSnapshot


@dataclass(frozen=True)
class FailedRead:
"""One requested file that could not be read, with the findings saying why.

Attributes:
schema_id: The layout node of the file.
project_relative_path: The path as the snapshot observed it.
findings: Why it could not be read; also merged into the snapshot the entry point returns.
"""

schema_id: SchemaId
project_relative_path: ProjectRelativePath
findings: tuple[Finding, ...]


def present_files_in_scope(
snapshot: ProjectSnapshot,
scope: Scope,
schema_ids: Collection[SchemaId],
*,
index: LayoutIndex,
) -> tuple[ObservedNode, ...]:
"""Return every ``PRESENT`` node of ``schema_ids`` that ``scope`` covers, in walk order, without rediscovering.

Nodes not ``PRESENT`` are left out: structure validation already reports them.
"""

return tuple(
node
for node in walk_observed_nodes(snapshot.project)
if node.schema_id in schema_ids
and node.status is NodeStatus.PRESENT
and covered(scope, node.schema_id, node.project_relative_path, index)
)


def covered(scope: Scope, schema_id: SchemaId, project_relative_path: ProjectRelativePath, index: LayoutIndex) -> bool:
"""Return whether ``scope`` covers the node with this schema ID at this path."""

instance = node_location(schema_id, project_relative_path, index=index)["instance"]

return scope.covers(schema_id, instance, index=index)
17 changes: 17 additions & 0 deletions dlccore/project/schemas/base/finding_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ class ConfiguredVideoFindingCode(FindingCode):
)


class FingerprintFindingCode(FindingCode):
"""Findings about file and frame fingerprints of requested project resources."""

FILE_FINGERPRINT_FAILED = (
"FILE_FINGERPRINT_FAILED",
"A requested resource could not be file-fingerprinted.",
Severity.ERROR,
("resource_kind", "error_type", "error_message"),
)
FILE_FINGERPRINT_CHANGED = (
"FILE_FINGERPRINT_CHANGED",
"A resource's file fingerprint differs from the one recorded for it, or it was not fingerprinted.",
Severity.WARNING,
("recorded_sha256", "observed_sha256"),
)


def iter_finding_codes() -> tuple[FindingCode, ...]:
"""Return every declared finding code, namespace by namespace."""

Expand Down
5 changes: 5 additions & 0 deletions tests/io/test_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def test_the_human_labels_prefix_is_spelled_once_in_io() -> None:
assert _modules_spelling("CollectedData_") == {"io/suffixes.py"}


def test_the_scan_sees_content_import_io() -> None:
# Control: content does import io, so the scan must report it, or "only content imports io" proves nothing.
assert any(_within(name, IO) for name in dict(_package_modules())[f"{CONTENT}.fingerprints"])


def test_the_scan_reaches_io() -> None:
# Control: io's own __init__ imports its errors module, so an empty scan cannot pass the test above.
assert f"{IO}.errors" in dict(_package_modules())[IO]
Expand Down
Loading
Loading