From a6efbaed975e1bc6f010002b63a465d969744166 Mon Sep 17 00:00:00 2001 From: jostar-yang Date: Fri, 7 Feb 2020 23:53:55 +0800 Subject: [PATCH] [device]Add check exception case when read port present fail (#4107) Modify get_psu_presence(). Move read() and close() to "try: ... except IOError:" To avoid when read port present sysfs fail. This exception will let sfputil.py to exit. So "sfputil show eeprom" can't show other port status. --- .../plugins/sfputil.py | 11 +++++----- .../plugins/sfputil.py | 22 +++++++++---------- .../plugins/sfputil.py | 20 ++++++++--------- .../plugins/sfputil.py | 20 ++++++++--------- .../plugins/sfputil.py | 14 +++++------- .../plugins/sfputil.py | 14 +++++------- .../plugins/sfputil.py | 22 +++++++++---------- .../plugins/sfputil.py | 9 ++++---- .../plugins/sfputil.py | 9 ++++---- .../plugins/sfputil.py | 11 +++++----- .../plugins/sfputil.py | 8 +++---- .../plugins/sfputil.py | 11 +++++----- .../plugins/sfputil.py | 9 ++++---- .../plugins/sfputil.py | 9 ++++---- .../plugins/sfputil.py | 8 ++++--- .../plugins/sfputil.py | 11 +++++----- .../plugins/sfputil.py | 9 ++++---- .../plugins/sfputil.py | 11 +++++----- .../plugins/sfputil.py | 8 ++++--- .../plugins/sfputil.py | 8 +++---- 20 files changed, 116 insertions(+), 128 deletions(-) diff --git a/device/accton/x86_64-accton_as4630_54pe-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as4630_54pe-r0/plugins/sfputil.py index cb18eb0d54..33a2630f1d 100755 --- a/device/accton/x86_64-accton_as4630_54pe-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as4630_54pe-r0/plugins/sfputil.py @@ -70,16 +70,15 @@ class SfpUtil(SfpUtilBase): present_path = self.BASE_CPLD_PATH + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as5712_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as5712_54x-r0/plugins/sfputil.py index 2ca1b8eec7..b1727f0a79 100755 --- a/device/accton/x86_64-accton_as5712_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as5712_54x-r0/plugins/sfputil.py @@ -144,16 +144,15 @@ class SfpUtil(SfpUtilBase): present_path = present_path + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -175,16 +174,15 @@ class SfpUtil(SfpUtilBase): q = self.qsfp_sb_remap(port_num) lp_mode_path = lp_mode_path + str(q) + content="0" try: val_file = open(lp_mode_path) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as5812_54t-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as5812_54t-r0/plugins/sfputil.py index c9f3008dcc..5647c84a35 100755 --- a/device/accton/x86_64-accton_as5812_54t-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as5812_54t-r0/plugins/sfputil.py @@ -105,16 +105,15 @@ class SfpUtil(SfpUtilBase): present_path = present_path + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -129,16 +128,15 @@ class SfpUtil(SfpUtilBase): lp_mode_path = lp_mode_path + "module_lp_mode_" lp_mode_path = lp_mode_path + str(port_num) + content="0" try: val_file = open(lp_mode_path) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: print "Error: unable to open file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as5812_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as5812_54x-r0/plugins/sfputil.py index 88d8e536c4..62b9db6043 100755 --- a/device/accton/x86_64-accton_as5812_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as5812_54x-r0/plugins/sfputil.py @@ -158,16 +158,15 @@ class SfpUtil(SfpUtilBase): present_path = present_path + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -189,16 +188,15 @@ class SfpUtil(SfpUtilBase): q = self.qsfp_sb_remap(port_num) lp_mode_path = lp_mode_path + str(q) + content = "0" try: val_file = open(lp_mode_path) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: print "Error: unable to open file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as5835_54t-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as5835_54t-r0/plugins/sfputil.py index 4fcbdb0225..34e8f724ae 100644 --- a/device/accton/x86_64-accton_as5835_54t-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as5835_54t-r0/plugins/sfputil.py @@ -100,16 +100,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/3-0062/module_present_{0}" port_ps = path.format(cage_num) + content="0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -131,8 +130,7 @@ class SfpUtil(SfpUtilBase): content = val_file.readline().rstrip() val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as5835_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as5835_54x-r0/plugins/sfputil.py index ba8e6181eb..7782f708fc 100644 --- a/device/accton/x86_64-accton_as5835_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as5835_54x-r0/plugins/sfputil.py @@ -167,16 +167,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}/module_present_{1}" port_ps = path.format(cpld_ps, cage_num) + content = "0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -201,8 +200,7 @@ class SfpUtil(SfpUtilBase): content = val_file.readline().rstrip() val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as6712_32x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as6712_32x-r0/plugins/sfputil.py index 661ecc7224..6889280608 100644 --- a/device/accton/x86_64-accton_as6712_32x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as6712_32x-r0/plugins/sfputil.py @@ -127,16 +127,15 @@ class SfpUtil(SfpUtilBase): self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True @@ -150,16 +149,15 @@ class SfpUtil(SfpUtilBase): _path = cpld_path + "/module_lp_mode_" _path += str(self._port_to_i2c_mapping[port_num][0]) + content="0" try: reg_file = open(_path) + content = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = reg_file.readline().rstrip() - reg_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as7116_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7116_54x-r0/plugins/sfputil.py index 4f0e4ef827..3562043ad9 100644 --- a/device/accton/x86_64-accton_as7116_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7116_54x-r0/plugins/sfputil.py @@ -126,14 +126,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}-0050/sfp_is_present" port_ps = path.format(self._port_to_i2c_mapping[port_num]) - + reg_value = '0' try: reg_file = open(port_ps) + reg_value = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - reg_value = reg_file.readline().rstrip() + if reg_value == '1': return True diff --git a/device/accton/x86_64-accton_as7212_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7212_54x-r0/plugins/sfputil.py index fa706867a5..92ba725072 100755 --- a/device/accton/x86_64-accton_as7212_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7212_54x-r0/plugins/sfputil.py @@ -97,14 +97,15 @@ class sfputil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}-0050/sfp_is_present" port_ps = path.format(self.port_to_i2c_mapping[port_num+1]) - + reg_value = '0' try: reg_file = open(port_ps) + reg_value = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - reg_value = reg_file.readline().rstrip() + if reg_value == '1': return True diff --git a/device/accton/x86_64-accton_as7312_54x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7312_54x-r0/plugins/sfputil.py index a1785fde55..52b57e210e 100644 --- a/device/accton/x86_64-accton_as7312_54x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7312_54x-r0/plugins/sfputil.py @@ -142,16 +142,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}/module_present_{1}" port_ps = path.format(cpld_ps, port_num) + content = "0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as7312_54xs-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7312_54xs-r0/plugins/sfputil.py index a1785fde55..90349ff539 100644 --- a/device/accton/x86_64-accton_as7312_54xs-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7312_54xs-r0/plugins/sfputil.py @@ -142,15 +142,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}/module_present_{1}" port_ps = path.format(cpld_ps, port_num) + content = "0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - content = val_file.readline().rstrip() - val_file.close() - # content is a string, either "0" or "1" if content == "1": return True diff --git a/device/accton/x86_64-accton_as7315_27xb-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7315_27xb-r0/plugins/sfputil.py index 1d777d25ae..dc37f5ea36 100644 --- a/device/accton/x86_64-accton_as7315_27xb-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7315_27xb-r0/plugins/sfputil.py @@ -114,16 +114,15 @@ class SfpUtil(SfpUtilBase): index = ((port_num-1)%24) +1 port_ps = path.format(cpld_ps, index) + content = "0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as7326_56x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7326_56x-r0/plugins/sfputil.py index b88536ecfe..553e48fdd2 100644 --- a/device/accton/x86_64-accton_as7326_56x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7326_56x-r0/plugins/sfputil.py @@ -145,16 +145,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}/module_present_{1}" port_ps = path.format(cpld_ps, port_num) + content="0" try: val_file = open(port_ps) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" if content == "1": return True diff --git a/device/accton/x86_64-accton_as7512_32x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7512_32x-r0/plugins/sfputil.py index d18e9295f4..a4f405fa6a 100644 --- a/device/accton/x86_64-accton_as7512_32x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7512_32x-r0/plugins/sfputil.py @@ -39,14 +39,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/{0}-0050/sfp_is_present" port_ps = path.format(port_num+18) - + reg_value = '0' try: reg_file = open(port_ps) + reg_value = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - reg_value = reg_file.readline().rstrip() + if reg_value == '1': return True diff --git a/device/accton/x86_64-accton_as7712_32x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7712_32x-r0/plugins/sfputil.py index 2b8d61ef42..8cfde932e7 100644 --- a/device/accton/x86_64-accton_as7712_32x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7712_32x-r0/plugins/sfputil.py @@ -96,13 +96,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/4-0060/module_present_{0}" port_ps = path.format(port_num) + reg_value = '0' try: reg_file = open(port_ps) + reg_value = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - reg_value = reg_file.readline().rstrip() + if reg_value == '1': return True diff --git a/device/accton/x86_64-accton_as7716_32x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7716_32x-r0/plugins/sfputil.py index 96637e85a0..b8e4e8d131 100755 --- a/device/accton/x86_64-accton_as7716_32x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7716_32x-r0/plugins/sfputil.py @@ -97,16 +97,15 @@ class SfpUtil(SfpUtilBase): present_path = self.BASE_CPLD_PATH + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content = "0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as7716_32xb-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7716_32xb-r0/plugins/sfputil.py index b582602fa9..8bc5869e6b 100755 --- a/device/accton/x86_64-accton_as7716_32xb-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7716_32xb-r0/plugins/sfputil.py @@ -87,16 +87,15 @@ class SfpUtil(SfpUtilBase): present_path = self.BASE_CPLD_PATH + "module_present_" + str(port_num+1) self.__port_to_is_present = present_path + content = "0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - # content is a string, either "0" or "1" if content == "1": return True diff --git a/device/accton/x86_64-accton_as7726_32x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7726_32x-r0/plugins/sfputil.py index 04a0388daa..f8ef438af9 100755 --- a/device/accton/x86_64-accton_as7726_32x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7726_32x-r0/plugins/sfputil.py @@ -100,16 +100,15 @@ class SfpUtil(SfpUtilBase): present_path = self.BASE_CPLD_PATH + "module_present_" + str(port_num) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - content = val_file.readline().rstrip() - val_file.close() - - # content is a string, either "0" or "1" + if content == "1": return True diff --git a/device/accton/x86_64-accton_as7816_64x-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as7816_64x-r0/plugins/sfputil.py index b5e76ec838..afb6f0c741 100644 --- a/device/accton/x86_64-accton_as7816_64x-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as7816_64x-r0/plugins/sfputil.py @@ -122,13 +122,15 @@ class SfpUtil(SfpUtilBase): path = "/sys/bus/i2c/devices/19-0060/module_present_{0}" port_ps = path.format(port_num) + reg_value = '0' try: reg_file = open(port_ps) + reg_value = reg_file.readline().rstrip() + reg_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - - reg_value = reg_file.readline().rstrip() + if reg_value == '1': return True diff --git a/device/accton/x86_64-accton_as9716_32d-r0/plugins/sfputil.py b/device/accton/x86_64-accton_as9716_32d-r0/plugins/sfputil.py index 1779903596..bc3297471c 100644 --- a/device/accton/x86_64-accton_as9716_32d-r0/plugins/sfputil.py +++ b/device/accton/x86_64-accton_as9716_32d-r0/plugins/sfputil.py @@ -98,15 +98,15 @@ class SfpUtil(SfpUtilBase): present_path = self.BASE_CPLD2_PATH + "module_present_" + str(port_num+1) self.__port_to_is_present = present_path + content="0" try: val_file = open(self.__port_to_is_present) + content = val_file.readline().rstrip() + val_file.close() except IOError as e: - print "Error: unable to open file: %s" % str(e) + print "Error: unable to access file: %s" % str(e) return False - content = val_file.readline().rstrip() - val_file.close() - if content == "1": return True