From cc0d1cbb38c53b6c07f74373ff2d788e7c659ca1 Mon Sep 17 00:00:00 2001 From: xuweibj Date: Fri, 8 Mar 2019 03:25:42 -0500 Subject: [PATCH] Modify openbmc python code to support python3 --- xCAT-openbmc-py/lib/python/agent/client.py | 4 ++-- .../lib/python/agent/common/rest.py | 19 ++++++++++--------- .../lib/python/agent/common/task.py | 8 ++++---- .../lib/python/agent/common/utils.py | 4 ++-- .../agent/hwctl/openbmc/openbmc_bmcconfig.py | 7 ++++--- .../agent/hwctl/openbmc/openbmc_eventlog.py | 4 ++-- .../agent/hwctl/openbmc/openbmc_flash.py | 6 +++--- .../agent/hwctl/openbmc/openbmc_inventory.py | 8 ++++---- .../lib/python/agent/hwctl/openbmc_client.py | 7 ++++--- .../lib/python/agent/xcatagent/server.py | 4 ++-- 10 files changed, 37 insertions(+), 34 deletions(-) diff --git a/xCAT-openbmc-py/lib/python/agent/client.py b/xCAT-openbmc-py/lib/python/agent/client.py index 1c8e0448e..ff2188e9e 100755 --- a/xCAT-openbmc-py/lib/python/agent/client.py +++ b/xCAT-openbmc-py/lib/python/agent/client.py @@ -55,13 +55,13 @@ class ClientShell(object): buf = json.dumps(req) s.send(utils.int2bytes(len(buf))) - s.send(buf) + s.send(buf.encode('utf-8')) while True: sz = s.recv(4) if len(sz) == 0: break sz = utils.bytes2int(sz) - data = s.recv(sz) + data = s.recv(sz).decode('utf-8') print(data) diff --git a/xCAT-openbmc-py/lib/python/agent/common/rest.py b/xCAT-openbmc-py/lib/python/agent/common/rest.py index 2d0ec5c43..e82485c6d 100644 --- a/xCAT-openbmc-py/lib/python/agent/common/rest.py +++ b/xCAT-openbmc-py/lib/python/agent/common/rest.py @@ -11,7 +11,7 @@ import urllib3 urllib3.disable_warnings() from requests.auth import AuthBase -import exceptions as xcat_exception +from . import exceptions as xcat_exception class RestSession(object): @@ -31,31 +31,32 @@ class RestSession(object): timeout=timeout) except requests.exceptions.ConnectionError as e: # Extract real reason for the exception and host/port from ConnectionError message - # Sometimes e.message is a list, sometimes is a string. Look for different patterns # to extract the data needed. + e = str(e) causing_error = "n/a" host_and_port = "n/a" - if "]" in e.message[0]: - causing_error_part1 = e.message[0].split("]")[1] + if "]" in e: + causing_error_part1 = e.split("]")[1] causing_error = causing_error_part1.split("'")[0] causing_error = causing_error.strip() - host_and_port = self.extract_server_and_port(e.message[0], "STRING") + host_and_port = self.extract_server_and_port(e, "STRING") - if "Connection aborted." in e.message[0]: + if "Connection aborted." in e: causing_error = "Connection reset by peer" host_and_port = self.extract_server_and_port(url, "URL") - if "connect timeout=" in e.message[0]: + if "connect timeout=" in e: causing_error = "timeout" - host_and_port = self.extract_server_and_port(e.message[0], "STRING") + host_and_port = self.extract_server_and_port(e, "STRING") message = 'Failed to connect to server.' # message = '\n\n--> {0} \n\n'.format(e.message[0]) raise xcat_exception.SelfServerException(message, '({0})'.format(causing_error), host_and_port) except requests.exceptions.Timeout as e: + e = str(e) causing_error = "timeout" - host_and_port = self.extract_server_and_port(e.message[0], "STRING") + host_and_port = self.extract_server_and_port(e, "STRING") message = 'Timeout to connect to server' raise xcat_exception.SelfServerException(message, '({0})'.format(causing_error), host_and_port) diff --git a/xCAT-openbmc-py/lib/python/agent/common/task.py b/xCAT-openbmc-py/lib/python/agent/common/task.py index 76f7fa04d..1f93cab0e 100644 --- a/xCAT-openbmc-py/lib/python/agent/common/task.py +++ b/xCAT-openbmc-py/lib/python/agent/common/task.py @@ -35,7 +35,7 @@ class BaseCommand(object): self._pre(op, *args, **kwargs) self._execute(op, *args, **kwargs) self._post(op, *args, **kwargs) - except Exception, e: + except Exception as e: # TODO: put e into log print(traceback.format_exc(), file=sys.stderr) return None @@ -65,7 +65,7 @@ class ParallelNodesCommand(BaseCommand): assert self.inventory and type(self.inventory) is dict func = getattr(self, '%s' % op) if len(self.inventory) == 1: - node = self.inventory.keys()[0] + node = list(self.inventory.keys())[0] func(*args, node=node, nodeinfo=self.inventory[node], **kw) return @@ -75,7 +75,7 @@ class ParallelNodesCommand(BaseCommand): for node in self.inventory.keys(): try: gevent_pool.add( gevent.spawn(func, *args, node=node, nodeinfo=self.inventory[node], **kw)) - except Exception, e: + except Exception as e: error = '%s: Internel Error occured in gevent' % node #print(traceback.format_exc(), file=sys.stderr) self.callback.error(error) @@ -89,7 +89,7 @@ class ParallelNodesCommand(BaseCommand): self._pre(op, *args, **kwargs) self._execute_in_parallel(op, *args, **kwargs) self._post(op, *args, **kwargs) - except Exception, e: + except Exception as e: # TODO: put e into log print(traceback.format_exc(), file=sys.stderr) return None diff --git a/xCAT-openbmc-py/lib/python/agent/common/utils.py b/xCAT-openbmc-py/lib/python/agent/common/utils.py index 252541b6c..bdbfba23e 100644 --- a/xCAT-openbmc-py/lib/python/agent/common/utils.py +++ b/xCAT-openbmc-py/lib/python/agent/common/utils.py @@ -63,9 +63,9 @@ def recv_all(sock, size): if left_size < recv_size: tmp_size = left_size buf_part = sock.recv(tmp_size) - buf_parts.append(buf_part) + buf_parts.append(buf_part.decode('utf-8')) buf_size += len(buf_part) - buf = ''.join(buf_parts) + buf = ''.join(str(i) for i in buf_parts) return buf diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_bmcconfig.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_bmcconfig.py index 19b990228..b7398d87c 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_bmcconfig.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_bmcconfig.py @@ -81,7 +81,7 @@ class OpenBMCBmcConfigTask(ParallelNodesCommand): self.callback.info('%s: No attributes returned from the BMC.' % node) return dump_info - keys = dump_dict.keys() + keys = list(dump_dict.keys()) keys.sort() for key in keys: info = '[%d] Generated: %s, Size: %s' % \ @@ -140,7 +140,7 @@ class OpenBMCBmcConfigTask(ParallelNodesCommand): return dump_dict = obmc.list_dump_info() - keys = dump_dict.keys() + keys = list(dump_dict.keys()) keys.sort() for key in keys: @@ -378,6 +378,7 @@ rmdir \"/tmp/$userid\" \n") self.callback.info("%s: BMC Setting Password..." % node) except (SelfServerException, SelfClientException) as e: self.callback.error(e.message, node) + return self.callback.info("%s: BMC password changed. Update 'bmcpasswd' for the node or the 'passwd' table with the new password." % node) @@ -422,7 +423,7 @@ rmdir \"/tmp/$userid\" \n") return self.callback.error(e.message, node) if isinstance(value, dict): - str_value = str(value.values()[0]) + str_value = str(list(value.values())[0]) elif value: str_value = str(value) else: diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_eventlog.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_eventlog.py index ad7f34853..65b29f230 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_eventlog.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_eventlog.py @@ -39,7 +39,7 @@ class OpenBMCEventlogTask(ParallelNodesCommand): # Get all eventlog records eventlog_info_dict = obmc.get_eventlog_info() - keys = eventlog_info_dict.keys() + keys = list(eventlog_info_dict.keys()) # Sort thy keys in natural order keys.sort(key=lambda x : int(x[0:])) @@ -76,7 +76,7 @@ class OpenBMCEventlogTask(ParallelNodesCommand): # Get all eventlog records eventlog_info_dict = obmc.get_eventlog_info() - keys = eventlog_info_dict.keys() + keys = list(eventlog_info_dict.keys()) # Sort the keys in natural order keys.sort(key=lambda x : int(x[0:])) diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_flash.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_flash.py index 2b4d21bf7..59e0f4153 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_flash.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_flash.py @@ -81,7 +81,7 @@ class OpenBMCFlashTask(ParallelNodesCommand): def _get_firmware_version(self, target_file): version = purpose = None - with open(target_file, 'r') as fh: + with open(target_file, encoding="utf8", errors='ignore') as fh: for line in fh: if 'version=' in line: version = line.split('=')[-1].strip() @@ -159,7 +159,7 @@ class OpenBMCFlashTask(ParallelNodesCommand): mapping_ids = [] if self.firmware: - version_list = self.firmware.keys() + version_list = list(self.firmware.keys()) else: return [] @@ -348,7 +348,7 @@ class OpenBMCFlashTask(ParallelNodesCommand): firmware_version = '' if self.firmware_file: - firmware_version = self.firmware.keys()[0] + firmware_version = list(self.firmware.keys())[0] try: obmc.upload_firmware(self.firmware_file) except (SelfServerException, SelfClientException) as e: diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_inventory.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_inventory.py index dd9d15c76..fbf02c12f 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_inventory.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc/openbmc_inventory.py @@ -29,7 +29,7 @@ class OpenBMCInventoryTask(ParallelNodesCommand): target_file = utils.get_full_path(self.cwd, target_file) version = purpose = None - with open(target_file, 'r') as fh: + with open(target_file, encoding="utf8", errors='ignore') as fh: for line in fh: if 'version=' in line: version = line.split('=')[-1].strip() @@ -44,13 +44,13 @@ class OpenBMCInventoryTask(ParallelNodesCommand): def _get_firm_info(self, firm_info_list): (has_functional, firm_obj_dict) = firm_info_list firm_info = [] - keys = firm_obj_dict.keys() + keys = list(firm_obj_dict.keys()) keys.sort() for key in keys: flag = '' if firm_obj_dict[key].functional: flag = '*' - elif firm_obj_dict[key].priority == 0: + elif firm_obj_dict[key].priority == 0: if not has_functional: flag = '*' else: @@ -115,7 +115,7 @@ class OpenBMCInventoryTask(ParallelNodesCommand): # Process returned inventory_info_dict depending on the inventory requested if all == 1: # Everything gets displayed, even firmware - keys = inventory_info_dict.keys() + keys = list(inventory_info_dict.keys()) keys.sort() for key in keys: inventory_info += utils.sort_string_with_numbers(inventory_info_dict[key]) diff --git a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py index c244abc40..3b1fffee4 100644 --- a/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py +++ b/xCAT-openbmc-py/lib/python/agent/hwctl/openbmc_client.py @@ -450,7 +450,7 @@ class OpenBMCRest(object): payload = { "data": PROJECT_PAYLOAD + BMC_URLS['reboot']['field'] } try: self.request('PUT', BMC_URLS['reboot']['path'], payload=payload, cmd='bmc_reset') - except SelfServerException,SelfClientException: + except (SelfServerException,SelfClientException) as e: # TODO: Need special handling for bmc reset, as it is normal bmc may return error pass @@ -578,7 +578,8 @@ class OpenBMCRest(object): logger.debug('IndexError (-2) for %s' % key) continue - key_type = filter(lambda x:x not in '0123456789', key_id).upper() + key_type_list = [x for x in key_id if x not in '0123456789'] + key_type = ''.join(key_type_list).upper() if key_type == 'CORE': key_type = 'CPU' @@ -656,7 +657,7 @@ class OpenBMCRest(object): # Check if policy table file is there ras_event_mapping = {} if os.path.isfile(RAS_POLICY_TABLE): - with open(RAS_POLICY_TABLE, "r") as data_file: + with open(RAS_POLICY_TABLE, encoding="utf8", errors='ignore') as data_file: policy_hash = json.load(data_file) if policy_hash: ras_event_mapping = policy_hash['events'] diff --git a/xCAT-openbmc-py/lib/python/agent/xcatagent/server.py b/xCAT-openbmc-py/lib/python/agent/xcatagent/server.py index 85a92dddd..7a676975a 100644 --- a/xCAT-openbmc-py/lib/python/agent/xcatagent/server.py +++ b/xCAT-openbmc-py/lib/python/agent/xcatagent/server.py @@ -25,7 +25,7 @@ class XCATMessager(utils.Messager): def _send(self, d): buf = json.dumps(d) self.sem.acquire() - self.sock.sendall(utils.int2bytes(len(buf)) + buf) + self.sock.sendall(utils.int2bytes(len(buf)) + buf.encode('utf-8')) self.sem.release() def info(self, msg): @@ -101,7 +101,7 @@ class Server(object): new_args=[] if req['args']: for a in req['args']: - new_args.append(a.encode('utf-8')) + new_args.append(a) # call the function in the specified manager func(req['nodeinfo'], new_args) # after the method returns, the request should be handled