-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
45 lines (37 loc) · 1.7 KB
/
run.py
File metadata and controls
45 lines (37 loc) · 1.7 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
import subprocess
import sys
# python3 run.py TEST.md "sudo ./test.sh TEST_FOLDER "/home/$(logname) && open TEST.md
def run_command_with_sudo_and_save_results(command, output_file_path):
try:
# Run the command with sudo, capturing stdout and stderr
completed_process = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=False # Don't raise an exception on a non-zero exit
)
# Write the output and error (if any) to the specified output file
with open(output_file_path, 'w') as file:
file.write(f"Command: {' '.join(command)}\n")
file.write(f"Return code: {completed_process.returncode}\n")
out = completed_process.stdout
out = out.replace("[1;33m", "")
out = out.replace("[0m", "")
out = out.replace("[0;31m", "- [ ]")
out = out.replace("[0;32m", "+ [x]")
file.write(f"--- STDOUT ---\n{out}\n")
file.write(f"--- STDERR ---\n{completed_process.stderr}\n")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python run.py <output_file_path> <'command'>")
sys.exit(1)
# Output file path comes from the first script argument
output_filepath = sys.argv[1]
# The command to run comes from the second script argument
# It is expected to be a single string, it will be split to form a list
command_to_run = sys.argv[2].split(' ')
# Run the command and save the results to the output file
run_command_with_sudo_and_save_results(command_to_run, output_filepath)