Standardisation 5

This commit is contained in:
2026-08-20 14:53:06 +02:00
parent bcba5facc8
commit 644e287586
5 changed files with 648 additions and 466 deletions
+12 -1
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import shutil
import uuid
from collections.abc import Iterable
from contextlib import contextmanager
from pathlib import Path
@@ -44,7 +45,11 @@ def staged_directory(destination: str | Path):
@contextmanager
def staged_files(destination: str | Path):
def staged_files(
destination: str | Path,
*,
remove: Iterable[str] = (),
):
"""Stage a set of files and merge them into a directory with rollback."""
target = Path(destination)
target.parent.mkdir(parents=True, exist_ok=True)
@@ -56,9 +61,15 @@ def staged_files(destination: str | Path):
try:
yield staging
staged = sorted(path for path in staging.iterdir() if path.is_file())
staged_names = {path.name for path in staged}
removed_names = set(remove) - staged_names
target.mkdir(parents=True, exist_ok=True)
backup.mkdir()
try:
for name in sorted(removed_names):
destination_path = target / name
if destination_path.is_file() or destination_path.is_symlink():
destination_path.replace(backup / name)
for source in staged:
destination_path = target / source.name
if destination_path.exists() or destination_path.is_symlink():