diff --git a/device/quanta/x86_64-quanta_ix1b_rglbmc-r0/plugins/psuutil.py b/device/quanta/x86_64-quanta_ix1b_rglbmc-r0/plugins/psuutil.py index 0b2027afdd..2cdcf10f8d 100755 --- a/device/quanta/x86_64-quanta_ix1b_rglbmc-r0/plugins/psuutil.py +++ b/device/quanta/x86_64-quanta_ix1b_rglbmc-r0/plugins/psuutil.py @@ -7,6 +7,7 @@ import os.path import subprocess import logging +from sonic_py_common.general import check_output_pipe try: from sonic_psu.psu_base import PsuBase @@ -22,11 +23,13 @@ def show_log(txt): return -def exec_cmd(cmd, show): +def exec_cmd(cmd_args, out_file, show): + cmd = ' '.join(cmd_args) + ' > ' + out_file logging.info('Run :'+cmd) try: - output = subprocess.check_output(cmd, shell=True, universal_newlines=True) - show_log(cmd + "output:"+str(output)) + with open(out_file, 'w') as f: + output = subprocess.check_output(cmd_args, stdout=f, universal_newlines=True) + show_log(cmd + "output:"+str(output)) except subprocess.CalledProcessError as e: logging.info("Failed :"+cmd) if show: @@ -40,12 +43,13 @@ def my_log(txt): return -def log_os_system(cmd, show): +def log_os_system(cmd1_args, cmd2_args, show): + cmd = ' '.join(cmd1_args) + ' | ' + ' '.join(cmd2_args) logging.info('Run :'+cmd) status = 1 output = "" try: - output = subprocess.check_output(cmd, shell=True, universal_newlines=True) + output = check_output_pipe(cmd1_args, cmd2_args) my_log(cmd + "output:"+str(output)) except subprocess.CalledProcessError as e: logging.info('Failed :'+cmd) @@ -55,28 +59,28 @@ def log_os_system(cmd, show): def gpio16_exist(): - ls = log_os_system("ls /sys/class/gpio/ | grep gpio16", 0) + ls = log_os_system(["ls", "/sys/class/gpio/"], ["grep", "gpio16"], 0) logging.info('mods:'+ls) if len(ls) == 0: return False def gpio17_exist(): - ls = log_os_system("ls /sys/class/gpio/ | grep gpio17", 0) + ls = log_os_system(["ls", "/sys/class/gpio/"], ["grep", "gpio17"], 0) logging.info('mods:'+ls) if len(ls) == 0: return False def gpio19_exist(): - ls = log_os_system("ls /sys/class/gpio/ | grep gpio19", 0) + ls = log_os_system(["ls", "/sys/class/gpio/"], ["grep", "gpio19"], 0) logging.info('mods:'+ls) if len(ls) == 0: return False def gpio20_exist(): - ls = log_os_system("ls /sys/class/gpio/ | grep gpio20", 0) + ls = log_os_system(["ls", "/sys/class/gpio/"], ["grep", "gpio20"], 0) logging.info('mods:'+ls) if len(ls) == 0: return False @@ -95,20 +99,20 @@ class PsuUtil(PsuBase): PsuBase.__init__(self) if gpio16_exist() == False: - output = exec_cmd("echo 16 > /sys/class/gpio/export ", 1) - output = exec_cmd("echo in > /sys/class/gpio/gpio16/direction ", 1) + output = exec_cmd(["echo", "16"], "/sys/class/gpio/export", 1) + output = exec_cmd(["echo", "in"], "/sys/class/gpio/gpio16/direction", 1) if gpio17_exist() == False: - output = exec_cmd("echo 17 > /sys/class/gpio/export ", 1) - output = exec_cmd("echo in > /sys/class/gpio/gpio17/direction ", 1) + output = exec_cmd(["echo", "17"], "/sys/class/gpio/export", 1) + output = exec_cmd(["echo", "in"], "/sys/class/gpio/gpio17/direction", 1) if gpio19_exist() == False: - output = exec_cmd("echo 19 > /sys/class/gpio/export ", 1) - output = exec_cmd("echo in > /sys/class/gpio/gpio19/direction ", 1) + output = exec_cmd(["echo", "19"], "/sys/class/gpio/export", 1) + output = exec_cmd(["echo", "in"], "/sys/class/gpio/gpio19/direction", 1) if gpio20_exist() == False: - output = exec_cmd("echo 20 > /sys/class/gpio/export ", 1) - output = exec_cmd("echo in > /sys/class/gpio/gpio20/direction ", 1) + output = exec_cmd(["echo", "20"], "/sys/class/gpio/export", 1) + output = exec_cmd(["echo", "in"], "/sys/class/gpio/gpio20/direction", 1) # Get sysfs attribute def get_attr_value(self, attr_path): diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix1b-32x/utils/quanta_ix1b_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix1b-32x/utils/quanta_ix1b_util.py index 2135932938..b92c5cf828 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix1b-32x/utils/quanta_ix1b_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix1b-32x/utils/quanta_ix1b_util.py @@ -27,13 +27,10 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import commands import sys, getopt import logging -import re import time -from collections import namedtuple DEBUG = False args = [] @@ -41,8 +38,8 @@ FORCE = 0 i2c_prefix = '/sys/bus/i2c/devices/' if DEBUG == True: - print sys.argv[0] - print 'ARGV :', sys.argv[1:] + print(sys.argv[0]) + print('ARGV :', sys.argv[1:]) def main(): global DEBUG @@ -56,10 +53,10 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: - print options - print args - print len(sys.argv) + if DEBUG is True: + print(options) + print(args) + print(len(sys.argv)) for opt, arg in options: if opt in ('-h', '--help'): @@ -83,12 +80,12 @@ def main(): return 0 def show_help(): - print __doc__ % {'scriptName' : sys.argv[0].split("/")[-1]} + print(__doc__ % {'scriptName' : sys.argv[0].split("/")[-1]}) sys.exit(0) def show_log(txt): - if DEBUG == True: - print "[IX1B-32X]" + txt + if DEBUG is True: + print("[IX1B-32X]" + txt) return @@ -165,7 +162,7 @@ def system_install(): status, output = exec_cmd("modprobe " + drivers[i], 1) if status: - print output + print(output) if FORCE == 0: return status @@ -174,7 +171,7 @@ def system_install(): status, output = exec_cmd(instantiate[i], 1) if status: - print output + print(output) if FORCE == 0: return status @@ -184,7 +181,9 @@ def system_install(): #QSFP for 1~32 port for port_number in range(1, 33): bus_number = port_number + 31 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') #Set system LED to green status, output = exec_cmd("echo 1 > /sys/class/leds/sysled_green/brightness", 1) @@ -199,14 +198,14 @@ def system_ready(): def install(): if not device_found(): - print "No device, installing...." + print("No device, installing....") status = system_install() if status: if FORCE == 0: return status else: - print " ix1b driver already installed...." + print(" ix1b driver already installed....") return @@ -215,10 +214,10 @@ def uninstall(): #uninstall drivers for i in range(len(drivers) - 1, -1, -1): - status, output = exec_cmd("rmmod " + drivers[i], 1) + status, output = exec_cmd("rmmod " + drivers[i], 1) if status: - print output + print(output) if FORCE == 0: return status diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/component.py index 9f5f69f422..bbaf23533f 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -159,7 +173,7 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component """ - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) if not res: return 'ERR' else: @@ -179,7 +193,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -194,7 +209,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/sfp.py index c833647305..a2da83d290 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/sonic_platform/sfp.py @@ -8,17 +8,17 @@ # ############################################################################# -import os import time +import subprocess from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -163,7 +163,7 @@ class Sfp(SfpBase): # Path to QSFP sysfs PLATFORM_ROOT_PATH = "/usr/share/sonic/device" PMON_HWSKU_PATH = "/usr/share/sonic/hwsku" - HOST_CHK_CMD = "docker > /dev/null 2>&1" + HOST_CHK_CMD = ["docker"] PLATFORM = "x86_64-quanta_ix7_rglbmc-r0" HWSKU = "Quanta-IX7-32X" @@ -259,7 +259,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/utils/quanta_ix7_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/utils/quanta_ix7_util.py index 4196e24f46..9b679cacd9 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/utils/quanta_ix7_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-32x/utils/quanta_ix7_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX7-32X]" + txt) return @@ -204,7 +203,9 @@ def system_install(): #QSFP for 1~32 port for port_number in range(1, 33): bus_number = port_number + 16 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') return diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/component.py index ae0c252289..e5228829f5 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -158,9 +172,9 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component """ - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x01") - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x00") + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x01"]) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x00"]) if not res: return 'ERR' else: @@ -180,7 +194,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -195,7 +210,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/sfp.py index 1faf552c7b..942074bb72 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/sonic_platform/sfp.py @@ -8,17 +8,17 @@ # ############################################################################# -import os import time +import subprocess from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -163,7 +163,7 @@ class Sfp(SfpBase): # Path to QSFP sysfs PLATFORM_ROOT_PATH = "/usr/share/sonic/device" PMON_HWSKU_PATH = "/usr/share/sonic/hwsku" - HOST_CHK_CMD = "docker > /dev/null 2>&1" + HOST_CHK_CMD = ["docker"] PLATFORM = "x86_64-quanta_ix7_bwde-r0" HWSKU = "Quanta-IX7-BWDE-32X" @@ -259,7 +259,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/utils/quanta_ix7_bwde_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/utils/quanta_ix7_bwde_util.py index a20aa33828..8a3c896de5 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/utils/quanta_ix7_bwde_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix7-bwde-32x/utils/quanta_ix7_bwde_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX7-BWDE-32X]"+txt) return @@ -203,7 +202,9 @@ def system_install(): #QSFP for 1~32 port for port_number in range(1, 33): bus_number = port_number + 12 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') status, output = exec_cmd("pip3 install /usr/share/sonic/device/x86_64-quanta_ix7_bwde-r0/sonic_platform-1.0-py3-none-any.whl",1) if status: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/component.py index 95f275014f..03b2b1c174 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -160,7 +174,7 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component """ - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) if not res: return 'ERR' else: @@ -180,7 +194,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -195,7 +210,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/sfp.py index f7a9a105a5..45b14d6427 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/sonic_platform/sfp.py @@ -8,17 +8,17 @@ # ############################################################################# -import os import time +import subprocess from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -163,7 +163,7 @@ class Sfp(SfpBase): # Path to QSFP sysfs PLATFORM_ROOT_PATH = "/usr/share/sonic/device" PMON_HWSKU_PATH = "/usr/share/sonic/hwsku" - HOST_CHK_CMD = "docker > /dev/null 2>&1" + HOST_CHK_CMD = ["docker"] PLATFORM = "x86_64-quanta_ix8_rglbmc-r0" HWSKU = "Quanta-IX8-56X" @@ -281,7 +281,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/utils/quanta_ix8_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/utils/quanta_ix8_util.py index 833bfe27c4..7875eec59a 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/utils/quanta_ix8_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8-56x/utils/quanta_ix8_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX8-56X]" + txt) return @@ -301,7 +300,9 @@ def system_install(): #QSFP for 1~56 port for port_number in range(1, 57): bus_number = port_number + 16 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') status, output = exec_cmd("pip3 install /usr/share/sonic/device/x86_64-quanta_ix8_rglbmc-r0/sonic_platform-1.0-py3-none-any.whl",1) if status: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/component.py index f697f9bbe5..414512fbf1 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -159,9 +173,9 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component """ - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x01") - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x00") + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x01"]) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x00"]) if not res: return 'ERR' else: @@ -181,7 +195,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -196,7 +211,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/sfp.py index abbebb0343..7d76740ca7 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/sonic_platform/sfp.py @@ -8,17 +8,17 @@ # ############################################################################# -import os import time +import subprocess from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -163,7 +163,7 @@ class Sfp(SfpBase): # Path to QSFP sysfs PLATFORM_ROOT_PATH = "/usr/share/sonic/device" PMON_HWSKU_PATH = "/usr/share/sonic/hwsku" - HOST_CHK_CMD = "docker > /dev/null 2>&1" + HOST_CHK_CMD = ["docker"] PLATFORM = "x86_64-quanta_ix8a_bwde-r0" HWSKU = "Quanta-IX8A-BWDE-56X" @@ -281,7 +281,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/utils/quanta_ix8a_bwde_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/utils/quanta_ix8a_bwde_util.py index c238247793..ff6141a511 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/utils/quanta_ix8a_bwde_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8a-bwde-56x/utils/quanta_ix8a_bwde_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX8A-BWDE-56X]" + txt) return @@ -301,7 +300,9 @@ def system_install(): #QSFP for 1~56 port for port_number in range(1, 57): bus_number = port_number + 12 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') #Enable front-ports LED decoding exec_cmd('echo 1 > /sys/class/cpld-led/CPLDLED-1/led_decode', 1) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/component.py index 75066275d2..15076314b3 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -158,10 +172,10 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component - """ - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x01") - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x00") + """ + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x01"]) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x00"]) if not res: return 'ERR' else: @@ -181,7 +195,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -196,7 +211,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/sfp.py index 305d78aba4..a9baef8eab 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/sonic_platform/sfp.py @@ -8,17 +8,17 @@ # ############################################################################# -import os +import subprocess import time from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -281,7 +281,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/utils/quanta_ix8c_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/utils/quanta_ix8c_util.py index a7e50d908f..ffa80e613a 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/utils/quanta_ix8c_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix8c-56x/utils/quanta_ix8c_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX8C-56X]" + txt) return @@ -297,7 +296,9 @@ def system_install(): #QSFP for 1~56 port for port_number in range(1, 57): bus_number = port_number + 12 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') return diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/component.py b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/component.py index 49d25f31cb..c1b94b62d3 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/component.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/component.py @@ -14,6 +14,7 @@ try: import subprocess from sonic_platform_base.component_base import ComponentBase from collections import namedtuple + from sonic_py_common.general import getstatusoutput_noshell_pipe except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -47,7 +48,7 @@ class Component(ComponentBase): try: proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, - shell=True, stderr=subprocess.STDOUT, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -60,12 +61,24 @@ class Component(ComponentBase): return result + @staticmethod + def _get_command_result_pipe(cmd1, cmd2): + try: + rc, result = getstatusoutput_noshell_pipe(cmd1, cmd2) + if rc != [0, 0]: + raise RuntimeError("Failed to execute command {} {}, return code {}, {}".format(cmd1, cmd2, rc, result)) + + except OSError as e: + raise RuntimeError("Failed to execute command {} {} due to {}".format(cmd1, cmd2, repr(e))) + + return result + class ComponentBIOS(Component): COMPONENT_NAME = 'BIOS' COMPONENT_DESCRIPTION = 'BIOS - Basic Input/Output System' - BIOS_QUERY_VERSION_COMMAND = "dmidecode -s bios-version" + BIOS_QUERY_VERSION_COMMAND = ["dmidecode", "-s", "bios-version"] def __init__(self): super(ComponentBIOS, self).__init__() @@ -90,7 +103,8 @@ class ComponentBIOS(Component): class ComponentBMC(Component): COMPONENT_NAME = 'BMC' COMPONENT_DESCRIPTION = 'BMC - Board Management Controller' - BMC_QUERY_VERSION_COMMAND = "ipmitool mc info | grep 'Firmware Revision'" + BMC_QUERY_VERSION_COMMAND1 = ["ipmitool", "mc", "info"] + BMC_QUERY_VERSION_COMMAND2 = ["grep", 'Firmware Revision'] def __init__(self): super(ComponentBMC, self).__init__() @@ -105,7 +119,7 @@ class ComponentBMC(Component): Returns: A string containing the firmware version of the component """ - bmc_ver = self._get_command_result(self.BMC_QUERY_VERSION_COMMAND) + bmc_ver = self._get_command_result_pipe(self.BMC_QUERY_VERSION_COMMAND1, self.BMC_QUERY_VERSION_COMMAND2) if not bmc_ver: return 'ERR' else: @@ -158,9 +172,9 @@ class ComponentCPLD(Component): Returns: A string containing the firmware version of the component """ - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x01") - res = self._get_command_result("ipmitool raw 0x32 0xff 0x02 {}".format(self.cplds[self.index].cmd_index)) - self._get_command_result("ipmitool raw 0x30 0xe0 0xd0 0x01 0x00") + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x01"]) + res = self._get_command_result(["ipmitool", "raw", "0x32", "0xff", "0x02", str(self.cplds[self.index].cmd_index)]) + self._get_command_result(["ipmitool", "raw", "0x30", "0xe0", "0xd0", "0x01", "0x00"]) if not res: return 'ERR' else: @@ -180,7 +194,8 @@ class ComponentCPLD(Component): class ComponentPCIE(Component): COMPONENT_NAME = 'PCIe' COMPONENT_DESCRIPTION = 'ASIC PCIe Firmware' - PCIE_QUERY_VERSION_COMMAND = "bcmcmd 'pciephy fw version' | grep 'FW version'" + PCIE_QUERY_VERSION_COMMAND1 = ["bcmcmd", 'pciephy fw version'] + PCIE_QUERY_VERSION_COMMAND2 = ["grep", 'FW version'] def __init__(self): super(ComponentPCIE, self).__init__() @@ -195,7 +210,7 @@ class ComponentPCIE(Component): Returns: A string containing the firmware version of the component """ - version = self._get_command_result(self.PCIE_QUERY_VERSION_COMMAND) + version = self._get_command_result_pipe(self.PCIE_QUERY_VERSION_COMMAND1, self.PCIE_QUERY_VERSION_COMMAND2) if not version: return 'ERR' else: diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/sfp.py b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/sfp.py index 77176ad083..30997115cb 100644 --- a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/sfp.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/sonic_platform/sfp.py @@ -8,21 +8,21 @@ # ############################################################################# -import os import time +import subprocess from ctypes import create_string_buffer try: - from sonic_platform_base.sfp_base import SfpBase - from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId - from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom - from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId - from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom - from sonic_platform_base.sonic_sfp.inf8628 import inf8628InterfaceId - from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_InterfaceId - from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_Dom - from sonic_py_common.logger import Logger - from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper + from sonic_platform_base.sfp_base import SfpBase + from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId + from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom + from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId + from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom + from sonic_platform_base.sonic_sfp.inf8628 import inf8628InterfaceId + from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_InterfaceId + from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_Dom + from sonic_py_common.logger import Logger + from sonic_platform_base.sonic_sfp.sfputilhelper import SfpUtilHelper except ImportError as e: raise ImportError (str(e) + "- required module not found") @@ -354,7 +354,7 @@ class Sfp(SfpBase): return 'N/A' def __is_host(self): - return os.system(self.HOST_CHK_CMD) == 0 + return subprocess.call(self.HOST_CHK_CMD) == 0 def __get_path_to_port_config_file(self): platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM]) diff --git a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/utils/quanta_ix9_util.py b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/utils/quanta_ix9_util.py index ec14e10ad6..fa6466da3d 100755 --- a/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/utils/quanta_ix9_util.py +++ b/platform/broadcom/sonic-platform-modules-quanta/ix9-32x/utils/quanta_ix9_util.py @@ -27,7 +27,6 @@ command: clean : uninstall drivers and remove related sysfs nodes """ -import os import subprocess import sys, getopt import logging @@ -54,7 +53,7 @@ def main(): 'debug', 'force', ]) - if DEBUG == True: + if DEBUG is True: print(options) print(args) print(len(sys.argv)) @@ -84,7 +83,7 @@ def show_help(): sys.exit(0) def show_log(txt): - if DEBUG == True: + if DEBUG is True: print("[IX9-32X]" + txt) return @@ -237,7 +236,9 @@ def system_install(): #QSFPDD for 1~32 port for port_number in range(1, 33): bus_number = port_number + 12 - os.system("echo %d >/sys/bus/i2c/devices/%d-0050/port_name" % (port_number, bus_number)) + file = "/sys/bus/i2c/devices/%d-0050/port_name" % bus_number + with open(file, 'w') as f: + f.write(str(port_number) + '\n') return