Skip to content

Commit

Permalink
Clear line implementation (sonic-net#284)
Browse files Browse the repository at this point in the history
* new consutil package - includes helper functions, and show/clear/connect skeletons

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Added connect command skeleton, added show line and clear line skeletons

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Added connect and consutil packages to setup.py and to bash completion

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Added docstrings and "TODO Stub" comments to clear line, show line, and connect line

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Added clear line implementation and added "consutil clear line <linenum>" to clear/main.py

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Fixed "consutil clear" call in clear/main.py

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Changed "connecting" to "connected" in consutil clear line function

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Changed popenWrapper() name to run_command()

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>

* Update main.py

Remove SIGTERM magic number

Signed-off-by: Cayla Wanderman-Milne <t-cawand@microsoft.com>
  • Loading branch information
cawand authored and lguohan committed Jul 31, 2018
1 parent 865caed commit f02073e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ def clear_vlan_fdb(vlanid):
@click.argument('linenum')
def line(linenum):
"""Clear preexisting connection to line"""
# TODO: Stub
return
cmd = "consutil clear " + str(linenum)
run_command(cmd)

if __name__ == '__main__':
cli()
8 changes: 4 additions & 4 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# runs command, exit if stderr is written to, returns stdout otherwise
# input: cmd (str), output: output of cmd (str)
def popenWrapper(cmd):
def run_command(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output = proc.stdout.read()
error = proc.stderr.read()
Expand All @@ -40,7 +40,7 @@ def checkDevice(linenum):
# returns a sorted list of all devices (whose name matches DEVICE_PREFIX)
def getAllDevices():
cmd = "ls " + DEVICE_PREFIX + "*"
output = popenWrapper(cmd)
output = run_command(cmd)

devices = output.split('\n')
devices = list(filter(lambda dev: re.match(DEVICE_PREFIX + r"\d+", dev) != None, devices))
Expand All @@ -52,7 +52,7 @@ def getAllDevices():
# maps line number to (pid, process start time)
def getBusyDevices():
cmd = 'ps -eo pid,lstart,cmd | grep -E "(mini|pico)com"'
output = popenWrapper(cmd)
output = run_command(cmd)
processes = output.split('\n')

# matches any number of spaces then any number of digits
Expand Down Expand Up @@ -80,7 +80,7 @@ def getBusyDevices():
def getBaud(linenum):
checkDevice(linenum)
cmd = "sudo stty -F " + DEVICE_PREFIX + str(linenum)
output = popenWrapper(cmd)
output = run_command(cmd)

match = re.match(r"^speed (\d+) baud;", output)
if match != None:
Expand Down
12 changes: 11 additions & 1 deletion consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ def line():
@click.argument('linenum')
def clear(linenum):
"""Clear preexisting connection to line"""
click.echo("clear line linenum")
checkDevice(linenum)
linenum = str(linenum)

busyDevices = getBusyDevices()
if linenum in busyDevices:
pid, _ = busyDevices[linenum]
cmd = "sudo kill -SIGTERM " + pid
click.echo("Sending SIGTERM to process " + pid)
run_command(cmd)
else:
click.echo("No process is connected to line " + linenum)

# 'connect' subcommand
@consutil.command()
Expand Down

0 comments on commit f02073e

Please sign in to comment.