From ce64df4e1eec83ef0bd3eec538998326b94d5abf Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 11 Feb 2021 16:40:03 -0800 Subject: [PATCH 1/2] Fix error output was missing --- adafruit_shell.py | 51 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/adafruit_shell.py b/adafruit_shell.py index 49cadc2..8752cd0 100644 --- a/adafruit_shell.py +++ b/adafruit_shell.py @@ -63,30 +63,35 @@ class Shell: """ 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: - decoded_output = output.decode("utf-8", errors="ignore").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", errors="ignore")) + original_stdout = sys.stdout + original_stderr = sys.stderr + try: + proc = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + full_output = "" + while True: + output = proc.stdout.readline() + err = proc.stderr.read() + if len(err) and not suppress_message: + self.error(err.decode("utf-8", errors="ignore")) + if len(output) == 0 and proc.poll() is not None: + break + if output: + decoded_output = output.decode("utf-8", errors="ignore").strip() + if not suppress_message: + self.info(decoded_output) + full_output += decoded_output + except Exception as err: # pylint: disable=broad-except + pass + finally: + sys.stdout = original_stdout + sys.stderr = original_stderr if return_output: return full_output + r = proc.poll() + if r == 0: + return True return False def info(self, message): @@ -453,7 +458,7 @@ class Shell: if os.path.exists("/etc/os-release"): with open("/etc/os-release") as f: if "Raspbian" in f.read(): - release = "Raspian" + release = "Raspbian" if self.run_command("command -v apt-get", suppress_message=True): with open("/etc/os-release") as f: release_file = f.read() From fd224b084211aceae466d0a2f3aed119f1b45c64 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 11 Feb 2021 16:51:41 -0800 Subject: [PATCH 2/2] Linted --- adafruit_shell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_shell.py b/adafruit_shell.py index 8752cd0..2ad7526 100644 --- a/adafruit_shell.py +++ b/adafruit_shell.py @@ -73,7 +73,7 @@ class Shell: while True: output = proc.stdout.readline() err = proc.stderr.read() - if len(err) and not suppress_message: + if err and not suppress_message: self.error(err.decode("utf-8", errors="ignore")) if len(output) == 0 and proc.poll() is not None: break