-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_command.py
81 lines (63 loc) · 2.06 KB
/
run_command.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
72
73
74
75
76
77
78
79
80
81
import select
import subprocess
import sys
from typing import List
from dotenv import load_dotenv
# List of dotenv files to load, in order of priority
# (last one has the highest priority)
DOTENV_FILES: List[str] = [
"./.example.env",
"./.local.env",
]
def load_env_vars() -> None:
for dotenv_file in DOTENV_FILES:
try:
print(f"Loading env vars from: {dotenv_file}")
load_dotenv(dotenv_file)
except FileNotFoundError:
print(f"File not found: {dotenv_file}")
continue
def run_command(cmd: str) -> int:
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if process.stdout is None or process.stderr is None:
raise ValueError("Failed to capture stdout or stderr")
stdout, stderr = process.stdout, process.stderr
while True:
reads = [stdout.fileno(), stderr.fileno()]
readable, _, _ = select.select(reads, [], [])
for r in readable:
if r == stdout.fileno():
output = stdout.readline()
if output:
print(output.decode("utf-8").strip())
if r == stderr.fileno():
error = stderr.readline()
if error:
print(error.decode("utf-8").strip(), file=sys.stderr)
if process.poll() is not None:
break
# Ensure all remaining output is printed
while True:
output = stdout.readline()
if output:
print(output.decode("utf-8").strip())
else:
break
while True:
error = stderr.readline()
if error:
print(error.decode("utf-8").strip(), file=sys.stderr)
else:
break
return process.returncode
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <command>")
sys.exit(1)
load_env_vars()
command = " ".join(sys.argv[1:])
exit_code = run_command(command)
print(f"Command exited with code: {exit_code}")
sys.exit(exit_code)