replace move to popen shell

This commit is contained in:
hyugogirubato
2025-06-14 11:59:53 +02:00
parent bd94c781c0
commit bcbf127624
+5 -10
View File
@@ -26,14 +26,9 @@ def shell(prompt: List[str]) -> Tuple[bool, str]:
Tuple[bool, str]: A tuple where the first value is True if the command failed, Tuple[bool, str]: A tuple where the first value is True if the command failed,
and the second value is the decoded standard output (stdout) string. and the second value is the decoded standard output (stdout) string.
Example:
shell(["ls", "-l"])
(False, "total 0\n-rw-r--r-- 1 user ...")
Note: Note:
The return status is inverted (True means failure), which should be considered The return status is inverted (True means failure), which should be considered
when checking execution outcomes. when checking execution outcomes.
""" """
# Convert all arguments to strings in case any are not # Convert all arguments to strings in case any are not
prompt = list(map(str, prompt)) prompt = list(map(str, prompt))
@@ -41,12 +36,12 @@ def shell(prompt: List[str]) -> Tuple[bool, str]:
# Uncomment for debugging shell command execution # Uncomment for debugging shell command execution
# logging.getLogger('Shell').debug('Executing command: %s', ' '.join(prompt)) # logging.getLogger('Shell').debug('Executing command: %s', ' '.join(prompt))
try: try:
# Execute the command and capture the output # Start the process with stdout and stderr redirected to PIPE
# TODO: Redirect standard output and error in PIP process = Popen(prompt, stdout=PIPE, stderr=PIPE, universal_newlines=True, encoding='utf-8')
sp = run(prompt, capture_output=True) stdout, stderr = process.communicate() # Waits for process to finish and collects output
# Return True if the command failed (non-zero exit), along with stdout # Return True on failure (non-zero exit code), and the decoded stdout
return sp.returncode != 0, sp.stdout.decode('utf-8').strip() return process.wait() != 0, (stdout + stderr).strip()
except KeyboardInterrupt: except KeyboardInterrupt:
return False, '' return False, ''