File size: 1,600 Bytes
c289d87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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
|