|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import yaml |
| 9 | + |
| 10 | +BASE_DIR = Path(__file__).resolve().parent |
| 11 | +_PKG_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]+") |
| 12 | + |
| 13 | + |
| 14 | +def resolve_test_path(raw_name: str) -> Path: |
| 15 | + return Path("tests") / f"{raw_name}.py" |
| 16 | + |
| 17 | + |
| 18 | +def load_yaml(filename: str) -> dict[str, Any]: |
| 19 | + with (BASE_DIR / filename).open("r", encoding="utf-8") as fh: |
| 20 | + return yaml.safe_load(fh) or {} |
| 21 | + |
| 22 | + |
| 23 | +def normalize_pkg_spec(spec: str) -> str: |
| 24 | + spec = (spec or "").strip() |
| 25 | + if not spec: |
| 26 | + return spec |
| 27 | + |
| 28 | + if spec.startswith("git+"): |
| 29 | + return spec |
| 30 | + |
| 31 | + if spec.startswith("https://github.com/"): |
| 32 | + spec = spec.rstrip("/") |
| 33 | + if not spec.endswith(".git"): |
| 34 | + spec += ".git" |
| 35 | + return "git+" + spec |
| 36 | + |
| 37 | + return spec |
| 38 | + |
| 39 | + |
| 40 | +def pkg_key(spec: str) -> str: |
| 41 | + spec = normalize_pkg_spec(spec) |
| 42 | + if not spec: |
| 43 | + return spec |
| 44 | + |
| 45 | + if spec.startswith("git+"): |
| 46 | + repo = spec.rsplit("/", 1)[-1] |
| 47 | + if repo.endswith(".git"): |
| 48 | + repo = repo[:-4] |
| 49 | + return repo.split("@", 1)[0].lower().replace("_", "-") |
| 50 | + |
| 51 | + if "://" in spec: |
| 52 | + return spec |
| 53 | + |
| 54 | + spec = spec.split(";", 1)[0].strip() |
| 55 | + if " @" in spec: |
| 56 | + spec = spec.split(" @", 1)[0].strip() |
| 57 | + |
| 58 | + match = _PKG_NAME_RE.match(spec) |
| 59 | + if not match: |
| 60 | + return spec.lower() |
| 61 | + |
| 62 | + return match.group(0).lower().replace("_", "-") |
| 63 | + |
| 64 | + |
| 65 | +def collect_pkgs(test_path: Path, deps: dict[str, Any], *, dedupe_common: bool) -> tuple[list[str], list[str]]: |
| 66 | + specific_pkgs: set[str] = set() |
| 67 | + common_pkgs: set[str] = set(deps.get("common") or []) |
| 68 | + |
| 69 | + specific_pkgs.update(deps.get("tests", {}).get(test_path.name) or []) |
| 70 | + |
| 71 | + test_path_str = test_path.as_posix() |
| 72 | + for key, value in deps.items(): |
| 73 | + if not (isinstance(key, str) and key.startswith("tests/")): |
| 74 | + continue |
| 75 | + if not test_path_str.startswith(key + "/"): |
| 76 | + continue |
| 77 | + |
| 78 | + if isinstance(value, list): |
| 79 | + specific_pkgs.update(value) |
| 80 | + elif isinstance(value, dict): |
| 81 | + specific_pkgs.update(value.get(test_path.name) or []) |
| 82 | + |
| 83 | + if dedupe_common: |
| 84 | + specific_keys = {pkg_key(pkg) for pkg in specific_pkgs} |
| 85 | + common_pkgs = {pkg for pkg in common_pkgs if pkg_key(pkg) not in specific_keys} |
| 86 | + |
| 87 | + return sorted(specific_pkgs), sorted(common_pkgs) |
| 88 | + |
| 89 | + |
| 90 | +def run_uv_pip(action: str, pkgs: list[str], *, extra_args: list[str] | None = None) -> None: |
| 91 | + if not pkgs: |
| 92 | + return |
| 93 | + |
| 94 | + normalized = [normalize_pkg_spec(pkg) for pkg in pkgs] |
| 95 | + print(f"--- {action.title()} deps with uv:") |
| 96 | + for pkg in normalized: |
| 97 | + print(" -", pkg) |
| 98 | + |
| 99 | + for pkg in normalized: |
| 100 | + cmd = ["uv", "pip", action] |
| 101 | + if extra_args: |
| 102 | + cmd.extend(extra_args) |
| 103 | + cmd.append(pkg) |
| 104 | + print("+", " ".join(cmd)) |
| 105 | + try: |
| 106 | + subprocess.check_call(cmd, shell=False) |
| 107 | + except Exception as exc: |
| 108 | + print(f"{action.title()} failed for {pkg}: {exc}") |
| 109 | + |
| 110 | + |
| 111 | +def install_deps(raw_name: str) -> int: |
| 112 | + test_path = resolve_test_path(raw_name.removeprefix("tests/").removesuffix(".py")) |
| 113 | + deps = load_yaml("deps.yaml") |
| 114 | + specific_pkgs, common_pkgs = collect_pkgs(test_path, deps, dedupe_common=True) |
| 115 | + run_uv_pip("install", specific_pkgs, extra_args=["--no-cache"]) |
| 116 | + run_uv_pip("install", common_pkgs, extra_args=["--no-cache"]) |
| 117 | + return 0 |
| 118 | + |
| 119 | + |
| 120 | +def uninstall_deps(raw_name: str) -> int: |
| 121 | + test_path = resolve_test_path(raw_name.removeprefix("tests/").removesuffix(".py")) |
| 122 | + deps = load_yaml("blacklist.yaml") |
| 123 | + specific_pkgs, common_pkgs = collect_pkgs(test_path, deps, dedupe_common=False) |
| 124 | + run_uv_pip("uninstall", specific_pkgs) |
| 125 | + run_uv_pip("uninstall", common_pkgs) |
| 126 | + return 0 |
| 127 | + |
| 128 | + |
| 129 | +def main() -> int: |
| 130 | + parser = argparse.ArgumentParser() |
| 131 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 132 | + |
| 133 | + install_parser = subparsers.add_parser("install") |
| 134 | + install_parser.add_argument("test_name") |
| 135 | + |
| 136 | + uninstall_parser = subparsers.add_parser("uninstall") |
| 137 | + uninstall_parser.add_argument("test_name") |
| 138 | + |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + if args.command == "install": |
| 142 | + return install_deps(args.test_name) |
| 143 | + if args.command == "uninstall": |
| 144 | + return uninstall_deps(args.test_name) |
| 145 | + raise AssertionError(f"Unhandled command: {args.command}") |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + sys.exit(main()) |
0 commit comments