|
5 | 5 | from __future__ import annotations
|
6 | 6 |
|
7 | 7 | import subprocess
|
| 8 | +import sys |
| 9 | +import re |
8 | 10 |
|
9 | 11 | PIXI_VERSION = "0.39.0"
|
10 | 12 | CARGO_VERSION = "1.81.0"
|
11 | 13 | RUST_VERSION = "1.81.0"
|
12 | 14 |
|
13 |
| - |
14 |
| -def check_version(cmd: str, expected: str, update: str, install: str) -> bool: |
| 15 | +def get_version_output(cmd: str) -> str | None: |
15 | 16 | try:
|
16 |
| - output = subprocess.check_output([cmd, "--version"]) |
17 |
| - |
18 |
| - version = output.strip().decode("utf-8").split(" ")[1] |
| 17 | + output = subprocess.check_output([cmd, "--version"], stderr=subprocess.STDOUT) |
| 18 | + return output.strip().decode("utf-8") |
| 19 | + except (FileNotFoundError, subprocess.CalledProcessError): |
| 20 | + return None |
19 | 21 |
|
20 |
| - if version != expected: |
21 |
| - print(f"Expected {cmd} version {expected}, got {version}") |
22 |
| - print(f"Please run `{update}`") |
23 |
| - return False |
24 |
| - else: |
25 |
| - print(f"{cmd} version {version} is correct") |
26 |
| - return True |
| 22 | +def extract_version(output: str) -> str | None: |
| 23 | + match = re.search(r"\d+\.\d+\.\d+", output) |
| 24 | + return match.group(0) if match else None |
27 | 25 |
|
28 |
| - except FileNotFoundError: |
| 26 | +def check_version(cmd: str, expected: str, update: str, install: str) -> bool: |
| 27 | + output = get_version_output(cmd) |
| 28 | + if output is None: |
29 | 29 | print(f"{cmd} not found in PATH. Please install via {install}")
|
30 | 30 | return False
|
31 |
| - |
| 31 | + |
| 32 | + version = extract_version(output) |
| 33 | + if version != expected: |
| 34 | + print(f"Expected {cmd} version {expected}, got {version}") |
| 35 | + print(f"Please run `{update}`") |
| 36 | + return False |
| 37 | + |
| 38 | + print(f"{cmd} version {version} is correct") |
| 39 | + return True |
32 | 40 |
|
33 | 41 | def main() -> int:
|
34 |
| - success = True |
35 |
| - |
36 |
| - success &= check_version( |
37 |
| - "pixi", |
38 |
| - PIXI_VERSION, |
39 |
| - f"pixi self-update --version {PIXI_VERSION}", |
40 |
| - "https://pixi.sh/latest/", |
41 |
| - ) |
42 |
| - |
43 |
| - success &= check_version( |
44 |
| - "cargo", |
45 |
| - CARGO_VERSION, |
46 |
| - f"rustup install {CARGO_VERSION}", |
47 |
| - "https://rustup.rs/", |
48 |
| - ) |
49 |
| - |
50 |
| - success &= check_version( |
51 |
| - "rustc", |
52 |
| - RUST_VERSION, |
53 |
| - f"rustup install {RUST_VERSION}", |
54 |
| - "https://rustup.rs/", |
55 |
| - ) |
56 |
| - |
57 |
| - if success: |
58 |
| - exit(0) |
59 |
| - else: |
60 |
| - exit(1) |
61 |
| - |
| 42 | + success = all([ |
| 43 | + check_version("pixi", PIXI_VERSION, f"pixi self-update --version {PIXI_VERSION}", "https://pixi.sh/latest/"), |
| 44 | + check_version("cargo", CARGO_VERSION, f"rustup install {CARGO_VERSION}", "https://rustup.rs/"), |
| 45 | + check_version("rustc", RUST_VERSION, f"rustup install {RUST_VERSION}", "https://rustup.rs/"), |
| 46 | + ]) |
| 47 | + |
| 48 | + sys.exit(0 if success else 1) |
62 | 49 |
|
63 | 50 | if __name__ == "__main__":
|
64 | 51 | main()
|
0 commit comments