From e18a0c7461858619f2c04ed76f9253c7d17454d5 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Fri, 4 Sep 2020 00:40:58 +0000 Subject: [PATCH] [sonic-py-common] fix the build/add more logic Signed-off-by: vaibhav-dahiya --- src/sonic-config-engine/portconfig.py | 4 +- src/sonic-config-engine/sonic-cfggen | 2 +- .../sonic_py_common/device_info.py | 54 ++++++++++++++++++- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index 7206f1f84835..6e867955a2c7 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -93,7 +93,7 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_conf return (ports, port_alias_map, port_alias_asic_map) if not port_config_file: - port_config_file = device_info.get_path_to_port_config_file(asic) + port_config_file = device_info.get_path_to_port_config_file(asic, hwsku) if not port_config_file: return ({}, {}, {}) @@ -263,7 +263,7 @@ def parse_platform_json_file(hwsku_json_file, platform_json_file): def get_breakout_mode(hwsku=None, platform=None, port_config_file=None): if not port_config_file: - port_config_file = device_info.get_path_to_port_config_file() + port_config_file = device_info.get_path_to_port_config_file(hwsku) if not port_config_file: return None if port_config_file.endswith('.json'): diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 0aa95d3cefb5..3e4e661f6adc 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -301,7 +301,7 @@ def main(): }}} deep_update(data, hardware_data) if args.port_config is None: - args.port_config = device_info.get_path_to_port_config_file() + args.port_config = device_info.get_path_to_port_config_file(hwsku) (ports, _, _) = get_port_config(hwsku, platform, args.port_config, asic_id) if not ports: print('Failed to get port config', file=sys.stderr) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 221faf153729..bb4bccd6db79 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -154,6 +154,52 @@ def get_asic_conf_file_path(): return None +def get_path_to_platform_dir(): + """ + Retreives the paths to the device's platform directory + + Returns: + A string containing the path to the platform directory of the device + """ + # Get platform + platform = get_platform() + + # Determine whether we're running in a container or on the host + platform_path_host = os.path.join(HOST_DEVICE_PATH, platform) + + if os.path.isdir(CONTAINER_PLATFORM_PATH): + platform_path = CONTAINER_PLATFORM_PATH + elif os.path.isdir(platform_path_host): + platform_path = platform_path_host + else: + raise OSError("Failed to locate platform directory") + + return platform_path + +def get_path_to_hwsku_dir(): + """ + Retreives the path to the device's hardware SKU data directory + + Returns: + A string, containing the path to the hardware SKU directory of the device + """ + # Get hwsku + hwsku = get_hwsku() + + # Determine whether we're running in a container or on the host + platform_path_host = os.path.join(HOST_DEVICE_PATH, platform) + + if os.path.isdir(CONTAINER_PLATFORM_PATH): + platform_path = CONTAINER_PLATFORM_PATH + elif os.path.isdir(platform_path_host): + platform_path = platform_path_host + else: + raise OSError("Failed to locate platform directory") + + hwsku_path = os.path.join(platform_path, hwsku) + + return hwsku_path + def get_paths_to_platform_and_hwsku_dirs(): """ Retreives the paths to the device's platform and hardware SKU data @@ -181,7 +227,7 @@ def get_paths_to_platform_and_hwsku_dirs(): return (platform_path, hwsku_path) -def get_path_to_port_config_file(asic=None): +def get_path_to_port_config_file(asic=None, hwsku=None): """ Retrieves the path to the device's port configuration file @@ -200,7 +246,11 @@ def get_path_to_port_config_file(asic=None): if not platform: return None - (platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs() + if hwsku: + platform_path = get_path_to_platform_dir() + hwsku_path = hwsku + else: + (platform_path, hwsku_path) = get_paths_to_platform_and_hwsku_dirs() port_config_candidates = []