Skip to content

Commit

Permalink
[consutil] Fix issue where the show line command crash if no ttyUSB e…
Browse files Browse the repository at this point in the history
…xists (#1173)

Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve authored Oct 15, 2020
1 parent a733df5 commit e51d44f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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:
Expand Down

0 comments on commit e51d44f

Please sign in to comment.