aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Misc/mypy/make_symlinks.py
blob: 9150604ed7939abc1521ecd3ff4b7296032675c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import os
from pathlib import Path

CURRENT_DIR = Path(__file__).parent
MISC_DIR = CURRENT_DIR.parent
REPO_ROOT = MISC_DIR.parent
LIB_DIR = REPO_ROOT / "Lib"
FILE_LIST = CURRENT_DIR / "typed-stdlib.txt"

parser = argparse.ArgumentParser(prog="make_symlinks.py")
parser.add_argument(
    "--symlink",
    action="store_true",
    help="Create symlinks",
)
parser.add_argument(
    "--clean",
    action="store_true",
    help="Delete any pre-existing symlinks",
)

args = parser.parse_args()

if args.clean:
    for entry in CURRENT_DIR.glob("*"):
        if entry.is_symlink():
            entry_at_root = entry.relative_to(REPO_ROOT)
            print(f"removing pre-existing {entry_at_root}")
            entry.unlink()

for link in FILE_LIST.read_text().splitlines():
    link = link.strip()
    if not link or link.startswith('#'):
        continue

    src = LIB_DIR / link
    dst = CURRENT_DIR / link
    src_at_root = src.relative_to(REPO_ROOT)
    dst_at_root = dst.relative_to(REPO_ROOT)
    if (
        dst.is_symlink()
        and src.resolve(strict=True) == dst.resolve(strict=True)
    ):
        continue

    if not args.symlink and args.clean:
        # when the user called --clean without --symlink, don't report missing
        # symlinks that we just deleted ourselves
        continue

    # we specifically want to create relative-path links with ..
    src_rel = os.path.relpath(src, CURRENT_DIR)
    action = "symlinking" if args.symlink else "missing symlink to"
    print(f"{action} {src_at_root} at {dst_at_root}")
    if args.symlink:
        os.symlink(src_rel, dst)