mirror of
https://github.com/Pythagora-io/gpt-pilot.git
synced 2026-01-09 21:27:53 -05:00
Added showing stderr output is stdout is empty(''), fixed so we can stop a command if it takes too long or hangs, and show the CLI output in real-time as is being logged
This commit is contained in:
@@ -5,29 +5,41 @@ import threading
|
|||||||
import queue
|
import queue
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def enqueue_output(out, queue):
|
from termcolor import colored
|
||||||
for line in iter(out.readline, b''):
|
|
||||||
queue.put(line)
|
def enqueue_output(out, q):
|
||||||
|
for line in iter(out.readline, ''):
|
||||||
|
q.put(line)
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
def run_command(command, queue, pid_container):
|
def run_command(command, q, pid_container):
|
||||||
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, preexec_fn=os.setsid)
|
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||||
pid_container[0] = process.pid
|
pid_container[0] = process.pid
|
||||||
t = threading.Thread(target=enqueue_output, args=(process.stdout, queue))
|
t = threading.Thread(target=enqueue_output, args=(process.stdout, q))
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
t.start()
|
t.start()
|
||||||
|
return process
|
||||||
|
|
||||||
def execute_command(command, timeout=5):
|
def execute_command(command, timeout=5):
|
||||||
q = queue.Queue()
|
q = queue.Queue()
|
||||||
pid_container = [None]
|
pid_container = [None]
|
||||||
run_command(command, q, pid_container)
|
process = run_command(command, q, pid_container)
|
||||||
output = ''
|
output = ''
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
elapsed_time = time.time() - start_time
|
elapsed_time = time.time() - start_time
|
||||||
|
|
||||||
|
# Check if process has finished
|
||||||
|
if process.poll() is not None:
|
||||||
|
# Get remaining lines from the queue
|
||||||
|
while not q.empty():
|
||||||
|
output += q.get_nowait()
|
||||||
|
break
|
||||||
|
|
||||||
|
# If timeout is reached, kill the process
|
||||||
if elapsed_time > timeout:
|
if elapsed_time > timeout:
|
||||||
os.killpg(pid_container[0], signal.SIGKILL)
|
os.kill(pid_container[0], signal.SIGKILL)
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -37,8 +49,11 @@ def execute_command(command, timeout=5):
|
|||||||
|
|
||||||
if line:
|
if line:
|
||||||
output += line
|
output += line
|
||||||
|
print(colored('CLI OUTPUT:', 'green') + line)
|
||||||
|
print(line, end='') # This will print the output in real-time
|
||||||
|
|
||||||
return output[-2000:]
|
stderr_output = process.stderr.read()
|
||||||
|
return output[-2000:] if output != '' else stderr_output[-2000:]
|
||||||
|
|
||||||
def build_directory_tree(path, prefix="", ignore=None, is_last=False):
|
def build_directory_tree(path, prefix="", ignore=None, is_last=False):
|
||||||
"""Build the directory tree structure in tree-like format.
|
"""Build the directory tree structure in tree-like format.
|
||||||
|
|||||||
Reference in New Issue
Block a user