-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.py
More file actions
79 lines (61 loc) · 2.87 KB
/
Copy pathexec.py
File metadata and controls
79 lines (61 loc) · 2.87 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
import subprocess
from merlin import Response, MerlinClient
from enum import Enum
import os
from errorstuff import Err, LogCommandOutput
def block_until_key(signal) -> bool:
if input() == signal:
return False
else:
return True
class RunSignal(Enum):
EXIT_SUCCESSFUL = 0
EXIT_UNSUCCESSFUL = 1
FIX_ERR_WITH_MERLIN = 2
EXIT_ABORTED = 3
def run_response(res: Response, client: MerlinClient, prompt: str, checkBeforeExecution: bool=False) -> tuple[RunSignal, ]:
current_dir = os.getcwd()
command_results: list[tuple[str, str]] = []
print(res.intro + "\n")
for i in range(len(res.commands)):
command = res.commands[i]
message = res.line_by_line[i]
if checkBeforeExecution:
print("Ok to run '{}'? (press 'N' key to abort)")
if not block_until_key('N'):
return (RunSignal.EXIT_ABORTED)
print(f"Current command: {message} | Would you like to run it? (press 'N' to skip, 'Enter' to run)")
if not block_until_key("N"):
continue # Skip this command if 'N' is pressed
if 'cd' in command.strip():
try:
# Extract the target directory
target_dir = command.split('cd ', 1)[1].strip()
# Handle relative paths
new_dir = os.path.join(current_dir, target_dir)
# Update and change to new directory
os.chdir(new_dir)
current_dir = os.getcwd()
print(f"Changed directory to: {current_dir}")
continue
except Exception as e:
print(f"Error changing directory: {e}")
return (RunSignal.FIX_ERR_WITH_MERLIN, Err(command, str(e), ""))
result = subprocess.run(command, shell=True, check=False, capture_output=True, cwd=current_dir)
if result.returncode != 0:
errmsg = result.stderr
out = result.stdout
print("Error happened while running the following command:")
print(command)
print("Would you like Merlin to fix it? (press N for no)")
if not block_until_key('N'):
return (RunSignal.EXIT_UNSUCCESSFUL, None)
else:
return (RunSignal.FIX_ERR_WITH_MERLIN, Err(command, errmsg, out))
command_output = result.stdout.decode('utf-8') if hasattr(result.stdout, 'decode') else str(result.stdout)
print(command_output)
command_results.append((command, command_output))
print(res.conclusion, end='\n\n')
logobj = LogCommandOutput((prompt, res.intro, res.conclusion), command_results)
client.add_to_history(logobj)
return (RunSignal.EXIT_SUCCESSFUL, None)