Skip to content

Commit

Permalink
[consutil] Display remote device name in show command (#1120)
Browse files Browse the repository at this point in the history
- Display remote device name in consutil show result table

Co-authored-by: Jing Kan<jika@microsoft.com>
  • Loading branch information
Blueve committed Sep 18, 2020
1 parent e634d88 commit c138eaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
44 changes: 31 additions & 13 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ERR_DEV = 2

CONSOLE_PORT_TABLE = "CONSOLE_PORT"
LINE_KEY = "LINE"
BAUD_KEY = "baud_rate"
DEVICE_KEY = "remote_device"
FLOW_KEY = "flow_control"
Expand All @@ -46,22 +47,42 @@ def run_command(cmd):
sys.exit(ERR_CMD)
return output

# returns a sorted list of all devices (whose name matches DEVICE_PREFIX)
# returns a sorted list of all devices
def getAllDevices():
config_db = ConfigDBConnector()
config_db.connect()

# Querying CONFIG_DB to get configured console ports
keys = config_db.get_keys(CONSOLE_PORT_TABLE)
devices = []
for k in keys:
device = config_db.get_entry(CONSOLE_PORT_TABLE, k)
device[LINE_KEY] = k
devices.append(device)

# Querying device directory to get all available console ports
cmd = "ls " + DEVICE_PREFIX + "*"
output = run_command(cmd)

devices = output.split('\n')
devices = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, devices))
devices.sort(key=lambda dev: int(dev[len(DEVICE_PREFIX):]))

availableTtys = output.split('\n')
availableTtys = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, availableTtys))
for tty in availableTtys:
k = tty[len(DEVICE_PREFIX):]
if k not in keys:
device = { LINE_KEY: k }
devices.append(device)

devices.sort(key=lambda dev: int(dev[LINE_KEY]))
return devices

# exits if inputted line number does not correspond to a device
# input: linenum
def checkDevice(linenum):
devices = getAllDevices()
if DEVICE_PREFIX + str(linenum) not in devices:
config_db = ConfigDBConnector()
config_db.connect()

entry = config_db.get_entry(CONSOLE_PORT_TABLE, str(linenum))
if not entry:
click.echo("Line number {} does not exist".format(linenum))
sys.exit(ERR_DEV)

Expand Down Expand Up @@ -120,12 +141,9 @@ def getLineNumber(target, deviceBool):
config_db.connect()

devices = getAllDevices()
linenums = list(map(lambda dev: dev[len(DEVICE_PREFIX):], devices))

for linenum in linenums:
entry = config_db.get_entry(CONSOLE_PORT_TABLE, linenum)
if DEVICE_KEY in entry and entry[DEVICE_KEY] == target:
return linenum
for device in devices:
if DEVICE_KEY in device and device[DEVICE_KEY] == target:
return device[LINE_KEY]

click.echo("Device {} does not exist".format(target))
sys.exit(ERR_DEV)
Expand Down
7 changes: 4 additions & 3 deletions consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ def show():
devices = getAllDevices()
busyDevices = getBusyDevices()

header = ["Line", "Actual/Configured Baud", "PID", "Start Time"]
header = ["Line", "Actual/Configured Baud", "PID", "Start Time", "Device"]
body = []
for device in devices:
lineNum = device[11:]
lineNum = device[LINE_KEY]
busy = " "
pid = ""
date = ""
remoteDevice = '-' if DEVICE_KEY not in device else device[DEVICE_KEY]
if lineNum in busyDevices:
pid, date = busyDevices[lineNum]
busy = "*"
actBaud, confBaud, _ = getConnectionInfo(lineNum)
# repeated "~" will be replaced by spaces - hacky way to align the "/"s
baud = "{}/{}{}".format(actBaud, confBaud, "~"*(15-len(confBaud)))
body.append([busy+lineNum, baud, pid, date])
body.append([busy+lineNum, baud, pid, date, remoteDevice])

# replace repeated "~" with spaces - hacky way to align the "/"s
click.echo(tabulate(body, header, stralign="right").replace('~', ' '))
Expand Down

0 comments on commit c138eaa

Please sign in to comment.