2
0
mirror of https://opendev.org/x/pyghmi synced 2025-01-27 19:37:44 +00:00

Merge "Further Python2/3 compatibility"

This commit is contained in:
Zuul 2019-10-03 19:01:57 +00:00 committed by Gerrit Code Review
commit 79973a263b
6 changed files with 55 additions and 53 deletions

View File

@ -753,7 +753,7 @@ class Command(object):
fetchcmd = bytearray((channel, param, 0, 0))
fetched = self.xraw_command(0xc, 2, data=fetchcmd)
fetchdata = fetched['data']
if ord(fetchdata[0]) != 17:
if bytearray(fetchdata)[0] != 17:
return None
if len(fetchdata) == 5: # IPv4 address
if prefixlen:
@ -770,7 +770,7 @@ class Command(object):
return None
return mac
elif len(fetchdata) == 2:
return ord(fetchdata[1])
return bytearray(fetchdata)[1]
else:
raise Exception("Unrecognized data format " + repr(fetchdata))
@ -989,7 +989,8 @@ class Command(object):
continue
else:
raise
chantype = ord(rsp['data'][1]) & 0b1111111
chantype = bytearray(rsp['data'])[1]
chantype = chantype & 0b1111111
if chantype in (4, 6):
try:
# Some implementations denote an inactive channel
@ -1003,7 +1004,8 @@ class Command(object):
# However some implementations may still have
# ambiguous channel info, that will need to be
# picked up on an OEM extension...
self._netchannel = ord(rsp['data'][0]) & 0b1111
netchan = bytearray(rsp['data'])[0]
self._netchannel = netchan & 0b1111
break
except exc.IpmiException as ie:
# This means the attempt to fetch parameter 5 failed,

View File

@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
import struct
try:
range = xrange
except NameError:
pass
class BitArray:
def __init__(self, data):
self._Data = data
self._Data = bytearray(data)
self._DataBitsLeft = len(data) * 8
@ -20,7 +24,7 @@ class BitArray:
result = 0
while bitsleftcount:
curbitsleftcount = 8 - self._BitIdx
curdata = ord(self._Data[self._ByteIdx]) & self.mask(
curdata = self._Data[self._ByteIdx] & self.mask(
curbitsleftcount)
if curbitsleftcount >= bitsleftcount:
@ -68,7 +72,7 @@ def loadhuffmansyms(bits, symscountbits, zeroskipidx):
# Allocate huffman codes to the length-ordered symbols
huffsyms[0][2] = 0
for idx in xrange(1, len(huffsyms)):
for idx in range(1, len(huffsyms)):
huffsyms[idx][2] = (huffsyms[idx - 1][2] + 1) << (
huffsyms[idx][1] - huffsyms[idx - 1][1])
@ -85,7 +89,7 @@ def buildhuffmantree(huffsyms):
continue
huffsubtree = hufftree
for bit in xrange(0, bitlen):
for bit in range(0, bitlen):
lr = huffcode & (1 << (bitlen - bit - 1)) != 0
if bit < bitlen - 1:
@ -137,7 +141,7 @@ def loadcharlenhuffmansyms(bits, extra_hufftree):
# Allocate huffman codes to the length-ordered symbols
huffsyms[0][2] = 0
for idx in xrange(1, len(huffsyms)):
for idx in range(1, len(huffsyms)):
huffsyms[idx][2] = (huffsyms[idx - 1][2] + 1) << (
huffsyms[idx][1] - huffsyms[idx - 1][1])
@ -148,7 +152,7 @@ def decompress(buf):
(compressed_size, decompressed_size) = struct.unpack("<II", buf[0:8])
bits = BitArray(buf[8:])
outbuf = ''
outbuf = bytearray()
blocksize = 0
charlen_hufftree = None
positionset_hufftree = None
@ -165,7 +169,7 @@ def decompress(buf):
c = huffmandecode(charlen_hufftree, bits)
blocksize -= 1
if c < 256:
outbuf += chr(c)
outbuf.append(c)
decompressed_size -= 1
else:
data_length = (c & 0xff) + 3
@ -176,8 +180,8 @@ def decompress(buf):
pos_bitlen - 1)
data_idx = len(outbuf) - data_offset - 1
for i in xrange(0, data_length):
outbuf += outbuf[data_idx + i]
for i in range(0, data_length):
outbuf.append(outbuf[data_idx + i])
decompressed_size -= data_length
return outbuf
return bytes(outbuf)

View File

@ -134,11 +134,12 @@ class LenovoFirmwareConfig(object):
self.connection = ipmicmd
def imm_size(self, filename):
data = []
data += LENOVO_ENTERPRISE
data += SIZE_COMMAND
for i in range(len(filename)):
data += [ord(filename[i])]
data = bytearray()
data.extend(LENOVO_ENTERPRISE)
data.extend(SIZE_COMMAND)
if not isinstance(filename, bytes):
filename = filename.encode('utf-8')
data.extend(filename)
response = run_command_with_retry(self.connection, data=data)
@ -150,21 +151,21 @@ class LenovoFirmwareConfig(object):
def imm_open(self, filename, write=False, size=None):
response = None
retries = 12
data = []
data += LENOVO_ENTERPRISE
data = bytearray()
data.extend(LENOVO_ENTERPRISE)
if write is False:
data += OPEN_RO_COMMAND
data.extend(OPEN_RO_COMMAND)
else:
assert size is not None
data += OPEN_WO_COMMAND
data.extend(OPEN_WO_COMMAND)
hex_size = struct.pack("<I", size)
for byte in hex_size[:4]:
data += [ord(byte)]
data += [0x01, 0x10]
for i in range(len(filename)):
data += [ord(filename[i])]
data.extend(bytearray(hex_size[:4]))
data.extend([0x01, 0x10])
if not isinstance(filename, bytes):
filename = filename.encode('utf-8')
data.extend(filename)
while len(data) < 38:
data += [0x00]
data.append(0)
while retries:
retries = retries-1
@ -185,10 +186,7 @@ class LenovoFirmwareConfig(object):
data += CLOSE_COMMAND
hex_filehandle = struct.pack("<I", filehandle)
for byte in hex_filehandle[:4]:
data += [ord(byte)]
data.extend(bytearray(hex_filehandle[:4]))
run_command_with_retry(self.connection, data=data)
def imm_write(self, filehandle, size, inputdata):
@ -199,20 +197,17 @@ class LenovoFirmwareConfig(object):
hex_filehandle = struct.pack("<I", filehandle)
while remaining > 0:
data = []
data += LENOVO_ENTERPRISE
data += WRITE_COMMAND
for byte in hex_filehandle[:4]:
data += [ord(byte)]
data = bytearray()
data.extend(LENOVO_ENTERPRISE)
data.extend(WRITE_COMMAND)
data.extend(hex_filehandle[:4])
hex_offset = struct.pack("<I", offset)
for byte in hex_offset[:4]:
data += [ord(byte)]
data.extend(hex_offset[:4])
if remaining < blocksize:
amount = remaining
else:
amount = blocksize
for byte in inputdata[offset:offset+amount]:
data += [ord(byte)]
data.extend(inputdata[offset:offset+amount])
remaining -= blocksize
offset += blocksize
run_command_with_retry(self.connection, data=data)
@ -220,7 +215,7 @@ class LenovoFirmwareConfig(object):
def imm_read(self, filehandle, size):
blocksize = 0xc8
offset = 0
output = ''
output = b''
remaining = size
hex_filehandle = struct.pack("<I", filehandle)
@ -230,15 +225,12 @@ class LenovoFirmwareConfig(object):
data = []
data += LENOVO_ENTERPRISE
data += READ_COMMAND
for byte in hex_filehandle[:4]:
data += [ord(byte)]
data.extend(bytearray(hex_filehandle[:4]))
hex_offset = struct.pack("<I", offset)
for byte in hex_offset[:4]:
data += [ord(byte)]
data.extend(bytearray(hex_offset[:4]))
if remaining < blocksize:
hex_blocksize = struct.pack("<H", remaining)
for byte in hex_blocksize[:2]:
data += [ord(byte)]
data.extend(hex_blocksize[:2])
remaining -= blocksize
offset += blocksize
@ -259,7 +251,7 @@ class LenovoFirmwareConfig(object):
def get_fw_options(self):
options = {}
data = None
for i in range(0, 30):
for _ in range(0, 30):
filehandle = self.imm_open("config.efi")
size = self.imm_size("config.efi")
data = self.imm_read(filehandle, size)

View File

@ -764,4 +764,4 @@ class SPD(object):
self.info['serial'] = hex(struct.unpack(
'>I', struct.pack('4B', *spd[325:329]))[0])[2:].rjust(8, '0')
self.info['model'] = struct.pack('18B', *spd[329:347]).strip(
'\x00\xff ')
b'\x00\xff ')

View File

@ -700,7 +700,7 @@ class SDR(object):
self.fw_minor, modtime)
cachefilename = os.path.join(self.cachedir, cachefilename)
if cachefilename and os.path.isfile(cachefilename):
with open(cachefilename, 'r') as cfile:
with open(cachefilename, 'rb') as cfile:
csdrs = pickle.load(cfile)
for sdrdata in csdrs:
self.add_sdr(sdrdata)
@ -782,7 +782,7 @@ class SDR(object):
if cachefilename:
suffix = ''.join(
random.choice(string.ascii_lowercase) for _ in range(12))
with open(cachefilename + '.' + suffix, 'w') as cfile:
with open(cachefilename + '.' + suffix, 'wb') as cfile:
pickle.dump(sdrraw, cfile)
os.rename(cachefilename + '.' + suffix, cachefilename)

View File

@ -139,6 +139,10 @@ class SecureHTTPConnection(httplib.HTTPConnection, object):
self.stdheaders[key] = value
def set_basic_credentials(self, username, password):
if isinstance(username, bytes) and not isinstance(username, str):
username = username.decode('utf-8')
if isinstance(password, bytes) and not isinstance(password, str):
password = password.decode('utf-8')
authinfo = ':'.join((username, password))
if not isinstance(authinfo, bytes):
authinfo = authinfo.encode('utf-8')