forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (sonic-net#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
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") | ||
|