mirror of
https://opendev.org/x/pyghmi
synced 2025-01-27 19:37:44 +00:00
Prepare to switch to flake8
Second wave of changes to prepare to migrate to flake8 tests. Change-Id: I0bf7ad7575eacac63e2e4d6f4066c8f51587943c
This commit is contained in:
parent
7f53f903da
commit
ae21ae184d
@ -1,5 +1,3 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 IBM Corporation
|
||||
# Copyright 2015-2017 Lenovo
|
||||
#
|
||||
@ -14,7 +12,8 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# This represents the low layer message framing portion of IPMI
|
||||
|
||||
"""This represents the low layer message framing portion of IPMI"""
|
||||
|
||||
import collections
|
||||
import hashlib
|
||||
@ -29,12 +28,15 @@ import threading
|
||||
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
|
||||
from cryptography.hazmat.primitives.ciphers import algorithms
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher
|
||||
from cryptography.hazmat.primitives.ciphers import modes
|
||||
|
||||
import pyghmi.exceptions as exc
|
||||
from pyghmi.ipmi.private import constants
|
||||
from pyghmi.ipmi.private import util
|
||||
from pyghmi.ipmi.private.util import get_ipmi_error, _monotonic_time
|
||||
from pyghmi.ipmi.private.util import _monotonic_time
|
||||
from pyghmi.ipmi.private.util import get_ipmi_error
|
||||
|
||||
|
||||
KEEPALIVE_SESSIONS = threading.RLock()
|
||||
@ -250,8 +252,9 @@ def _poller(timeout=0):
|
||||
|
||||
|
||||
def _aespad(data):
|
||||
"""ipmi demands a certain pad scheme,
|
||||
per table 13-20 AES-CBC encrypted payload fields.
|
||||
"""ipmi demands a certain pad scheme, per table 13-20 AES-CBC encrypted
|
||||
|
||||
payload fields.
|
||||
"""
|
||||
currlen = len(data) + 1 # need to count the pad length field as well
|
||||
neededpad = currlen % 16
|
||||
@ -399,9 +402,7 @@ class Session(object):
|
||||
return tmpsocket
|
||||
|
||||
def _sync_login(self, response):
|
||||
"""Handle synchronous callers in liue of
|
||||
a client-provided callback.
|
||||
"""
|
||||
"""Handle synchronous callers in lieu of a client-provided callback"""
|
||||
# Be a stub, the __init__ will catch and respond to ensure response
|
||||
# is given in the same thread as was called
|
||||
return
|
||||
@ -621,6 +622,7 @@ class Session(object):
|
||||
|
||||
def _make_bridge_request_msg(self, channel, netfn, command):
|
||||
"""This function generate message for bridge request. It is a
|
||||
|
||||
part of ipmi payload.
|
||||
"""
|
||||
head = bytearray((constants.IPMI_BMC_ADDRESS,
|
||||
@ -636,6 +638,7 @@ class Session(object):
|
||||
|
||||
def _add_request_entry(self, entry=()):
|
||||
"""This function record the request with netfn, sequence number and
|
||||
|
||||
command, which will be used in parse_ipmi_payload.
|
||||
:param entry: a set of netfn, sequence number and command.
|
||||
"""
|
||||
@ -651,6 +654,7 @@ class Session(object):
|
||||
|
||||
def _make_ipmi_payload(self, netfn, command, bridge_request=None, data=()):
|
||||
"""This function generates the core ipmi payload that would be
|
||||
|
||||
applicable for any channel (including KCS)
|
||||
"""
|
||||
bridge_msg = []
|
||||
@ -906,9 +910,9 @@ class Session(object):
|
||||
message.append(neededpad)
|
||||
message.append(7) # reserved, 7 is the required value for the
|
||||
# specification followed
|
||||
message += hmac.new(self.k1,
|
||||
bytes(message[4:]),
|
||||
self.currhashlib).digest()[:self.currhashlen]
|
||||
message += hmac.new(
|
||||
self.k1, bytes(message[4:]),
|
||||
self.currhashlib).digest()[:self.currhashlen]
|
||||
# per RFC2404 truncates to 96 bits
|
||||
self.netpacket = message
|
||||
# advance idle timer since we don't need keepalive while sending
|
||||
@ -1239,8 +1243,8 @@ class Session(object):
|
||||
return _keptalive
|
||||
|
||||
def _keepalive(self):
|
||||
"""Performs a keepalive to avoid idle disconnect
|
||||
"""
|
||||
"""Performs a keepalive to avoid idle disconnect"""
|
||||
|
||||
try:
|
||||
keptalive = False
|
||||
if self._customkeepalives:
|
||||
@ -1267,7 +1271,7 @@ class Session(object):
|
||||
return
|
||||
if self.autokeepalive:
|
||||
self.raw_command(netfn=6, command=1,
|
||||
callback=self._keepalive_wrapper(None))
|
||||
callback=self._keepalive_wrapper(None))
|
||||
else:
|
||||
self.logout()
|
||||
except exc.IpmiException:
|
||||
@ -1379,7 +1383,8 @@ class Session(object):
|
||||
if self.k1 is None: # we are in no shape to process a packet now
|
||||
return
|
||||
expectedauthcode = hmac.new(
|
||||
self.k1, data[4:-self.currhashlen], self.currhashlib).digest()[:self.currhashlen]
|
||||
self.k1, data[4:-self.currhashlen],
|
||||
self.currhashlib).digest()[:self.currhashlen]
|
||||
if authcode != expectedauthcode:
|
||||
return # BMC failed to assure integrity to us, drop it
|
||||
sid = struct.unpack("<I", bytes(data[6:10]))[0]
|
||||
@ -1504,11 +1509,13 @@ class Session(object):
|
||||
self.remoterandombytes = bytes(data[8:24])
|
||||
self.remoteguid = bytes(data[24:40])
|
||||
userlen = len(self.userid)
|
||||
hmacdata = struct.pack("<II", localsid, self.pendingsessionid) +\
|
||||
self.randombytes + self.remoterandombytes + self.remoteguid +\
|
||||
struct.pack("2B", self.nameonly | self.privlevel, userlen) +\
|
||||
self.userid
|
||||
expectedhash = hmac.new(self.password, hmacdata, self.currhashlib).digest()
|
||||
hmacdata = (struct.pack("<II", localsid, self.pendingsessionid) +
|
||||
self.randombytes + self.remoterandombytes +
|
||||
self.remoteguid + struct.pack(
|
||||
"2B", self.nameonly | self.privlevel, userlen) +
|
||||
self.userid)
|
||||
expectedhash = hmac.new(self.password, hmacdata,
|
||||
self.currhashlib).digest()
|
||||
hashlen = len(expectedhash)
|
||||
givenhash = struct.pack("%dB" % hashlen, *data[40:hashlen + 40])
|
||||
if givenhash != expectedhash:
|
||||
@ -1575,8 +1582,8 @@ class Session(object):
|
||||
hmacdata = self.randombytes +\
|
||||
struct.pack("<I", self.pendingsessionid) +\
|
||||
self.remoteguid
|
||||
expectedauthcode = hmac.new(self.sik, hmacdata,
|
||||
self.currhashlib).digest()[:self.currhashlen]
|
||||
expectedauthcode = hmac.new(
|
||||
self.sik, hmacdata, self.currhashlib).digest()[:self.currhashlen]
|
||||
aclen = len(expectedauthcode)
|
||||
authcode = struct.pack("%dB" % aclen, *data[8:aclen + 8])
|
||||
if authcode != expectedauthcode:
|
||||
|
@ -1,6 +1,3 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
# coding=utf8
|
||||
|
||||
# Copyright 2015 Lenovo
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -15,17 +12,18 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# This implements parsing of DDR SPD data. This is offered up in a pass
|
||||
# through fashion by some service processors.
|
||||
"""This implements parsing of DDR SPD data. This is offered up in a pass
|
||||
through fashion by some service processors.
|
||||
|
||||
# For now, just doing DDR3 and DDR4
|
||||
For now, just doing DDR3 and DDR4
|
||||
|
||||
# In many cases, astute readers will note that some of the lookup tables
|
||||
# should be a matter of math rather than lookup. However the SPD
|
||||
# specification explicitly reserves values not in the lookup tables for
|
||||
# future use. It has happened, for example, that a spec was amended
|
||||
# with discontinuous values for a field that was until that point
|
||||
# possible to derive in a formulaic way
|
||||
In many cases, astute readers will note that some of the lookup tables
|
||||
should be a matter of math rather than lookup. However the SPD
|
||||
specification explicitly reserves values not in the lookup tables for
|
||||
future use. It has happened, for example, that a spec was amended
|
||||
with discontinuous values for a field that was until that point
|
||||
possible to derive in a formulaic way
|
||||
"""
|
||||
|
||||
import struct
|
||||
|
||||
@ -721,9 +719,9 @@ class SPD(object):
|
||||
if fineoffset & 0b10000000:
|
||||
# Take two's complement for negative offset
|
||||
fineoffset = 0 - ((fineoffset ^ 0xff) + 1)
|
||||
fineoffset = (finetime * fineoffset) * 10**-3
|
||||
fineoffset = (finetime * fineoffset) * 10 ** -3
|
||||
mtb = spd[10] / float(spd[11])
|
||||
clock = 2 // ((mtb * spd[12] + fineoffset)*10**-3)
|
||||
clock = 2 // ((mtb * spd[12] + fineoffset) * 10 ** -3)
|
||||
self.info['speed'] = speed_from_clock(clock)
|
||||
self.info['ecc'] = (spd[8] & 0b11000) != 0
|
||||
self.info['module_type'] = module_types.get(spd[3] & 0xf, 'Unknown')
|
||||
|
@ -1,5 +1,3 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2015-2017 Lenovo
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,6 +11,7 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import ctypes
|
||||
import functools
|
||||
import os
|
||||
|
@ -16,13 +16,15 @@
|
||||
class Disk(object):
|
||||
def __init__(self, name, description=None, id=None, status=None,
|
||||
serial=None, fru=None, stripsize=None):
|
||||
"""
|
||||
:param name: A name descripbing the disk in human readable terms
|
||||
"""Define a disk object
|
||||
|
||||
:param name: A name describing the disk in human readable terms
|
||||
:param description: A description of the device
|
||||
:param id: Identifier used by the controller
|
||||
:param status: Controller indicated status of disk
|
||||
:param serial: Serial number of the drive
|
||||
:param fru: FRU number of the driver
|
||||
:param stripsize: The stripsize of the disk in kibibytes
|
||||
"""
|
||||
self.name = str(name)
|
||||
self.description = description
|
||||
@ -37,15 +39,17 @@ class Array(object):
|
||||
def __init__(self, disks=None, raid=None, status=None, volumes=(), id=None,
|
||||
spans=None, hotspares=(), capacity=None,
|
||||
available_capacity=None):
|
||||
"""
|
||||
"""Define an array of disks object
|
||||
|
||||
:param disks: An array of Disk objects
|
||||
:param layout: The layout of the Array, generally the RAID level
|
||||
:param raid: the RAID level
|
||||
:param status: Status of the array according to the controller
|
||||
:param id: Unique identifier used by controller to identify
|
||||
:param spans: Number of spans for a multi-dimensional array
|
||||
:param hotspares: List of Disk objects that are dedicated hot spares
|
||||
for this array.
|
||||
:param capacity: the total capacity of the array
|
||||
:param available_capacity: the remaining capacity of the array
|
||||
"""
|
||||
self.disks = disks
|
||||
self.raid = raid
|
||||
@ -61,12 +65,12 @@ class Array(object):
|
||||
class Volume(object):
|
||||
def __init__(self, name=None, size=None, status=None, id=None,
|
||||
stripsize=None):
|
||||
"""
|
||||
"""Define a Volume as an object
|
||||
|
||||
:param name: Name of the volume
|
||||
:param size: Size of the volume in MB
|
||||
:param status: Controller indicated status of the volume
|
||||
:param id: Controller idintefier of a given volume
|
||||
:param id: Controller identifier of a given volume
|
||||
:param stripsize: The stripsize of the volume in kibibytes
|
||||
"""
|
||||
self.name = name
|
||||
@ -97,7 +101,6 @@ class ConfigSpec(object):
|
||||
|
||||
:param disks: A list of Disk in the configuration not in an array
|
||||
:param arrays: A list of Array objects
|
||||
:return:
|
||||
"""
|
||||
self.disks = disks
|
||||
self.arrays = arrays
|
||||
|
@ -12,8 +12,9 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
from dateutil import tz
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
def parse_time(timeval):
|
||||
@ -61,4 +62,4 @@ def parse_time(timeval):
|
||||
return datetime.strptime(timeval, '%m/%d/%Y')
|
||||
except ValueError:
|
||||
pass
|
||||
return None
|
||||
return None
|
||||
|
@ -32,6 +32,7 @@ except ImportError:
|
||||
import http.client as httplib
|
||||
import http.cookies as Cookie
|
||||
import io
|
||||
import six
|
||||
|
||||
__author__ = 'jjohnson2'
|
||||
|
||||
@ -60,8 +61,9 @@ class FileUploader(threading.Thread):
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.rsp = self.wc.upload(self.url, self.filename, self.data,
|
||||
self.formname, otherfields=self.otherfields)
|
||||
self.rsp = self.wc.upload(
|
||||
self.url, self.filename, self.data, self.formname,
|
||||
otherfields=self.otherfields)
|
||||
except Exception:
|
||||
self.rspstatus = self.wc.rspstatus
|
||||
raise
|
||||
@ -78,6 +80,7 @@ class FileDownloader(threading.Thread):
|
||||
def run(self):
|
||||
self.wc.download(self.url, self.savefile)
|
||||
|
||||
|
||||
def get_upload_form(filename, data, formname, otherfields):
|
||||
if not formname:
|
||||
formname = filename
|
||||
@ -88,13 +91,18 @@ def get_upload_form(filename, data, formname, otherfields):
|
||||
data = data.read()
|
||||
except AttributeError:
|
||||
pass
|
||||
form = b'--' + BND + '\r\nContent-Disposition: form-data; ' \
|
||||
'name="{0}"; filename="{1}"\r\n'.format(formname,
|
||||
filename).encode('utf-8')
|
||||
form = (b'--' +
|
||||
BND +
|
||||
'\r\nContent-Disposition: form-data; '
|
||||
'name="{0}"; filename="{1}"\r\n'.format(
|
||||
formname, filename).encode('utf-8'))
|
||||
form += b'Content-Type: application/octet-stream\r\n\r\n' + data
|
||||
for ofield in otherfields:
|
||||
form += b'\r\n--' + BND + '\r\nContent-Disposition: form-data; ' \
|
||||
'name="{0}"\r\n\r\n{1}'.format(ofield, otherfields[ofield]).encode('utf-8')
|
||||
form += (b'\r\n--' +
|
||||
BND +
|
||||
'\r\nContent-Disposition: form-data; '
|
||||
'name="{0}"\r\n\r\n{1}'.format(
|
||||
ofield, otherfields[ofield]).encode('utf-8'))
|
||||
form += b'\r\n--' + BND + b'--\r\n'
|
||||
uploadforms[filename] = form
|
||||
return form
|
||||
@ -188,7 +196,8 @@ class SecureHTTPConnection(httplib.HTTPConnection, object):
|
||||
|
||||
def grab_json_response(self, url, data=None, referer=None, headers=None):
|
||||
self.lastjsonerror = None
|
||||
body, status = self.grab_json_response_with_status(url, data, referer, headers)
|
||||
body, status = self.grab_json_response_with_status(
|
||||
url, data, referer, headers)
|
||||
if status == 200:
|
||||
return body
|
||||
self.lastjsonerror = body
|
||||
@ -227,7 +236,7 @@ class SecureHTTPConnection(httplib.HTTPConnection, object):
|
||||
"""Download a file to filename or file object
|
||||
|
||||
"""
|
||||
if isinstance(file, str) or isinstance(file, unicode):
|
||||
if isinstance(file, six.string_types):
|
||||
file = open(file, 'wb')
|
||||
webclient = self.dupe()
|
||||
dlheaders = self.stdheaders.copy()
|
||||
@ -260,9 +269,8 @@ class SecureHTTPConnection(httplib.HTTPConnection, object):
|
||||
"""
|
||||
if data is None:
|
||||
data = open(filename, 'rb')
|
||||
self._upbuffer = io.BytesIO(get_upload_form(filename, data,
|
||||
formname,
|
||||
otherfields))
|
||||
self._upbuffer = io.BytesIO(get_upload_form(
|
||||
filename, data, formname, otherfields))
|
||||
ulheaders = self.stdheaders.copy()
|
||||
ulheaders['Content-Type'] = b'multipart/form-data; boundary=' + BND
|
||||
ulheaders['Content-Length'] = len(uploadforms[filename])
|
||||
|
Loading…
x
Reference in New Issue
Block a user