Fix: others issues for Python 2/3 compatible code

1)adding from functools import reduce
2)replace func.func_name with func.__name__
3)from six.moves import filter, replace
  itertools.ifilter with filter
4)if in PY3, use __self__ insdeed of im_self
5)replace xmlrpclib with six.moves.xmlrpc_client
6)replace commands with subprocess
7)rename Queue to six.moves.queue
8)change lambda (x):func(x), to lambda x:func(x)
9)remove wrap_exception function in sysinv/common/exception.py
  and delete sysinv/common/safe_utils.py since it is not used

Story: 2003433
Task: 28371

Change-Id: I32f1b65ca90fe6ff5b7d77c5815dcf2775eb8f76
Signed-off-by: Sun Austin <austin.sun@intel.com>
This commit is contained in:
Sun Austin 2018-12-12 11:27:29 +08:00
parent bdba2e955e
commit 375b4bc121
9 changed files with 24 additions and 116 deletions

View File

@ -288,12 +288,12 @@ class EthInterface(Interface):
def getNetworkMap(self):
return {
'dataNetwork': lambda (node): DataNetwork(node),
'infraNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_INFRA),
'oamNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_OAM),
'mgmtNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_MGMT),
'pciPassthrough': lambda (node): PciPassthrough(node),
'pciSriov': lambda (node): PciSriov(node)
'dataNetwork': lambda node: DataNetwork(node),
'infraNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_INFRA),
'oamNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_OAM),
'mgmtNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_MGMT),
'pciPassthrough': lambda node: PciPassthrough(node),
'pciSriov': lambda node: PciSriov(node)
}
@ -330,10 +330,10 @@ class AeInterface(Interface):
def getNetworkMap(self):
return {
'dataNetwork': lambda (node): DataNetwork(node),
'infraNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_INFRA),
'oamNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_OAM),
'mgmtNetwork': lambda (node): ExternalNetwork(node, constants.NETWORK_TYPE_MGMT)
'dataNetwork': lambda node: DataNetwork(node),
'infraNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_INFRA),
'oamNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_OAM),
'mgmtNetwork': lambda node: ExternalNetwork(node, constants.NETWORK_TYPE_MGMT)
}
def validateWithIfNames(self, allInterfaceNames):

View File

@ -28,8 +28,8 @@ import threading
import time
import cgi
import Queue
import re
import six.moves.queue as Queue
import socket
import stat
from wsgiref import simple_server

View File

@ -25,13 +25,10 @@ SHOULD include dedicated exception logging.
"""
import functools
import six
from oslo_config import cfg
from sysinv.common import safe_utils
from sysinv.openstack.common import excutils
from sysinv.openstack.common import log as logging
from sysinv.openstack.common.gettextutils import _
@ -74,46 +71,6 @@ def _cleanse_dict(original):
return dict((k, v) for k, v in original.items() if "_pass" not in k)
def wrap_exception(notifier=None, publisher_id=None, event_type=None,
level=None):
"""This decorator wraps a method to catch any exceptions that may
get thrown. It logs the exception as well as optionally sending
it to the notification system.
"""
def inner(f):
def wrapped(self, context, *args, **kw):
# Don't store self or context in the payload, it now seems to
# contain confidential information.
try:
return f(self, context, *args, **kw)
except Exception as e:
with excutils.save_and_reraise_exception():
if notifier:
payload = dict(exception=e)
call_dict = safe_utils.getcallargs(f, *args, **kw)
cleansed = _cleanse_dict(call_dict)
payload.update({'args': cleansed})
# Use a temp vars so we don't shadow
# our outer definitions.
temp_level = level
if not temp_level:
temp_level = notifier.ERROR
temp_type = event_type
if not temp_type:
# If f has multiple decorators, they must use
# functools.wraps to ensure the name is
# propagated.
temp_type = f.__name__
notifier.notify(context, publisher_id, temp_type,
temp_level, payload)
return functools.wraps(f)(wrapped)
return inner
class SysinvException(Exception):
"""Base Sysinv Exception

View File

@ -1,55 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# Copyright 2011 Justin Santa Barbara
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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.
"""Utilities and helper functions that won't produce circular imports."""
import inspect
def getcallargs(function, *args, **kwargs):
"""This is a simplified inspect.getcallargs (2.7+).
It should be replaced when python >= 2.7 is standard.
"""
keyed_args = {}
argnames, varargs, keywords, defaults = inspect.getargspec(function)
keyed_args.update(kwargs)
# NOTE(alaski) the implicit 'self' or 'cls' argument shows up in
# argnames but not in args or kwargs. Uses 'in' rather than '==' because
# some tests use 'self2'.
if 'self' in argnames[0] or 'cls' == argnames[0]:
# The function may not actually be a method or have im_self.
# Typically seen when it's stubbed with mox.
if inspect.ismethod(function) and hasattr(function, 'im_self'):
keyed_args[argnames[0]] = function.im_self
else:
keyed_args[argnames[0]] = None
remaining_argnames = [x for x in argnames if x not in keyed_args]
keyed_args.update(dict(zip(remaining_argnames, args)))
if defaults:
num_defaults = len(defaults)
for argname, value in zip(argnames[-num_defaults:], defaults):
if argname not in keyed_args:
keyed_args[argname] = value
return keyed_args

View File

@ -493,7 +493,7 @@ def _wrap_db_error(f):
except Exception as e:
LOG.exception(_('DB exception wrapped.'))
raise exception.DBError(e)
_wrap.func_name = f.func_name
_wrap.__name__ = f.__name__
return _wrap

View File

@ -38,9 +38,9 @@ import functools
import inspect
import itertools
import json
import xmlrpclib
import six
import six.moves.xmlrpc_client as xmlrpclib
from sysinv.openstack.common import timeutils

View File

@ -41,7 +41,7 @@ import sys
import traceback
from oslo_config import cfg
from six.moves import filter
from sysinv.openstack.common.gettextutils import _
from sysinv.openstack.common import importutils
from sysinv.openstack.common import jsonutils
@ -285,7 +285,7 @@ class JSONFormatter(logging.Formatter):
def formatException(self, ei, strip_newlines=True):
lines = traceback.format_exception(*ei)
if strip_newlines:
lines = [itertools.ifilter(
lines = [filter(
lambda x: x,
line.rstrip().splitlines()) for line in lines]
lines = list(itertools.chain(*lines))

View File

@ -32,6 +32,7 @@ from sysinv.openstack.common import importutils
from sysinv.openstack.common import jsonutils
from sysinv.openstack.common import processutils as utils
from sysinv.openstack.common.rpc import common as rpc_common
from functools import reduce
zmq = importutils.try_import('eventlet.green.zmq')

View File

@ -40,8 +40,7 @@ postgres=# create user openstack_citest with createdb login password
postgres=# create database openstack_citest with owner openstack_citest;
"""
import commands
import six
from six.moves import configparser
import os
from six.moves.urllib.parse import urlparse
@ -199,7 +198,13 @@ class BaseMigrationTestCase(test_utils.BaseTestCase):
super(BaseMigrationTestCase, self).tearDown()
def execute_cmd(self, cmd=None):
status, output = commands.getstatusoutput(cmd)
if six.PY2:
import commands
status, output = commands.getstatusoutput(cmd)
else:
import subprocess
status, output = subprocess.getstatusoutput(cmd)
LOG.debug(output)
self.assertEqual(0, status,
"Failed to run: %s\n%s" % (cmd, output))