Added saving command run responses and restoring them

This commit is contained in:
Zvonimir Sabljic
2023-08-03 20:40:10 +02:00
parent f451f5053f
commit 6395f6fba7
3 changed files with 95 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ import queue
import time
from termcolor import colored
from database.database import get_command_run_from_hash_id, save_command_run
from utils.questionary import styled_text
from const.code_execution import MAX_COMMAND_DEBUG_TRIES
@@ -34,22 +35,33 @@ def run_command(command, root_path, q_stdout, q_stderr, pid_container):
return process
def execute_command(root_path, command, timeout=5000):
def execute_command(project, command, timeout=5000):
# check if we already have the command run saved
print(colored(f'Can i execute the command: `{command}` with {timeout}ms timeout?', 'white', attrs=['bold']))
project.command_runs_count += 1
command_run = get_command_run_from_hash_id(project, command)
if command_run is not None and project.skip_steps:
# if we do, use it
print(colored(f'Restoring command run response id {command_run.id}:\n```\n{command_run.cli_response}```', 'yellow'))
return command_run.cli_response
answer = styled_text(
f'Can i execute the command: `{command}` with {timeout}ms timeout?\n' +
'If yes, just press ENTER and if not, please paste the output of running this command here and press ENTER'
)
return_value = None
if answer != '':
return answer[-2000:]
return_value = answer[-2000:]
q_stderr = queue.Queue()
q = queue.Queue()
pid_container = [None]
process = run_command(command, root_path, q, q_stderr, pid_container)
process = run_command(command, project.root_path, q, q_stderr, pid_container)
output = ''
start_time = time.time()
while True:
while True and return_value is None:
elapsed_time = time.time() - start_time
# Check if process has finished
if process.poll() is not None:
@@ -79,7 +91,13 @@ def execute_command(root_path, command, timeout=5000):
stderr_output = ''
while not q_stderr.empty():
stderr_output += q_stderr.get_nowait()
return output[-2000:] if output != '' else stderr_output[-2000:]
if return_value is None:
return_value = output[-2000:] if output != '' else stderr_output[-2000:]
command_run = save_command_run(project, command, return_value)
return return_value
def build_directory_tree(path, prefix="", ignore=None, is_last=False):
"""Build the directory tree structure in tree-like format.
@@ -119,7 +137,7 @@ def build_directory_tree(path, prefix="", ignore=None, is_last=False):
return output
def execute_command_and_check_cli_response(command, timeout, convo):
cli_response = execute_command(command, timeout)
cli_response = execute_command(convo.agent.project, command, timeout)
response = convo.send_message('dev_ops/ran_command.prompt',
{ 'cli_response': cli_response, 'command': command })
return response
@@ -127,7 +145,7 @@ def execute_command_and_check_cli_response(command, timeout, convo):
def run_command_until_success(command, timeout, convo):
command_executed = False
for _ in range(MAX_COMMAND_DEBUG_TRIES):
cli_response = execute_command(convo.agent.project.root_path, command, timeout)
cli_response = execute_command(convo.agent.project, command, timeout)
response = convo.send_message('dev_ops/ran_command.prompt',
{'cli_response': cli_response, 'command': command})