59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""Create an immutable snapshot of generated per-map marker defaults.
|
|
|
|
Existing backups are never overwritten. If a generated file differs from its
|
|
backup, the script stops and reports it so replacing the snapshot is always an
|
|
explicit manual decision.
|
|
|
|
python tools/backup_markers.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import shutil
|
|
import stat
|
|
from pathlib import Path
|
|
|
|
from common import DATA, ROOT
|
|
|
|
|
|
SOURCE_DIR = DATA / "markers"
|
|
BACKUP_DIR = DATA / "marker-defaults"
|
|
|
|
|
|
def make_read_only(path: Path) -> None:
|
|
path.chmod(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
|
|
|
|
|
def main() -> None:
|
|
sources = sorted(SOURCE_DIR.glob("*.json"))
|
|
if not sources:
|
|
raise SystemExit("no generated marker files found; run extract_markers.py first")
|
|
BACKUP_DIR.mkdir(parents=True, exist_ok=True)
|
|
created = 0
|
|
for source in sources:
|
|
raw = json.loads(source.read_text(encoding="utf-8"))
|
|
if raw.get("mapId") != source.stem or not isinstance(raw.get("markers"), list):
|
|
raise SystemExit(f"invalid per-map marker file: {source.relative_to(ROOT)}")
|
|
destination = BACKUP_DIR / source.name
|
|
if destination.exists():
|
|
if destination.read_bytes() != source.read_bytes():
|
|
raise SystemExit(
|
|
f"backup differs and was not replaced: {destination.relative_to(ROOT)}"
|
|
)
|
|
make_read_only(destination)
|
|
print(f"kept {destination.relative_to(ROOT)}")
|
|
continue
|
|
temporary = destination.with_suffix(".json.tmp")
|
|
shutil.copyfile(source, temporary)
|
|
os.replace(temporary, destination)
|
|
make_read_only(destination)
|
|
created += 1
|
|
print(f"created read-only {destination.relative_to(ROOT)}")
|
|
print(f"backup ready: {len(sources)} map file(s), {created} newly created")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|