Printing the output of a subprocess while saving the result is not a new problem, and has been answered many times before e.g.: https://stackoverflow.com/a/28319191/5506400
This does not work for me because I am trying to maintain the shell colours printed. E.g. when one goes systemctl status application
, its prints running in green.
The above-mentioned methods all rely on reading a line one by one from subprocess, but it seems to me by then the colour information is stripped off and lost.
I tried to make an object which tee's off the stdout prints and saves them into a variable:
from subprocess import *
import sys
class Tee():
def __init__(self):
self.content = ''
self.stdout = sys.stdout
sys.stdout = self
def __enter__(self):
return self
def __exit__(self, *args):
pass
def __del__(self):
sys.stdout = self.stdout
def write(self, data):
self.content += data
self.stdout.write(data)
def flush(self):
self.content = ''
with Tee() as tee:
# Saves print to tee.content
print("Hello World")
# This line does not save prints to tee.content
run(['apt-get', 'update'])
# raises an error that tee.fileno is not supported
run(['systemctl', 'status', 'nginx'], stdout=tee)
content = tee.content
print("---------------------")
print(content)
But the problem is subprocess's stdout requires an actual file: https://stackoverflow.com/a/2298003/5506400
Is there anyway to print realtime the output of a subprocess, while maintaining the colours, and store the value to a variable (without going through a temp file)?
You can't make it with subprocess
, but pty
can. pty
creates pseudo-terminal so the command being executed detects that it's running with tty and enables color output.
import pty, os
output_bytes = []
def read(fd):
data = os.read(fd, 1024)
output_bytes.append(data)
return data
pty.spawn([command], read)
output = str(output_bytes)
# parse output as you need