from __future__ import annotations import shutil from dataclasses import dataclass from datetime import UTC, datetime from pathlib import Path from typing import Iterable, List @dataclass class CleanupItem: path: Path action: str reason: str def plan_results_cleanup( results_dir: Path, *, keep_dirs: Iterable[str] = ( "large_benchmark", "discovery_benchmark", "ppi_benchmark", "ppi_sanity_check", "experimental_benchmark_single_library", "smoke_test", ), ) -> List[CleanupItem]: keep = {str(x).strip() for x in keep_dirs} items: List[CleanupItem] = [] if not results_dir.exists(): return items for child in sorted(results_dir.iterdir()): if not child.is_dir(): continue if child.name in keep or child.name == "archive": continue items.append( CleanupItem( path=child, action="archive", reason="obsolete_or_legacy_result_artifact", ) ) return items def execute_results_cleanup(results_dir: Path, plan: List[CleanupItem]) -> Path: archive_root = results_dir / "archive" / datetime.now(UTC).strftime("%Y%m%dT%H%M%SZ") archive_root.mkdir(parents=True, exist_ok=True) for item in plan: if item.action != "archive" or not item.path.exists(): continue target = archive_root / item.path.name if target.exists(): shutil.rmtree(target) shutil.move(str(item.path), str(target)) return archive_root