-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
71 lines (60 loc) · 2.42 KB
/
util.py
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
import os
import sys
import shutil
import subprocess
def writeViteEnv(env, output_file="webapp/.env"):
print("Writing environment file for web app")
# Open the file for writing
with open(output_file, "w") as f:
for key, value in env.items():
if not key.startswith("__") and isinstance(value, (str, int, float)):
f.write(f"{key}={value}\n")
print(f"Environment variables have been written to {output_file}")
def substitutions(currdir, env):
if os.path.isdir(currdir):
try:
for file in os.listdir(currdir):
substitutions(os.path.join(currdir, file), env)
except:
print(f"Couldn't process {currdir}")
else:
if currdir.endswith(".template"):
print("Applying substitutions to " + currdir)
newFile = currdir.replace(".template","")
with open(currdir, 'r') as f:
templateText = f.read()
for k, v in vars(env).items():
templateText = templateText.replace("$"+k, str(v))
newFile = newFile.replace("$"+k, str(v)) # also templetize the filename (!)
print(f"Writing to {newFile}")
with open(newFile, 'w+') as f:
f.write(templateText)
def initializeFiles():
# Check if we are in a GitHub Actions environment
in_github_actions = os.getenv("GITHUB_ACTIONS") == "true"
print(in_github_actions)
if not os.path.isfile("env.py"):
shutil.copy("env.example.py", "env.py")
print("env.py file did not exist and has been created. Please edit it to update the necessary values, then re-run this script.")
# Exit only if not in GitHub Actions
if not in_github_actions:
sys.exit(1)
else:
print("Running in GitHub Actions, continuing without exiting.")
import docker
def check_nvidia_gpu():
print("NVIDIA GPU Detected on system")
try:
# Try nvidia-smi command
subprocess.run(["nvidia-smi"], check=True, capture_output=True)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False
def check_amd_gpu():
print("AMD GPU Detected on system")
try:
# Try rocm-smi command
subprocess.run(["rocm-smi"], check=True, capture_output=True)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False