From 5539db9dcf59676906977969ca574b7d521cc4b8 Mon Sep 17 00:00:00 2001 From: Al Bailey Date: Wed, 5 Sep 2018 15:51:12 -0500 Subject: [PATCH] Convert pep8 to flake8 for stx-nfv Flake8 and Hacking offer stricter style checks than pep8. This submission fixes these codes: E305 expected 2 blank lines after class or function definition, found 1 E722 do not use bare except' F841 local variable 'blah' is assigned to but never used H101: Use TODO(NAME) H201: no 'except:' at least use 'except Exception:' H237: module commands is removed in Python 3 H238: old style class declaration, use new style(inherit from `object`) This submission enables 2 additional checks that are off by default H106 vim config in files H203 Use assertIs(Not)None to check for None 8 hacking codes and 2 flake8 types were added to the ignore list and will be fixed by later updates. An optional hacking code H904 will be enabled and the code fixed by a later commit Story: 2003499 Task: 26195 Change-Id: I48f99fc0dd41addc574c81cee6662600faeb8e75 Signed-off-by: Al Bailey --- .../histogram_analysis/Histogram.py | 3 +- .../histogram_analysis/plotter.py | 5 ++-- .../nfvi_plugins/nfvi_infrastructure_api.py | 2 +- .../nfv_unit_tests/tests/test_instance.py | 2 +- .../tests/test_sw_patch_strategy.py | 2 +- .../tests/test_sw_upgrade_strategy.py | 2 +- .../nova_api_proxy/common/histogram.py | 1 + .../nova_api_proxy/common/log.py | 1 + .../nova_api_proxy/common/timestamp.py | 3 +- tox.ini | 28 +++++++++++++++---- 10 files changed, 35 insertions(+), 14 deletions(-) diff --git a/nfv/nfv-debug-tools/histogram_analysis/Histogram.py b/nfv/nfv-debug-tools/histogram_analysis/Histogram.py index 123026c2..3bf56d18 100755 --- a/nfv/nfv-debug-tools/histogram_analysis/Histogram.py +++ b/nfv/nfv-debug-tools/histogram_analysis/Histogram.py @@ -57,7 +57,7 @@ call("cp nfv-vim.log nfv-vim.log.[0-9] nfv-vim.log.[0-9][0-9] nfv-vim.log.[0-9]. call("gunzip logs/nfv-vim.log.[0-9].gz logs/nfv-vim.log.[0-9][0-9].gz", shell=True) -class Parser: +class Parser(object): def __init__(self): self.proc="" # Name of process being read self.timestamp="" # Timestamp found on line stating process name @@ -132,6 +132,7 @@ class Parser: csvOut.write(line+"\n") csvOut.close() + process=Parser() process.main() print("\nComplete\n") diff --git a/nfv/nfv-debug-tools/histogram_analysis/plotter.py b/nfv/nfv-debug-tools/histogram_analysis/plotter.py index c46874a9..63a92d40 100755 --- a/nfv/nfv-debug-tools/histogram_analysis/plotter.py +++ b/nfv/nfv-debug-tools/histogram_analysis/plotter.py @@ -48,7 +48,7 @@ import plotly.graph_objs as go from plotly.graph_objs import Scatter, Layout from plotly import tools from glob import iglob -import commands +import subprocess from builtins import input dir = os.path.dirname(__file__) @@ -195,7 +195,6 @@ def gCommand(groups): liNum=i if str("GroupName="+g) == cfgLines[i].strip(): groupFound=True - linum=i while not finishedGroup: liNum+=1 if "GroupEND" in cfgLines[liNum]: @@ -366,7 +365,7 @@ def setFilename(graphName): print("Welcome to plotter, type --help for information") # Checks that plotly is installed, otherwise graphs cannot be generated. -plotCheck=commands.getstatusoutput("pip list | grep plotly") +plotCheck=subprocess.getstatusoutput("pip list | grep plotly") if plotCheck[0]==0: if "plotly" not in plotCheck[1]: print("\n\tWARNING: Plotly is not installed on your system.\n\tPlease install it with: sudo pip install plotly\n") diff --git a/nfv/nfv-plugins/nfv_plugins/nfvi_plugins/nfvi_infrastructure_api.py b/nfv/nfv-plugins/nfv_plugins/nfvi_plugins/nfvi_infrastructure_api.py index 2720ca50..8e84603f 100755 --- a/nfv/nfv-plugins/nfv_plugins/nfvi_plugins/nfvi_infrastructure_api.py +++ b/nfv/nfv-plugins/nfv_plugins/nfvi_plugins/nfvi_infrastructure_api.py @@ -122,7 +122,7 @@ class NFVIInfrastructureAPI(nfvi.api.v1.NFVIInfrastructureAPI): @staticmethod def _host_supports_kubernetes(personality): - # TODO: This check will disappear once kubernetes is the default + # TODO(bwensley): This check will disappear once kubernetes is the default if os.path.isfile('/etc/kubernetes/admin.conf'): return ('compute' in personality or 'controller' in personality) else: diff --git a/nfv/nfv-tests/nfv_unit_tests/tests/test_instance.py b/nfv/nfv-tests/nfv_unit_tests/tests/test_instance.py index 2628122c..c3550b32 100755 --- a/nfv/nfv-tests/nfv_unit_tests/tests/test_instance.py +++ b/nfv/nfv-tests/nfv_unit_tests/tests/test_instance.py @@ -249,7 +249,7 @@ def fake_event_issue(a, b, c, d): @mock.patch('nfv_vim.tables._instance_table._instance_table', _instance_table) @mock.patch('nfv_vim.tables._instance_type_table._instance_type_table', _instance_type_table) @mock.patch('nfv_vim.tables._image_table._image_table', _image_table) -class TestInstance: +class TestInstance(object): def setup(self): """ diff --git a/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_patch_strategy.py b/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_patch_strategy.py index fb9ad54b..88e26bde 100755 --- a/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_patch_strategy.py +++ b/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_patch_strategy.py @@ -297,7 +297,7 @@ def fake_event_issue(a, b, c, d): @mock.patch('nfv_vim.tables._host_aggregate_table._host_aggregate_table', _host_aggregate_table) @mock.patch('nfv_vim.tables._instance_table._instance_table', _instance_table) @mock.patch('nfv_vim.strategy._strategy.get_local_host_name', fake_host_name) -class TestSwPatchStrategy: +class TestSwPatchStrategy(object): def setup(self): """ diff --git a/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_upgrade_strategy.py b/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_upgrade_strategy.py index 8b8dfb76..5aee8223 100755 --- a/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_upgrade_strategy.py +++ b/nfv/nfv-tests/nfv_unit_tests/tests/test_sw_upgrade_strategy.py @@ -304,7 +304,7 @@ def fake_event_issue(a, b, c, d): @mock.patch('nfv_vim.tables._host_group_table._host_group_table', _host_group_table) @mock.patch('nfv_vim.tables._host_aggregate_table._host_aggregate_table', _host_aggregate_table) @mock.patch('nfv_vim.tables._instance_table._instance_table', _instance_table) -class TestSwUpgradeStrategy: +class TestSwUpgradeStrategy(object): def setup(self): """ diff --git a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/histogram.py b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/histogram.py index ee2981ef..80c10f10 100644 --- a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/histogram.py +++ b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/histogram.py @@ -105,6 +105,7 @@ class Histogram(object): '*' * min(60, bucket_value))) LOG.info("%s" % '-' * 120) + _histograms = list() diff --git a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/log.py b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/log.py index b9a22008..4b718cff 100644 --- a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/log.py +++ b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/log.py @@ -35,6 +35,7 @@ class ProxySysLogHandler(SysLogHandler): msg = logging.handlers.SysLogHandler.format(self, record) return self.binary_name + '(' + self.app + ')' + ': ' + msg + _loggers = {} diff --git a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/timestamp.py b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/timestamp.py index ba2b75e9..fef84409 100644 --- a/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/timestamp.py +++ b/nova-api-proxy/nova-api-proxy/nova_api_proxy/common/timestamp.py @@ -15,11 +15,12 @@ class timespec(ctypes.Structure): """ _fields_ = [('tv_sec', ctypes.c_long), ('tv_nsec', ctypes.c_long)] + try: librt = ctypes.CDLL('librt.so.1', use_errno=True) clock_gettime = librt.clock_gettime clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] -except: +except Exception: raise OSError("Could not load librt.so.1 library") diff --git a/tox.ini b/tox.ini index 6c1edb58..c01dc978 100644 --- a/tox.ini +++ b/tox.ini @@ -24,7 +24,7 @@ commands = -o -type f -name '*.yaml' \ -print0 | xargs -0 yamllint" -[pep8] +[flake8] # Temporarily ignoring these warnings # E116 unexpected indentation (comment) # E121 continuation line under-indented for hanging indent @@ -43,16 +43,34 @@ commands = # E265 block comment should start with '# ' # E501 line too long # E712 comparison to bool should be reworded - -ignore = E116,E121,E122,E123,E124,E126,E127,E128,E129,E225,E226,E231,E241,E261,E265,E501,E712 +# - hacking codes - +# H102: license header not found +# H104: File contains nothing but comments +# H301: one import per line +# H306: imports not in alphabetical order +# H401: docstring should not start with a space +# H404: multi line docstring should start without a leading new line +# H405: multi line docstring summary not separated with an empty line +# H501: Do not use self.__dict__ for string formatting +# - flake8 codes - +# F401 '' imported but unused +# F821 undefined name 'unicode' (python3 specific) +ignore = E116,E121,E122,E123,E124,E126,E127,E128,E129,E225,E226,E231,E241,E261,E265,E501,E712, + H102,H104,H301,H306,H401,H404,H405,H501, + F401,F821, +# H106 Don’t put vim configuration in source files (off by default). +# H203 Use assertIs(Not)None to check for None (off by default). +# TODO: enable: H904 Delay string interpolations at logging calls (off by default). +enable-extensions = H106,H203 [testenv:pep8] usedevelop = False skip_install = True deps = - pep8 + hacking + flake8 commands = - pep8 + flake8 [testenv:venv] commands = {posargs}