Skip to content

Commit 82deb93

Browse files
authored
Merge pull request #9 from makermelissa/master
Fix error output was missing
2 parents 695fd5e + fd224b0 commit 82deb93

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

adafruit_shell.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,35 @@ def run_command(self, cmd, suppress_message=False, return_output=False):
6363
"""
6464
Run a shell command and show the output as it runs
6565
"""
66-
proc = subprocess.Popen(
67-
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
68-
)
69-
full_output = ""
70-
while True:
71-
output = proc.stdout.readline()
72-
if len(output) == 0 and proc.poll() is not None:
73-
break
74-
if output:
75-
decoded_output = output.decode("utf-8", errors="ignore").strip()
76-
if not suppress_message:
77-
self.info(decoded_output)
78-
full_output += decoded_output
66+
original_stdout = sys.stdout
67+
original_stderr = sys.stderr
68+
try:
69+
proc = subprocess.Popen(
70+
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
71+
)
72+
full_output = ""
73+
while True:
74+
output = proc.stdout.readline()
75+
err = proc.stderr.read()
76+
if err and not suppress_message:
77+
self.error(err.decode("utf-8", errors="ignore"))
78+
if len(output) == 0 and proc.poll() is not None:
79+
break
80+
if output:
81+
decoded_output = output.decode("utf-8", errors="ignore").strip()
82+
if not suppress_message:
83+
self.info(decoded_output)
84+
full_output += decoded_output
85+
except Exception as err: # pylint: disable=broad-except
86+
pass
87+
finally:
88+
sys.stdout = original_stdout
89+
sys.stderr = original_stderr
90+
if return_output:
91+
return full_output
7992
r = proc.poll()
8093
if r == 0:
81-
if return_output:
82-
return full_output
8394
return True
84-
85-
err = proc.stderr.read()
86-
if not suppress_message:
87-
self.error(err.decode("utf-8", errors="ignore"))
88-
if return_output:
89-
return full_output
9095
return False
9196

9297
def info(self, message):
@@ -453,7 +458,7 @@ def get_os(self):
453458
if os.path.exists("/etc/os-release"):
454459
with open("/etc/os-release") as f:
455460
if "Raspbian" in f.read():
456-
release = "Raspian"
461+
release = "Raspbian"
457462
if self.run_command("command -v apt-get", suppress_message=True):
458463
with open("/etc/os-release") as f:
459464
release_file = f.read()

0 commit comments

Comments
 (0)