[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.
This commit is contained in:
jostar-yang 2020-02-07 23:53:55 +08:00 committed by GitHub
parent c63b5fc719
commit a6efbaed97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 116 additions and 128 deletions

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# 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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# 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

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# 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

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True
@ -132,7 +131,6 @@ class SfpUtil(SfpUtilBase):
content = val_file.readline().rstrip()
val_file.close()
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True
@ -202,7 +201,6 @@ class SfpUtil(SfpUtilBase):
content = val_file.readline().rstrip()
val_file.close()
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# 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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = reg_file.readline().rstrip()
reg_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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

View File

@ -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

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -142,14 +142,14 @@ 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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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

View File

@ -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

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
# content is a string, either "0" or "1"
if content == "1":
return True

View File

@ -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

View File

@ -98,14 +98,14 @@ 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)
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return False
content = val_file.readline().rstrip()
val_file.close()
except IOError as e:
print "Error: unable to access file: %s" % str(e)
return False
if content == "1":
return True