- imptune/db/database.py: deferred SqliteDatabase with WAL + foreign_keys pragmas and init_db() - imptune/db/models.py: full schema (Client, Driver, Printer, Icon) for phases 1-5 - imptune/storage/driver_store.py: SHA256 content-addressed DriverStore with dedup - tests/test_db.py: 7 TDD tests covering all db and storage behaviors
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
"""Content-addressed file storage for driver packages."""
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
|
|
class DriverStore:
|
|
"""SHA256 content-addressed file storage for printer driver packages.
|
|
|
|
Files are stored as DRIVERS_DIR/{sha256} so identical uploads are
|
|
deduplicated automatically.
|
|
"""
|
|
|
|
def __init__(self, base_dir: str) -> None:
|
|
self._base = Path(base_dir)
|
|
self._base.mkdir(parents=True, exist_ok=True)
|
|
|
|
def save(self, data: bytes) -> str:
|
|
"""Persist *data* and return its SHA256 hex digest.
|
|
|
|
If the file already exists the write is skipped (deduplication).
|
|
"""
|
|
digest = hashlib.sha256(data).hexdigest()
|
|
dest = self._base / digest
|
|
if not dest.exists():
|
|
dest.write_bytes(data)
|
|
return digest
|
|
|
|
def get_path(self, sha256: str) -> Path:
|
|
"""Return the filesystem path for a given SHA256 digest."""
|
|
return self._base / sha256
|
|
|
|
def exists(self, sha256: str) -> bool:
|
|
"""Return True if the file for *sha256* exists on disk."""
|
|
return (self._base / sha256).exists()
|