-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (42 loc) · 1.82 KB
/
main.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
import subprocess
import os
import sys
def main():
if len(sys.argv) < 2:
print("Usage: gcu <util_name> [arguments...]")
# print all files inside the utils directory
print("-"*80)
print("Available utilities:")
for file in os.listdir(os.path.join(os.path.dirname(__file__), 'utils')):
if file.endswith('.py'):
print(f" - {file[:-3]}")
return
script_name = sys.argv[1]
script_args = sys.argv[2:]
# Append .py extension to the script name
script_name_with_extension = "./utils/" + script_name + '.py'
script_path = os.path.join(os.path.dirname(__file__), script_name_with_extension)
if not os.path.isfile(script_path):
print(f"Script '{script_name_with_extension}' not found in the current directory.")
return
# Check if virtual environment exists
venv_path = os.path.join(os.path.dirname(__file__), 'venv')
if not os.path.isdir(venv_path):
print("Virtual environment 'venv' not found in the current directory.")
return
# Determine the path to the Python interpreter in the virtual environment
if os.name == 'nt': # Windows
python_executable = os.path.join(venv_path, 'Scripts', 'python.exe')
else: # Unix or MacOS
python_executable = os.path.join(venv_path, 'bin', 'python')
if not os.path.isfile(python_executable):
print("Python executable not found in the virtual environment.")
return
# Construct the command to run the target script with arguments
command = [python_executable, script_path] + script_args
# Run the command
result = subprocess.run(command, text=True)
if result.returncode != 0:
print(f"Script '{script_name_with_extension}' exited with an error: {result.returncode}")
if __name__ == "__main__":
main()