-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.py
More file actions
72 lines (57 loc) · 2.67 KB
/
checker.py
File metadata and controls
72 lines (57 loc) · 2.67 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
import subprocess
import shutil
import platform
import pathlib
PYTHON_VERSION = platform.python_version()
INSTALL_DIR = pathlib.Path("./runner/python")
SOURCE_DIR = INSTALL_DIR / f"Python-{PYTHON_VERSION}"
PYTHON_BIN = SOURCE_DIR / "bin" / "python3"
def get_python_bin():
INSTALL_DIR.mkdir(parents=True, exist_ok=True)
if PYTHON_BIN.exists():
try:
subprocess.run([PYTHON_BIN, "--version"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return PYTHON_BIN
except Exception:
print("[!] Found python binary but it's not runnable, reinstalling...")
# Create venv
try:
print("[*] Cloning python via venv...")
subprocess.run(["python3", "-m", "venv", "--without-pip", str(SOURCE_DIR)], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError:
print("[!] Found python binary but venv doesn't exist...")
raise RuntimeError(f"[!] Found python binary but venv doesn't exist...")
print(f"[+] Python ready at {PYTHON_BIN}")
return PYTHON_BIN
def check_environ():
environ = ["uname"]
commands = ["javac", "java", "g++", "gcc", "python3"]
python_bin = get_python_bin()
print("=== 🔍 Checking environment... ===\n")
environ.extend(commands)
for cmd in environ:
path = shutil.which(cmd)
if path:
try:
# Special case cho uname (nó xài khác tí)
if cmd == "uname":
output = subprocess.check_output([cmd, "-a"], stderr=subprocess.STDOUT)
else:
if cmd == "python3":
output = subprocess.check_output([python_bin, "--version"], stderr=subprocess.STDOUT)
else:
output = subprocess.check_output([cmd, "--version"], stderr=subprocess.STDOUT)
first_line = output.decode("utf-8", errors="ignore").splitlines()[0]
print(f"✅ {cmd} found: {first_line}")
except Exception as e:
print(f"⚠️ {cmd} found but failed to get version: {e}")
else:
print(f"❌ {cmd} not found in PATH")
if cmd in commands:
raise RuntimeError(f"Not supported {cmd}, the program will be terminated")
else:
raise RuntimeError(f"Not supported environment {platform.system()}, the program will be terminated")
print("\n=== 🧠 System Info ===")
print(f"OS: {platform.system()} {platform.release()}")
print(f"Python (node): {platform.python_version()}")
print("\n=== ✅ Environment check completed, Starting Judgment node ===\n")