Restore settings from a backup tar.gz. This will overwrite your current files for those settings.
(backup_path: Path)
| 429 | except FileNotFoundError: |
| 430 | return "" |
| 431 | except UnicodeDecodeError: |
| 432 | return path.read_text(encoding="utf-8", errors="replace") |
| 433 | |
| 434 | |
| 435 | def atomic_write_text(path: Path, content: str, mode: int = 0o644) -> None: |
| 436 | path.parent.mkdir(parents=True, exist_ok=True) |
| 437 | fd, temp_name = tempfile.mkstemp(prefix=path.name + ".", dir=str(path.parent)) |
| 438 | temp_path = Path(temp_name) |
| 439 | try: |
| 440 | with os.fdopen(fd, "w", encoding="utf-8") as f: |
| 441 | f.write(content) |
| 442 | f.flush() |
| 443 | os.fsync(f.fileno()) |
| 444 | os.chmod(temp_path, mode) |
| 445 | temp_path.replace(path) |
| 446 | finally: |
| 447 | temp_path.unlink(missing_ok=True) |
| 448 | |
| 449 | |
| 450 | def sha256_file(path: Path) -> str: |
| 451 | h = hashlib.sha256() |
| 452 | with path.open("rb") as f: |
| 453 | for chunk in iter(lambda: f.read(1024 * 1024), b""): |
| 454 | h.update(chunk) |
| 455 | return h.hexdigest() |
| 456 | |
| 457 | |
| 458 | def now_stamp() -> str: |
| 459 | # Include sub-second precision so multiple backups in one second cannot collide. |
| 460 | return time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}" |
no test coverage detected