← Files EW_knowledge_forgeARCHIVED FILE
skills/obsidian-knowledge-forge/scripts/validate_source_files.py
4.08 KB · Nov 21, 2026 · 06:10 UTC
"""Validate the original files and full-text note links declared for a Vault volume."""
from __future__ import annotations
import hashlib
import json
import re
from pathlib import Path, PurePosixPath
ORIGINALS = "90-Originals"
DOCUMENT_SUFFIXES = {
".csv", ".doc", ".docx", ".gif", ".heic", ".html", ".jpeg", ".jpg", ".json",
".md", ".odg", ".odp", ".ods", ".odt", ".pdf", ".png", ".ppt", ".pptx",
".rtf", ".tif", ".tiff", ".tsv", ".txt", ".webp", ".xls", ".xlsx", ".xml",
".yaml", ".yml",
}
def relative_path(value: object, field: str) -> PurePosixPath:
if not isinstance(value, str):
raise ValueError(f"{field} must be a Vault-relative path")
path = PurePosixPath(value)
if (
not path.parts or path.is_absolute() or path.as_posix() != value
or ".." in path.parts or re.search(r'[\\:\[\]|#<>?"*\x00-\x1f]', value)
or any(part.startswith(".") or part.endswith((" ", ".")) for part in path.parts)
):
raise ValueError(f"Invalid Vault-relative path for {field}: {value!r}")
return path
def validate_source_files(root: Path, manifest: Path) -> None:
entries = json.loads(manifest.read_text(encoding="utf-8"))
if not isinstance(entries, list):
raise ValueError("Source file manifest must be a JSON array")
source_ids: set[str] = set()
original_paths: set[PurePosixPath] = set()
folded_paths: set[str] = set()
note_paths: set[PurePosixPath] = set()
for entry in entries:
if not isinstance(entry, dict):
raise ValueError("Each source file entry must be an object")
source_id = entry.get("source_id")
if (
not isinstance(source_id, str) or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_-]*", source_id)
or source_id.casefold() in source_ids
):
raise ValueError(f"Invalid or duplicate source ID: {source_id!r}")
source_ids.add(source_id.casefold())
original = relative_path(entry.get("original"), "original")
if original.parts[:-1] != (ORIGINALS, source_id) or original.suffix.lower() not in DOCUMENT_SUFFIXES:
raise ValueError(f"Original must be a supported document in {ORIGINALS}/{source_id}/: {original}")
folded = original.as_posix().casefold()
if folded in folded_paths:
raise ValueError(f"Duplicate portable original path: {original}")
folded_paths.add(folded)
original_paths.add(original)
original_file = root / original
if not original_file.is_file():
raise ValueError(f"Missing original file: {original}")
with original_file.open("rb") as stream:
digest = sha256_stream(stream)
if digest != entry.get("sha256"):
raise ValueError(f"Original SHA-256 mismatch: {original}")
notes = entry.get("full_text_notes")
if not isinstance(notes, list) or not notes:
raise ValueError(f"Missing full-text notes for {source_id}")
link = re.compile(r"\[\[" + re.escape(original.as_posix()) + r"(?:\|[^\]\n]+)?\]\]")
for value in notes:
note = relative_path(value, "full_text_notes")
if note.suffix != ".md" or note.parts[:2] != ("90-Sources", source_id) or note in note_paths:
raise ValueError(f"Invalid or duplicate full-text Markdown path: {note}")
note_paths.add(note)
if not (root / note).is_file():
raise ValueError(f"Missing full-text Markdown: {note}")
if not link.search((root / note).read_text(encoding="utf-8")):
raise ValueError(f"Full-text Markdown must link to its original file: {note}")
actual_paths = {
PurePosixPath(path.relative_to(root).as_posix())
for path in (root / ORIGINALS).rglob("*") if path.is_file()
}
if actual_paths != original_paths:
raise ValueError(f"Unregistered originals: {', '.join(map(str, sorted(actual_paths - original_paths)))}")
def sha256_stream(stream) -> str:
digest = hashlib.sha256()
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
SHA-256: abd0e2252d4f0f59160840cb7e6e1e6e4f051fc179bf92cbfd8a847cb242202f