Skip to content

Commit e51d44f

Browse files
authored
[consutil] Fix issue where the show line command crash if no ttyUSB exists (sonic-net#1173)
Signed-off-by: Jing Kan [email protected]
1 parent a733df5 commit e51d44f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

consutil/lib.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@
4949
line = fp.readlines()
5050
DEVICE_PREFIX = "/dev/" + line[0]
5151

52-
53-
# runs command, exit if stderr is written to, returns stdout otherwise
54-
# input: cmd (str), output: output of cmd (str)
55-
def run_command(cmd):
52+
# runs command, exit if stderr is written to and abort argument is ture, returns stdout, stderr otherwise
53+
# input: cmd (str, bool), output: output of cmd (str) and error of cmd (str) if abort is not true
54+
def run_command(cmd, abort=True):
5655
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
5756
output = proc.stdout.read()
5857
error = proc.stderr.read()
59-
if error != "":
58+
if abort and error != "":
6059
click.echo("Command resulted in error: {}".format(error))
6160
sys.exit(ERR_CMD)
62-
return output
61+
return output if abort else (output, error)
6362

6463
# returns a list of all lines
6564
def getAllLines():
@@ -76,7 +75,7 @@ def getAllLines():
7675

7776
# Querying device directory to get all available console ports
7877
cmd = "ls " + DEVICE_PREFIX + "*"
79-
output = run_command(cmd)
78+
output, _ = run_command(cmd, abort=False)
8079
availableTtys = output.split('\n')
8180
availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys))
8281
for tty in availableTtys:

0 commit comments

Comments
 (0)