From f21998612fcde9fb8ab426f10e8dc4d1d33a366e Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 1 Oct 2020 11:46:16 -0600 Subject: [PATCH] Can get output returned from run_command --- adafruit_shell.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/adafruit_shell.py b/adafruit_shell.py index 5898cb0..33d8098 100644 --- a/adafruit_shell.py +++ b/adafruit_shell.py @@ -59,26 +59,34 @@ class Shell: ) return prompt.options(message, options) - def run_command(self, cmd, suppress_message=False): + def run_command(self, cmd, suppress_message=False, return_output=False): """ Run a shell command and show the output as it runs """ proc = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) + full_output = "" while True: output = proc.stdout.readline() if len(output) == 0 and proc.poll() is not None: break - if output and not suppress_message: - self.info(output.decode("utf-8").strip()) + if output: + decoded_output = output.decode("utf-8").strip() + if not suppress_message: + self.info(decoded_output) + full_output += decoded_output r = proc.poll() if r == 0: + if return_output: + return full_output return True err = proc.stderr.read() if not suppress_message: self.error(err.decode("utf-8")) + if return_output: + return full_output return False def info(self, message): @@ -366,6 +374,11 @@ class Shell: service_file.write(content) service_file.close() + @staticmethod + def is_python3(): + "Check if we are running Python 3 or later" + return int(platform.python_version()[0]) >= 3 + @staticmethod def is_linux(): """