-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tool to convert port alias when used as bash pipe (#135)
* Add a tool to convert port alias when used as bash pipe
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import subprocess | ||
from cStringIO import StringIO | ||
from portconfig import get_port_config | ||
|
||
|
||
def get_platform_hwsku(): | ||
hwsku = None | ||
platform = None | ||
command = "show platform summary" | ||
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | ||
output = p.stdout.readlines() | ||
for line in output: | ||
tokens = line.split() | ||
if not tokens: | ||
continue | ||
if tokens[0].lower() == 'hwsku:': | ||
hwsku = tokens[1] | ||
elif tokens[0].lower() == 'platform:': | ||
platform = tokens[1] | ||
return (platform, hwsku) | ||
|
||
def translate_line(line, ports): | ||
allowed_symbols = ['-', '_'] | ||
sb = StringIO() | ||
start = 0 | ||
end = 0 | ||
while end < len(line): | ||
if line[end].isalnum() or line[end] in allowed_symbols: | ||
pass | ||
else: | ||
# End of a word | ||
word = line[start:end] | ||
if word in ports: | ||
sb.write(ports[word]['alias']) | ||
else: | ||
sb.write(word) | ||
sb.write(line[end]) | ||
start = end + 1 | ||
end += 1 | ||
if start != len(line): | ||
word = line[start:] | ||
if word in ports: | ||
sb.write(ports[word]['alias']) | ||
else: | ||
sb.write(word) | ||
return sb.getvalue() | ||
|
||
def main(): | ||
(platform, hwsku) = get_platform_hwsku() | ||
(ports, _) = get_port_config(hwsku, platform) | ||
for line in sys.stdin: | ||
sys.stdout.write(translate_line(line, ports)) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import glob | ||
from setuptools import setup | ||
import unittest | ||
|
||
def get_test_suite(): | ||
test_loader = unittest.TestLoader() | ||
test_suite = test_loader.discover('sonic-utilities-tests', pattern='*.py') | ||
return test_suite | ||
|
||
setup( | ||
name='sonic-utilities', | ||
|
@@ -11,7 +17,7 @@ | |
url='https://github.com/Azure/sonic-utilities', | ||
maintainer='Joe LeVeque', | ||
maintainer_email='[email protected]', | ||
packages=['config', 'sfputil', 'show', 'sonic_eeprom', 'sonic_installer', 'sonic_psu', 'sonic_sfp', 'acl_loader'], | ||
packages=['config', 'sfputil', 'show', 'sonic_eeprom', 'sonic_installer', 'sonic_psu', 'sonic_sfp', 'acl_loader', 'sonic-utilities-tests'], | ||
package_data={ | ||
'show': ['aliases.ini'] | ||
}, | ||
|
@@ -26,6 +32,7 @@ | |
'scripts/fdbshow', | ||
'scripts/generate_dump', | ||
'scripts/lldpshow', | ||
'scripts/port2alias', | ||
'scripts/portstat', | ||
'scripts/teamshow', | ||
], | ||
|
@@ -60,4 +67,5 @@ | |
'Topic :: Utilities', | ||
], | ||
keywords='sonic SONiC utilities command line cli CLI', | ||
test_suite='setup.get_test_suite' | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
import os | ||
from unittest import TestCase | ||
|
||
import imp | ||
|
||
port2alias = imp.load_source('port2alias', os.path.join(os.path.dirname(__file__), '..', 'scripts', 'port2alias')) | ||
|
||
class TestPort2Alias(TestCase): | ||
def setUp(self): | ||
self.ports = { | ||
"Ethernet1": {"alias" : "fortyG0/1"}, | ||
"Ethernet2": {"alias" : "fortyG0/2"}, | ||
"Ethernet10": {"alias" : "fortyG0/10"}, | ||
"Ethernet_11": {"alias" : "fortyG0/11"}, | ||
} | ||
|
||
def test_translate_line_single_word(self): | ||
self.assertEqual(port2alias.translate_line("1", self.ports),"1") | ||
self.assertEqual(port2alias.translate_line("1\n", self.ports),"1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1", self.ports),"fortyG0/1") | ||
self.assertEqual(port2alias.translate_line("Ethernet1\n", self.ports),"fortyG0/1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet2\n", self.ports),"fortyG0/2\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet10\n", self.ports),"fortyG0/10\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet20\n", self.ports),"Ethernet20\n") | ||
|
||
def test_translate_line_with_symbol(self): | ||
self.assertEqual(port2alias.translate_line("Ethernet_11\n", self.ports),"fortyG0/11\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1_1\n", self.ports),"Ethernet1_1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1-1\n", self.ports),"Ethernet1-1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1/1\n", self.ports),"fortyG0/1/1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1:1\n", self.ports),"fortyG0/1:1\n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1 U\n", self.ports),"fortyG0/1 U\n") | ||
|
||
def test_translate_line_multiple_words(self): | ||
self.assertEqual(port2alias.translate_line(" Ethernet1 Ethernet2 \n", self.ports)," fortyG0/1 fortyG0/2 \n") | ||
self.assertEqual(port2alias.translate_line("Ethernet1,Ethernet1,Ethernet1,Ethernet1\n", self.ports),"fortyG0/1,fortyG0/1,fortyG0/1,fortyG0/1\n") | ||
|
||
def test_translate_line_empty_ports(self): | ||
self.assertEqual(port2alias.translate_line("Ethernet1\n", {}),"Ethernet1\n") | ||
|