在指定位置创建一个指定目录的链接 - Windows:Junction - Unix/macOS:symlink
(src: Path, dst: Path)
| 25 | |
| 26 | |
| 27 | def create_directory_link(src: Path, dst: Path) -> bool: |
| 28 | """ |
| 29 | 在指定位置创建一个指定目录的链接 |
| 30 | - Windows:Junction |
| 31 | - Unix/macOS:symlink |
| 32 | """ |
| 33 | if dst.exists() or dst.is_symlink(): |
| 34 | if dst.is_dir() and not dst.is_symlink(): |
| 35 | try: |
| 36 | dst.rmdir() |
| 37 | except OSError: |
| 38 | shutil.rmtree(dst) |
| 39 | else: |
| 40 | dst.unlink(missing_ok=True) |
| 41 | |
| 42 | dst.parent.mkdir(parents=True, exist_ok=True) |
| 43 | |
| 44 | if platform.system() == "Windows": |
| 45 | result = subprocess.run( |
| 46 | ["cmd", "/c", "mklink", "/J", str(dst), str(src)], |
| 47 | capture_output=True, |
| 48 | text=True, |
| 49 | ) |
| 50 | if result.returncode != 0: |
| 51 | print(Console.err(t("err_create_junction_failed", stderr=result.stderr))) |
| 52 | return False |
| 53 | else: |
| 54 | dst.symlink_to(src) |
| 55 | |
| 56 | return True |
| 57 | |
| 58 | LOCALS_DIR = Path(__file__).parent / "locals" / "setup_workspace" |
| 59 |
no test coverage detected