|
| 1 | +import argparse |
| 2 | +import enum |
| 3 | +import json |
| 4 | +import os |
| 5 | +import platform |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +REPO_ROOT_DIR = Path(__file__).parent.parent.resolve() |
| 13 | +ENVS_CONFIG = REPO_ROOT_DIR / ".devcontainer" / "envs.json" |
| 14 | +parser = argparse.ArgumentParser(description="Manual setup for dependencies in the repo") |
| 15 | +_ = parser.add_argument( |
| 16 | + "--python-version", |
| 17 | + type=str, |
| 18 | + default=f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", |
| 19 | + help="What version to install.", |
| 20 | +) |
| 21 | +_ = parser.add_argument("--skip-check-lock", action="store_true", default=False, help="Skip the lock file check step") |
| 22 | +_ = parser.add_argument( |
| 23 | + "--optionally-check-lock", action="store_true", default=False, help="Check the lock file IFF it exists" |
| 24 | +) |
| 25 | +_ = parser.add_argument( |
| 26 | + "--no-python", |
| 27 | + action="store_true", |
| 28 | + default=False, |
| 29 | + help="Do not process any environments using python package managers", |
| 30 | +) |
| 31 | +_ = parser.add_argument( |
| 32 | + "--no-node", action="store_true", default=False, help="Do not process any environments using node package managers" |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class PackageManager(str, enum.Enum): |
| 37 | + UV = "uv" |
| 38 | + PNPM = "pnpm" |
| 39 | + |
| 40 | + |
| 41 | +class EnvConfig: |
| 42 | + def __init__(self, json_dict: dict[str, Any]): |
| 43 | + super().__init__() |
| 44 | + self.package_manager = PackageManager(json_dict["package_manager"]) |
| 45 | + self.path = REPO_ROOT_DIR |
| 46 | + if "relative_directory" in json_dict: |
| 47 | + self.path = REPO_ROOT_DIR / json_dict["relative_directory"] |
| 48 | + if self.package_manager == PackageManager.UV: |
| 49 | + self.lock_file = self.path / "uv.lock" |
| 50 | + elif self.package_manager == PackageManager.PNPM: |
| 51 | + self.lock_file = self.path / "pnpm-lock.yaml" |
| 52 | + else: |
| 53 | + raise NotImplementedError(f"Package manager {self.package_manager} is not supported") |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + args = parser.parse_args(sys.argv[1:]) |
| 58 | + is_windows = platform.system() == "Windows" |
| 59 | + uv_env = dict(os.environ) |
| 60 | + uv_env.update({"UV_PYTHON_PREFERENCE": "only-system", "UV_PYTHON": args.python_version}) |
| 61 | + skip_check_lock = args.skip_check_lock |
| 62 | + if skip_check_lock and args.optionally_check_lock: |
| 63 | + print("Cannot skip and optionally check the lock file at the same time.") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | + with ENVS_CONFIG.open("r") as f: |
| 67 | + envs = json.load(f) |
| 68 | + |
| 69 | + for env_dict in envs: |
| 70 | + env = EnvConfig(env_dict) |
| 71 | + if args.no_python and env.package_manager == PackageManager.UV: |
| 72 | + print(f"Skipping environment {env.path} as it uses a Python package manager and --no-python is set") |
| 73 | + continue |
| 74 | + if args.no_node and env.package_manager == PackageManager.PNPM: |
| 75 | + print(f"Skipping environment {env.path} as it uses a Node package manager and --no-node is set") |
| 76 | + continue |
| 77 | + env_skip_check_lock = skip_check_lock |
| 78 | + if args.optionally_check_lock and env.lock_file.exists(): |
| 79 | + env_skip_check_lock = False |
| 80 | + if not env_skip_check_lock: |
| 81 | + if env.package_manager == PackageManager.UV: |
| 82 | + _ = subprocess.run(["uv", "lock", "--check", "--directory", str(env.path)], check=True, env=uv_env) |
| 83 | + elif env.package_manager == PackageManager.PNPM: |
| 84 | + pass # doesn't seem to be a way to do this https://github.com/orgs/pnpm/discussions/3202 |
| 85 | + else: |
| 86 | + raise NotImplementedError(f"Package manager {env.package_manager} does not support lock file checking") |
| 87 | + if env.package_manager == PackageManager.UV: |
| 88 | + sync_command = ["uv", "sync", "--directory", str(env.path)] |
| 89 | + if not env_skip_check_lock: |
| 90 | + sync_command.append("--frozen") |
| 91 | + _ = subprocess.run( |
| 92 | + sync_command, |
| 93 | + check=True, |
| 94 | + env=uv_env, |
| 95 | + ) |
| 96 | + elif env.package_manager == PackageManager.PNPM: |
| 97 | + pnpm_command = ["pnpm", "install", "--dir", str(env.path)] |
| 98 | + if not env_skip_check_lock: |
| 99 | + pnpm_command.append("--frozen-lockfile") |
| 100 | + if is_windows: |
| 101 | + pwsh = shutil.which("pwsh") or shutil.which("powershell") |
| 102 | + if not pwsh: |
| 103 | + raise FileNotFoundError("Neither 'pwsh' nor 'powershell' found on PATH") |
| 104 | + pnpm_command = [ |
| 105 | + pwsh, |
| 106 | + "-NoProfile", |
| 107 | + "-NonInteractive", |
| 108 | + "-Command", |
| 109 | + " ".join(pnpm_command), |
| 110 | + ] |
| 111 | + _ = subprocess.run( |
| 112 | + pnpm_command, |
| 113 | + check=True, |
| 114 | + ) |
| 115 | + else: |
| 116 | + raise NotImplementedError(f"Package manager {env.package_manager} is not supported for installation") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments