[DellEMC]: EEPROM platform API Python3 compliance changes (#5960)

Make EEPROM platform APIs Python3 compliant in DellEMC platforms by handling bytearray type returned by read_eeprom and read_eeprom_bytes methods.
This commit is contained in:
Arun Saravanan Balachandran 2020-11-25 01:30:41 +00:00 committed by GitHub
parent 503873056e
commit dc15fbc0ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 90 deletions

View File

@ -11,7 +11,6 @@
try:
import os.path
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
@ -40,7 +39,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not self.is_valid_tlvinfo_header(eeprom):
return
total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length
@ -49,16 +48,16 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
break
tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]
name, value = self.decoder(None, tlv)
self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break
tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2
def serial_number_str(self):
"""
@ -68,7 +67,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def base_mac_addr(self, e):
"""
@ -79,7 +78,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid or t[1] != 6:
return super(eeprom_tlvinfo.TlvInfoDecoder, self).switchaddrstr(t)
return ":".join([binascii.b2a_hex(T) for T in t[2]])
return ":".join(["{:02x}".format(T) for T in t[2]]).upper()
def modelstr(self):
"""
@ -90,7 +89,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def part_number_str(self):
"""
@ -101,7 +100,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def serial_str(self):
"""
@ -112,7 +111,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def revision_str(self):
"""
@ -123,7 +122,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def system_eeprom_info(self):
"""
@ -132,5 +131,3 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
found in the system EEPROM.
"""
return self.eeprom_tlv_dict

View File

@ -18,7 +18,6 @@
try:
import binascii
import os
import redis
import struct
from collections import OrderedDict
@ -108,7 +107,7 @@ class Eeprom(TlvInfoDecoder):
self.serial = 'NA'
return
total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | (eeprom[10])
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length
@ -117,21 +116,21 @@ class Eeprom(TlvInfoDecoder):
break
tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % (tlv[0])
if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)
self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break
tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2
self.base_mac = self.eeprom_tlv_dict.get(
"0x%X" % (self._TLV_CODE_MAC_BASE), 'NA')
@ -191,7 +190,7 @@ class Eeprom(TlvInfoDecoder):
else:
self.fan_type = 'NA'
def _get_eeprom_field(self, field_name):
def _get_eeprom_field(self, field_name, decode=True):
"""
For a field name specified in the EEPROM format, returns the
presence of the field and the value for the same.
@ -200,7 +199,10 @@ class Eeprom(TlvInfoDecoder):
for field in self.format:
field_end = field_start + field[2]
if field[0] == field_name:
return (True, self.eeprom_data[field_start:field_end].decode('utf-8'))
if decode:
return (True, self.eeprom_data[field_start:field_end].decode('ascii'))
else:
return (True, self.eeprom_data[field_start:field_end])
field_start = field_end
return (False, None)
@ -259,7 +261,7 @@ class EepromS6000(EepromDecoder):
_EEPROM_MAX_LEN = 128
_BLK_HDR_LEN = 6
_BLK_HDR_MAGIC = '\x3a\x29'
_BLK_HDR_MAGIC = b'\x3a\x29'
_BLK_HDR_REVID = 1
_BLK_CODE_MFG = 0x20
@ -297,10 +299,10 @@ class EepromS6000(EepromDecoder):
def _is_valid_block(self, e, blk_code):
return (e[:2] == self._BLK_HDR_MAGIC
and struct.unpack('<H', e[2:4])[0] == self._BLK_INFO[blk_code]["size"]
and ord(e[4]) == blk_code
and ord(e[5]) == self._BLK_HDR_REVID)
and e[4] == blk_code
and e[5] == self._BLK_HDR_REVID)
def _get_eeprom_field(self, e, blk_code, field_name):
def _get_eeprom_field(self, e, blk_code, field_name, decode=True):
"""
For a field name specified in the EEPROM format, returns the
presence of the field and the value for the same.
@ -314,7 +316,10 @@ class EepromS6000(EepromDecoder):
for field in self._BLK_INFO[blk_code]["format"]:
field_end = field_start + field[1]
if field[0] == field_name:
return (True, e[field_start:field_end])
if decode:
return (True, e[field_start:field_end].decode('ascii'))
else:
return (True, e[field_start:field_end])
field_start = field_end
return (False, None)
@ -339,9 +344,9 @@ class EepromS6000(EepromDecoder):
elif f[0] == "Card ID":
data = hex(struct.unpack('<I', e[offset:offset+f[1]])[0])
elif f[0] == "Base MAC address":
data = ":".join([binascii.b2a_hex(T) for T in e[offset:offset+f[1]]]).upper()
data = ":".join(["{:02x}".format(T) for T in e[offset:offset+f[1]]]).upper()
else:
data = e[offset:offset+f[1]]
data = e[offset:offset+f[1]].decode('ascii')
print("{:<20s} {:>3d} {:<s}".format(f[0], f[1], data))
offset += f[1]
@ -406,9 +411,9 @@ class EepromS6000(EepromDecoder):
elif f[0] == "Card ID":
data = hex(struct.unpack('<I', e[offset:offset+f[1]])[0])
elif f[0] == "Base MAC address":
data = ":".join([binascii.b2a_hex(T) for T in e[offset:offset+f[1]]]).upper()
data = ":".join(["{:02x}".format(T) for T in e[offset:offset+f[1]]]).upper()
else:
data = e[offset:offset+f[1]]
data = e[offset:offset+f[1]].decode('ascii')
client.hset('EEPROM_INFO|{}'.format(f[0]), 'Value', data)
offset += f[1]
@ -424,10 +429,10 @@ class EepromS6000(EepromDecoder):
"""
Returns the base MAC address found in the system EEPROM.
"""
(valid, data) = self._get_eeprom_field(self.eeprom_data,
self._BLK_CODE_MAC, "Base MAC address")
(valid, data) = self._get_eeprom_field(self.eeprom_data, self._BLK_CODE_MAC,
"Base MAC address", False)
if valid:
return ":".join([binascii.b2a_hex(T) for T in data]).upper()
return ":".join(["{:02x}".format(T) for T in data]).upper()
else:
return 'NA'
@ -479,9 +484,9 @@ class EepromS6000(EepromDecoder):
"""
Returns the base MAC address.
"""
(valid, data) = self._get_eeprom_field(e, self._BLK_CODE_MAC, "Base MAC address")
(valid, data) = self._get_eeprom_field(e, self._BLK_CODE_MAC, "Base MAC address", False)
if valid:
return ":".join([binascii.b2a_hex(T) for T in data]).upper()
return ":".join(["{:02x}".format(T) for T in data]).upper()
else:
return 'NA'

View File

@ -11,7 +11,6 @@
try:
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
@ -30,7 +29,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
try:
if self.is_module:
self.write_eeprom("\x00\x00")
self.write_eeprom(b"\x00\x00")
self.eeprom_data = self.read_eeprom_bytes(256)
else:
self.eeprom_data = self.read_eeprom()
@ -48,7 +47,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not self.is_valid_tlvinfo_header(eeprom):
return
total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length
@ -57,22 +56,21 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
break
tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]
if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)
self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break
tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2
def serial_number_str(self):
(is_valid, results) = self.get_tlv_field(
@ -80,7 +78,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def base_mac_addr(self):
(is_valid, results) = self.get_tlv_field(
@ -88,7 +86,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid or results[1] != 6:
return super(TlvInfoDecoder, self).switchaddrstr(e)
return ":".join([binascii.b2a_hex(T) for T in results[2]])
return ":".join(["{:02x}".format(T) for T in results[2]]).upper()
def modelstr(self):
if self.is_module:
@ -100,7 +98,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def part_number_str(self):
(is_valid, results) = self.get_tlv_field(
@ -108,7 +106,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def serial_str(self):
(is_valid, results) = self.get_tlv_field(
@ -116,7 +114,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def revision_str(self):
(is_valid, results) = self.get_tlv_field(
@ -124,7 +122,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def system_eeprom_info(self):
"""

View File

@ -11,7 +11,6 @@
try:
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
@ -33,7 +32,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not self.is_valid_tlvinfo_header(eeprom):
return
total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length
@ -42,28 +41,29 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
break
tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]
if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)
self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break
tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2
def serial_number_str(self):
(is_valid, results) = self.get_tlv_field(
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def base_mac_addr(self):
(is_valid, t) = self.get_tlv_field(
@ -71,7 +71,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid or t[1] != 6:
return super(TlvInfoDecoder, self).switchaddrstr(e)
return ":".join([binascii.b2a_hex(T) for T in t[2]])
return ":".join(["{:02x}".format(T) for T in t[2]]).upper()
def modelstr(self):
(is_valid, results) = self.get_tlv_field(
@ -79,7 +79,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def part_number_str(self):
(is_valid, results) = self.get_tlv_field(
@ -87,7 +87,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def serial_str(self):
(is_valid, results) = self.get_tlv_field(
@ -95,7 +95,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def revision_str(self):
(is_valid, results) = self.get_tlv_field(
@ -103,7 +103,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def system_eeprom_info(self):
"""

View File

@ -12,7 +12,6 @@
try:
import os.path
from sonic_eeprom import eeprom_tlvinfo
import binascii
except ImportError as e:
raise ImportError(str(e) + "- required module not found")
@ -41,7 +40,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not self.is_valid_tlvinfo_header(eeprom):
return
total_length = (ord(eeprom[9]) << 8) | ord(eeprom[10])
total_length = (eeprom[9] << 8) | eeprom[10]
tlv_index = self._TLV_INFO_HDR_LEN
tlv_end = self._TLV_INFO_HDR_LEN + total_length
@ -50,21 +49,21 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
break
tlv = eeprom[tlv_index:tlv_index + 2
+ ord(eeprom[tlv_index + 1])]
code = "0x%02X" % (ord(tlv[0]))
+ eeprom[tlv_index + 1]]
code = "0x%02X" % tlv[0]
if ord(tlv[0]) == self._TLV_CODE_VENDOR_EXT:
value = str((ord(tlv[2]) << 24) | (ord(tlv[3]) << 16) |
(ord(tlv[4]) << 8) | ord(tlv[5]))
value += str(tlv[6:6 + ord(tlv[1])])
if tlv[0] == self._TLV_CODE_VENDOR_EXT:
value = str((tlv[2] << 24) | (tlv[3] << 16) |
(tlv[4] << 8) | tlv[5])
value += tlv[6:6 + tlv[1]].decode('ascii')
else:
name, value = self.decoder(None, tlv)
self.eeprom_tlv_dict[code] = value
if ord(eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
if eeprom[tlv_index] == self._TLV_CODE_CRC_32:
break
tlv_index += ord(eeprom[tlv_index+1]) + 2
tlv_index += eeprom[tlv_index+1] + 2
def serial_number_str(self):
"""
@ -74,7 +73,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
self.eeprom_data, self._TLV_CODE_SERIAL_NUMBER)
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def base_mac_addr(self):
"""
@ -85,7 +84,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid or t[1] != 6:
return super(TlvInfoDecoder, self).switchaddrstr(e)
return ":".join([binascii.b2a_hex(T) for T in t[2]])
return ":".join(["{:02x}".format(T) for T in t[2]]).upper()
def modelstr(self):
"""
@ -96,7 +95,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def part_number_str(self):
"""
@ -107,7 +106,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def serial_str(self):
"""
@ -118,7 +117,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def revision_str(self):
"""
@ -129,7 +128,7 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
if not is_valid:
return "N/A"
return results[2]
return results[2].decode('ascii')
def system_eeprom_info(self):
"""