refactor: let the restore command print its report

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:57 +01:00
1 parent 75ccaf460c
commit 3869a24b6a
3 files changed
+125 -84

No files matched your search

+20 -26
View File
@@ -3,7 +3,7 @@
from __future__ import annotations
import os
import sys
from dataclasses import dataclass, field
from pathlib import Path
from model.largefiles import registered_paths, restore_verified
@@ -36,41 +36,35 @@ def index_cache(cache_dir: str) -> dict[str, str]:
return index
def restore(
cache_dir: str, db_path: str, gitignore: str
) -> tuple[int, list[str]]:
@dataclass
class Restored:
"""What a restore wrote, what it could not supply, and whether a cache was there."""
restored: list[str] = field(default_factory=list)
unsatisfied: list[str] = field(default_factory=list)
cache_found: bool = True
def restore(cache_dir: str, db_path: str, gitignore: str, *, root: Path) -> Restored:
"""Write each gitignored, absent database path under root from the cache."""
ignored = gitignored_paths(gitignore)
result = Restored()
# No cache is not nothing to do: the paths it would have supplied are
# still missing, and the check below is the one that must say so.
if os.path.isdir(cache_dir):
index = index_cache(cache_dir)
else:
print(f"No cache at {cache_dir}")
result.cache_found = False
index = {}
db = load_database(db_path)
restored = 0
unsatisfied: list[str] = []
for sha1, entry in db.get("files", {}).items():
path = entry.get("path", "")
if path not in ignored or os.path.exists(path):
target = root / path
if path not in ignored or target.exists():
continue
source = index.get(sha1)
if not source:
unsatisfied.append(path)
if not source or not restore_verified(source, str(target), sha1):
result.unsatisfied.append(path)
continue
os.makedirs(os.path.dirname(path), exist_ok=True)
if not restore_verified(source, path, sha1):
unsatisfied.append(path)
continue
print(f"Restored: {path}")
restored += 1
print(f"Total: {restored} files restored")
if unsatisfied:
# Every consumer downstream resolves against the disk, so a path the
# cache cannot supply is not a smaller restore: it drops entries from
# the manifest and inflates the missing count the README publishes.
print(f"Unsatisfied: {len(unsatisfied)} declared paths the cache "
"cannot supply", file=sys.stderr)
for path in sorted(unsatisfied)[:10]:
print(f" {path}", file=sys.stderr)
return restored, unsatisfied
result.restored.append(path)
return result
+16 -2
View File
@@ -18,6 +18,8 @@ from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
from core.cli import add_repo_inputs
from release.restore import restore
@@ -37,8 +39,20 @@ def main() -> None:
args = parser.parse_args()
if not os.path.isfile(args.gitignore):
parser.error(f"no file at {args.gitignore}: it lists the release assets")
_restored, unsatisfied = restore(args.cache, args.db, args.gitignore)
if unsatisfied:
result = restore(args.cache, args.db, args.gitignore, root=Path.cwd())
if not result.cache_found:
print(f"No cache at {args.cache}")
for path in result.restored:
print(f"Restored: {path}")
print(f"Total: {len(result.restored)} files restored")
if result.unsatisfied:
# Every consumer downstream resolves against the disk, so a path the
# cache cannot supply is not a smaller restore: it drops entries from
# the manifest and inflates the missing count the README publishes.
print(f"Unsatisfied: {len(result.unsatisfied)} declared paths the cache "
"cannot supply", file=sys.stderr)
for path in sorted(result.unsatisfied)[:10]:
print(f" {path}", file=sys.stderr)
raise SystemExit(1)