|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +CURRENT_DIR = Path(__file__).parent |
| 9 | +MISC_DIR = CURRENT_DIR.parent |
| 10 | +REPO_ROOT = MISC_DIR.parent |
| 11 | +LIB_DIR = REPO_ROOT / "Lib" |
| 12 | +FILE_LIST = CURRENT_DIR / "typed-stdlib.txt" |
| 13 | + |
| 14 | +parser = argparse.ArgumentParser(prog="make_symlinks.py") |
| 15 | +parser.add_argument( |
| 16 | + "--symlink", |
| 17 | + action="store_true", |
| 18 | + help="Create symlinks", |
| 19 | +) |
| 20 | +parser.add_argument( |
| 21 | + "--clean", |
| 22 | + action="store_true", |
| 23 | + help="Delete any pre-existing symlinks", |
| 24 | +) |
| 25 | + |
| 26 | +args = parser.parse_args() |
| 27 | + |
| 28 | +if args.clean: |
| 29 | + for entry in CURRENT_DIR.glob("*"): |
| 30 | + if entry.is_symlink(): |
| 31 | + entry_at_root = entry.relative_to(REPO_ROOT) |
| 32 | + print(f"removing pre-existing {entry_at_root}") |
| 33 | + entry.unlink() |
| 34 | + |
| 35 | +for link in FILE_LIST.read_text().splitlines(): |
| 36 | + link = link.strip() |
| 37 | + if not link or link.startswith('#'): |
| 38 | + continue |
| 39 | + |
| 40 | + src = LIB_DIR / link |
| 41 | + dst = CURRENT_DIR / link |
| 42 | + src_at_root = src.relative_to(REPO_ROOT) |
| 43 | + dst_at_root = dst.relative_to(REPO_ROOT) |
| 44 | + if ( |
| 45 | + dst.is_symlink() |
| 46 | + and src.resolve(strict=True) == dst.resolve(strict=True) |
| 47 | + ): |
| 48 | + continue |
| 49 | + |
| 50 | + if not args.symlink and args.clean: |
| 51 | + # when the user called --clean without --symlink, don't report missing |
| 52 | + # symlinks that we just deleted ourselves |
| 53 | + continue |
| 54 | + |
| 55 | + # we specifically want to create relative-path links with .. |
| 56 | + src_rel = os.path.relpath(src, CURRENT_DIR) |
| 57 | + action = "symlinking" if args.symlink else "missing symlink to" |
| 58 | + print(f"{action} {src_at_root} at {dst_at_root}") |
| 59 | + if args.symlink: |
| 60 | + os.symlink(src_rel, dst) |
0 commit comments