-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_command.py
More file actions
55 lines (47 loc) · 2.1 KB
/
shell_command.py
File metadata and controls
55 lines (47 loc) · 2.1 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
import logging
import os
import subprocess
import traceback
def execute_shell_command(command, aosp_root_path):
current_directory = os.path.dirname(os.path.realpath(__file__))
os.chdir(aosp_root_path)
result = subprocess.run(command, shell=True, capture_output=True, text=False)
log_out = result.stdout.decode('utf-8', errors='ignore').strip()
log_err = result.stderr.decode('utf-8', errors='ignore').strip()
is_success = result.returncode == 0 or "error" not in log_err.lower()
os.chdir(current_directory)
log = f"is_success: {is_success} result.returncode: {result.returncode}, stdout: {log_out} | error: {log_err}"
return is_success, log
def execute_command(command, cwd=None, shell=False):
"""
Execute a command and checks if it has an exit code of 0.
:param command: list - the command and its arguments to execute.
:return: tuple - (bool, str) - True if the command was successful, False otherwise.
"""
if not cwd:
cwd = os.getcwd()
is_success = False
try:
result = subprocess.run(command, capture_output=True, text=False, cwd=cwd, shell=shell)
logging.debug(f"Executed command: {command} - {result.returncode}")
if result.returncode == 0:
is_success = True
log = result.stdout.decode('utf-8', errors='ignore').strip()
else:
is_success = False
log = (
f"Error executing command: {command}\n"
f"Working directory: {cwd}\n"
f"Return code: {result.returncode}\n"
f"Stdout: {result.stdout.decode('utf-8', errors='ignore').strip()}\n"
f"Stderr: {result.stderr.decode('utf-8', errors='ignore').strip()}"
)
log = f"Return code: {result.returncode} with message: {result.stderr.decode('utf-8', errors='ignore').strip()}"
except Exception as e:
log = (
f"Exception while executing command: {command}\n"
f"Working directory: {cwd}\n"
f"Error: {e}\n"
f"Stack trace:\n{traceback.format_exc()}"
)
return is_success, log