(
root: Path, moved_pairs: list[tuple[Path, Path]], *, dry_run: bool
)
| 453 | |
| 454 | |
| 455 | def _write_rollback_scripts( |
| 456 | root: Path, moved_pairs: list[tuple[Path, Path]], *, dry_run: bool |
| 457 | ) -> None: |
| 458 | stamp = _now_stamp() |
| 459 | sh_path = root / "logs" / f"undo_organization_{stamp}.sh" |
| 460 | ps_path = root / "logs" / f"undo_organization_{stamp}.ps1" |
| 461 | |
| 462 | # 反向执行:从 dst 移回 src。按逆序更安全。 |
| 463 | lines_sh = ["#!/usr/bin/env bash", "set -euo pipefail", ""] |
| 464 | lines_ps = ["Set-StrictMode -Version Latest", "$ErrorActionPreference = 'Stop'", ""] |
| 465 | for src, dst in reversed(moved_pairs): |
| 466 | lines_sh.append(f"mkdir -p {shlex_quote(str(src.parent))}") |
| 467 | lines_sh.append(f"mv {shlex_quote(str(dst))} {shlex_quote(str(src))}") |
| 468 | lines_ps.append( |
| 469 | f"New-Item -ItemType Directory -Force -Path {ps_quote(str(src.parent))} | Out-Null" |
| 470 | ) |
| 471 | lines_ps.append( |
| 472 | f"Move-Item -Force -Path {ps_quote(str(dst))} -Destination {ps_quote(str(src))}" |
| 473 | ) |
| 474 | |
| 475 | if dry_run: |
| 476 | return |
| 477 | |
| 478 | sh_path.parent.mkdir(parents=True, exist_ok=True) |
| 479 | sh_path.write_text("\n".join(lines_sh) + "\n", encoding="utf-8") |
| 480 | ps_path.write_text("\n".join(lines_ps) + "\n", encoding="utf-8") |
| 481 | |
| 482 | |
| 483 | def shlex_quote(s: str) -> str: |
no test coverage detected