from __future__ import annotations import os import shutil from pathlib import Path def ensure_dir(path: Path) -> None: path.mkdir(parents=True, exist_ok=True) def write_if_changed(path: Path, text: str, *, encoding: str = "utf-8") -> bool: existing = None try: existing = path.read_text(encoding=encoding) except Exception: existing = None if existing == text: return False ensure_dir(path.parent) path.write_text(text, encoding=encoding) return True def copy_if_changed(src: Path, dst: Path) -> bool: try: if dst.exists() and src.read_bytes() == dst.read_bytes(): return False except Exception: pass ensure_dir(dst.parent) shutil.copy2(src, dst) return True def chmod_if_needed(path: Path, mode: int) -> bool: try: current = path.stat().st_mode & 0o777 except Exception: return False if current == mode: return False os.chmod(path, mode) return True