-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
93 lines (78 loc) · 2.66 KB
/
utils.py
File metadata and controls
93 lines (78 loc) · 2.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import asyncio
import os
import subprocess
async def run_subprocess(cmd, env=None, cwd=None):
# Create the subprocess with pipes for stdout and stderr
# Set up environment variables
subprocess_env = dict(os.environ, PYTHONUNBUFFERED="1")
if env:
subprocess_env.update(env)
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=subprocess_env,
cwd=cwd,
)
# Asynchronous function to read and print lines from a stream
async def read_stream(stream, is_stderr):
while True:
line = await stream.readline()
if not line:
break
line_stripped = line.decode().strip()
if line_stripped:
if is_stderr and ("error" in line_stripped.lower() or "exception" in line_stripped.lower()):
print(f"ERROR: {line_stripped}", flush=True)
else:
print(line_stripped, flush=True)
# Run both stream readers concurrently
await asyncio.gather(
read_stream(proc.stdout, False),
read_stream(proc.stderr, True),
)
# Wait for the subprocess to finish
await proc.wait()
return proc.returncode
def run_subprocess_realtime(cmd, timeout=None):
"""
Run subprocess with real-time output without using asyncio.
This avoids event loop conflicts while still providing live output.
"""
import time
env = dict(os.environ, PYTHONUNBUFFERED="1")
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, # Merge stderr into stdout
universal_newlines=True,
bufsize=1, # Line buffered
env=env
)
start_time = time.time()
# Read output line by line in real-time
while True:
# Check timeout
if timeout and (time.time() - start_time) > timeout:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
raise subprocess.TimeoutExpired(cmd, timeout)
# Read line with timeout
line = proc.stdout.readline()
if line:
print(line.strip(), flush=True)
elif proc.poll() is not None:
# Process has finished
break
else:
# No output yet, wait a bit
time.sleep(0.1)
# Get any remaining output
remaining_output = proc.stdout.read()
if remaining_output:
print(remaining_output.strip(), flush=True)
return proc.returncode