From e51d44f2832a3ff6b964e7e6110a1db49b5745b3 Mon Sep 17 00:00:00 2001 From: Blueve <672454911@qq.com> Date: Thu, 15 Oct 2020 10:33:06 +0800 Subject: [PATCH] [consutil] Fix issue where the show line command crash if no ttyUSB exists (#1173) Signed-off-by: Jing Kan jika@microsoft.com --- consutil/lib.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/consutil/lib.py b/consutil/lib.py index 7a6239c78693..ae4d8a81548a 100644 --- a/consutil/lib.py +++ b/consutil/lib.py @@ -49,17 +49,16 @@ line = fp.readlines() DEVICE_PREFIX = "/dev/" + line[0] - -# runs command, exit if stderr is written to, returns stdout otherwise -# input: cmd (str), output: output of cmd (str) -def run_command(cmd): +# runs command, exit if stderr is written to and abort argument is ture, returns stdout, stderr otherwise +# input: cmd (str, bool), output: output of cmd (str) and error of cmd (str) if abort is not true +def run_command(cmd, abort=True): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) output = proc.stdout.read() error = proc.stderr.read() - if error != "": + if abort and error != "": click.echo("Command resulted in error: {}".format(error)) sys.exit(ERR_CMD) - return output + return output if abort else (output, error) # returns a list of all lines def getAllLines(): @@ -76,7 +75,7 @@ def getAllLines(): # Querying device directory to get all available console ports cmd = "ls " + DEVICE_PREFIX + "*" - output = run_command(cmd) + output, _ = run_command(cmd, abort=False) availableTtys = output.split('\n') availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys)) for tty in availableTtys: