diff --git a/config/main.py b/config/main.py index 22b28fa06e3a..966831001979 100755 --- a/config/main.py +++ b/config/main.py @@ -16,7 +16,7 @@ import aaa import mlnx -SONIC_CFGGEN_PATH = "sonic-cfggen" +SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' # # Helper functions @@ -42,13 +42,14 @@ def run_command(command, display_cmd=False, ignore_error=False): def interface_alias_to_name(interface_alias): """Return default interface name if alias name is given as argument """ - - cmd = 'sonic-cfggen -d --var-json "PORT"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - - port_dict = json.loads(p.stdout.read()) + config_db = ConfigDBConnector() + config_db.connect() + port_dict = config_db.get_table('PORT') if interface_alias is not None: + if not port_dict: + click.echo("port_dict is None!") + raise click.Abort() for port_name in natsorted(port_dict.keys()): if interface_alias == port_dict[port_name]['alias']: return port_name @@ -60,13 +61,14 @@ def interface_alias_to_name(interface_alias): def interface_name_to_alias(interface_name): """Return alias interface name if default name is given as argument """ - - cmd = 'sonic-cfggen -d --var-json "PORT"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - - port_dict = json.loads(p.stdout.read()) + config_db = ConfigDBConnector() + config_db.connect() + port_dict = config_db.get_table('PORT') if interface_name is not None: + if not port_dict: + click.echo("port_dict is None!") + raise click.Abort() for port_name in natsorted(port_dict.keys()): if interface_name == port_name: return port_dict[port_name]['alias'] diff --git a/show/main.py b/show/main.py index 6f62492a0ee7..92065c6b2668 100755 --- a/show/main.py +++ b/show/main.py @@ -18,6 +18,8 @@ import mlnx +SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' + try: # noinspection PyPep8Naming import ConfigParser as configparser @@ -43,15 +45,19 @@ def read_config(self, filename): except configparser.NoSectionError: pass - class InterfaceAliasConverter(object): """Class which handles conversion between interface name and alias""" def __init__(self): self.alias_max_length = 0 - cmd = 'sonic-cfggen -d --var-json "PORT"' - p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - self.port_dict = json.loads(p.stdout.read()) + + config_db = ConfigDBConnector() + config_db.connect() + self.port_dict = config_db.get_table('PORT') + + if not self.port_dict: + click.echo("port_dict is None!") + raise click.Abort() for port_name in self.port_dict.keys(): if self.alias_max_length < len(