Skip to content

Commit faf91fc

Browse files
feat: add dev environment setup check script
1 parent 7ecccce commit faf91fc

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

scripts/check_env.py

+29-42
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,47 @@
55
from __future__ import annotations
66

77
import subprocess
8+
import sys
9+
import re
810

911
PIXI_VERSION = "0.39.0"
1012
CARGO_VERSION = "1.81.0"
1113
RUST_VERSION = "1.81.0"
1214

13-
14-
def check_version(cmd: str, expected: str, update: str, install: str) -> bool:
15+
def get_version_output(cmd: str) -> str | None:
1516
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
1921

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
2725

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:
2929
print(f"{cmd} not found in PATH. Please install via {install}")
3030
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
3240

3341
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)
6249

6350
if __name__ == "__main__":
6451
main()

0 commit comments

Comments
 (0)