Update hacking for Python3

The repo is Python 3 now, so update hacking to version 3.0 which
supports Python 3.

Fix problems found:
E117 over-indented
E501 line too long
E741 ambiguous variable name 'l'
W605 invalid escape sequence

Blacklist:
W503 line break before binary operator
W504 line break after binary operator

Update local hacking checks for new flake8.

Change-Id: I5d11a1f87d9547d9b6a27bf1f79cd69e0f73c741
This commit is contained in:
Andreas Jaeger 2020-03-30 16:07:49 +02:00
parent 07db145919
commit a6f84335bb
12 changed files with 55 additions and 45 deletions

View File

@ -24,12 +24,12 @@ from cyborg.common import constants
from oslo_serialization import jsonutils
PCI_INFO_PATTERN = re.compile("(?P<slot>[0-9a-f]{4}:[0-9a-f]{2}:"
"[0-9a-f]{2}\.[0-9a-f]) "
"(?P<class>.*) [\[].*]: (?P<device>.*) .*"
"[\[](?P<vendor_id>[0-9a-fA-F]"
"{4}):(?P<device_id>[0-9a-fA-F]{4})].*"
"[(rev ](?P<revision>[0-9a-f]{2})")
PCI_INFO_PATTERN = re.compile(r"(?P<slot>[0-9a-f]{4}:[0-9a-f]{2}:"
r"[0-9a-f]{2}\.[0-9a-f]) "
r"(?P<class>.*) [\[].*]: (?P<device>.*) .*"
r"[\[](?P<vendor_id>[0-9a-fA-F]"
r"{4}):(?P<device_id>[0-9a-fA-F]{4})].*"
r"[(rev ](?P<revision>[0-9a-f]{2})")
class AscendDriver(GenericDriver):

View File

@ -85,7 +85,7 @@ class FakeDriver(GenericDriver):
name = "%s_%s" % (CONF.host, pci.get('device'))
# Replace any non alphanumeric, hyphen or underscore character with
# underscore to comply with placement RP name requirements
driver_dep.name = re.sub("(?![a-zA-Z0-9_\-]).", "_", name)
driver_dep.name = re.sub(r"(?![a-zA-Z0-9_\-]).", "_", name)
driver_dep.driver_name = 'fake'
driver_dep.num_accelerators = self.NUM_ACCELERATORS
driver_dep.attribute_list = self._generate_attribute_list()

View File

@ -44,7 +44,7 @@ DEVICE = "device"
PF = "physfn"
VF = "virtfn*"
BDF_PATTERN = re.compile(
"^[a-fA-F\d]{4}:[a-fA-F\d]{2}:[a-fA-F\d]{2}\.[a-fA-F\d]$")
r"^[a-fA-F\d]{4}:[a-fA-F\d]{2}:[a-fA-F\d]{2}\.[a-fA-F\d]$")
DEVICE_FILE_MAP = {"vendor": "vendor",
"device": "product_id"}
@ -199,11 +199,11 @@ def get_traits(device_name, product_id, vf=True):
traits.append("CUSTOM_FPGA_INTEL")
traits.append("CUSTOM_FPGA_INTEL_" + PRODUCT_MAP.get(product_id))
for i in get_afu_ids(device_name):
l = "CUSTOM_FPGA_FUNCTION_ID_INTEL_" + i.upper()
traits.append(l)
la = "CUSTOM_FPGA_FUNCTION_ID_INTEL_" + i.upper()
traits.append(la)
for i in get_region_ids(device_name):
l = "CUSTOM_FPGA_REGION_INTEL_" + i.upper()
traits.append(l)
la = "CUSTOM_FPGA_REGION_INTEL_" + i.upper()
traits.append(la)
return {"traits": traits}

View File

@ -34,11 +34,11 @@ import cyborg.privsep
LOG = logging.getLogger(__name__)
GPU_FLAGS = ["VGA compatible controller", "3D controller"]
GPU_INFO_PATTERN = re.compile("(?P<devices>[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:"
"[0-9a-fA-F]{2}\.[0-9a-fA-F]) "
"(?P<controller>.*) [\[].*]: (?P<name>.*) .*"
"[\[](?P<vendor_id>[0-9a-fA-F]"
"{4}):(?P<product_id>[0-9a-fA-F]{4})].*")
GPU_INFO_PATTERN = re.compile(r"(?P<devices>[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:"
r"[0-9a-fA-F]{2}\.[0-9a-fA-F]) "
r"(?P<controller>.*) [\[].*]: (?P<name>.*) .*"
r"[\[](?P<vendor_id>[0-9a-fA-F]"
r"{4}):(?P<product_id>[0-9a-fA-F]{4})].*")
VENDOR_MAPS = {"10de": "nvidia", "102b": "matrox"}

View File

@ -59,16 +59,16 @@ class GENERICDRIVER(object):
def attach(self, accelerator, instance):
def install(self, accelerator):
pass
def install(self, accelerator):
pass
def detach(self, accelerator, instance):
def uninstall(self, accelerator):
pass
def uninstall(self, accelerator):
pass
def delete(self):
pass
def delete(self):
pass
def discover(self):
pass

View File

@ -131,7 +131,7 @@ integer = wtypes.IntegerType()
class JsonPatchType(wtypes.Base):
"""A complex type that represents a single json-patch operation."""
path = wtypes.wsattr(wtypes.StringType(pattern='^(/[\w-]+)+$'),
path = wtypes.wsattr(wtypes.StringType(pattern=r'^(/[\w-]+)+$'),
mandatory=True)
op = wtypes.wsattr(wtypes.Enum(str, 'add', 'replace', 'remove'),
mandatory=True)

View File

@ -36,7 +36,7 @@ class AuthTokenMiddleware(auth_token.AuthProtocol):
def __init__(self, app, conf, public_api_routes=None):
public_api_routes = public_api_routes or []
self.app = app
route_pattern_tpl = '%s(\.json)?$'
route_pattern_tpl = r'%s(\.json)?$'
try:
self.public_api_routes = [re.compile(route_pattern_tpl % route_tpl)

View File

@ -15,6 +15,8 @@
import re
from hacking import core
"""
Guidelines for writing new hacking checks
@ -40,7 +42,7 @@ assert_equal_with_is_not_none_re = re.compile(
r"assertEqual\(.*?\s+is+\s+not+\s+None\)$")
assert_true_isinstance_re = re.compile(
r"(.)*assertTrue\(isinstance\((\w|\.|\'|\"|\[|\])+, "
"(\w|\.|\'|\"|\[|\])+\)\)")
r"(\w|\.|\'|\"|\[|\])+\)\)")
dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
assert_xrange_re = re.compile(
r"\s*xrange\s*\(")
@ -56,16 +58,18 @@ custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
underscore_import_check = re.compile(r"(.)*import _(.)*")
translated_log = re.compile(
r"(.)*LOG\.(audit|error|info|critical|exception)"
"\(\s*_\(\s*('|\")")
r"\(\s*_\(\s*('|\")")
string_translation = re.compile(r"[^_]*_\(\s*('|\")")
@core.flake8ext
def no_mutable_default_args(logical_line):
msg = "M322: Method's default argument shouldn't be mutable!"
if mutable_default_args.match(logical_line):
yield (0, msg)
@core.flake8ext
def assert_equal_not_none(logical_line):
"""Check for assertEqual(A is not None) sentences M302"""
msg = "M302: assertEqual(A is not None) sentences not allowed."
@ -74,6 +78,7 @@ def assert_equal_not_none(logical_line):
yield (0, msg)
@core.flake8ext
def assert_true_isinstance(logical_line):
"""Check for assertTrue(isinstance(a, b)) sentences
@ -83,6 +88,7 @@ def assert_true_isinstance(logical_line):
yield (0, "M316: assertTrue(isinstance(a, b)) sentences not allowed")
@core.flake8ext
def assert_equal_in(logical_line):
"""Check for assertEqual(True|False, A in B), assertEqual(A in B, True|False)
@ -96,6 +102,7 @@ def assert_equal_in(logical_line):
"contents.")
@core.flake8ext
def no_xrange(logical_line):
"""Disallow 'xrange()'
@ -105,6 +112,7 @@ def no_xrange(logical_line):
yield(0, "M339: Do not use xrange().")
@core.flake8ext
def use_timeutils_utcnow(logical_line, filename):
# tools are OK to use the standard datetime module
if "/tools/" in filename:
@ -118,6 +126,7 @@ def use_timeutils_utcnow(logical_line, filename):
yield (pos, msg % f)
@core.flake8ext
def dict_constructor_with_list_copy(logical_line):
msg = ("M336: Must use a dict comprehension instead of a dict constructor"
" with a sequence of key-value pairs."
@ -126,6 +135,7 @@ def dict_constructor_with_list_copy(logical_line):
yield (0, msg)
@core.flake8ext
def no_log_warn(logical_line):
"""Disallow 'LOG.warn('
@ -140,6 +150,7 @@ def no_log_warn(logical_line):
yield (0, msg)
@core.flake8ext
def check_explicit_underscore_import(logical_line, filename):
"""Check for explicit import of the _ function
@ -159,15 +170,3 @@ def check_explicit_underscore_import(logical_line, filename):
elif (translated_log.match(logical_line) or
string_translation.match(logical_line)):
yield(0, "M340: Found use of _() without explicit import of _ !")
def factory(register):
register(no_mutable_default_args)
register(assert_equal_not_none)
register(assert_true_isinstance)
register(assert_equal_in)
register(use_timeutils_utcnow)
register(dict_constructor_with_list_copy)
register(no_xrange)
register(no_log_warn)
register(check_explicit_underscore_import)

View File

@ -187,4 +187,5 @@ class DbQuotaDriver(object):
# logged, however, because this is less than optimal.
LOG.exception("Failed to commit reservations %s", reservations)
QUOTAS = QuotaEngine()

View File

@ -19,8 +19,8 @@ from oslo_serialization import jsonutils
from cyborg.accelerator.drivers.gpu import utils
from cyborg.tests import base
NVIDIA_GPU_INFO = "0000:00:06.0 3D controller [0302]: NVIDIA Corporation GP100GL " \
"[Tesla P100 PCIe 12GB] [10de:15f7] (rev a1)"
NVIDIA_GPU_INFO = "0000:00:06.0 3D controller [0302]: NVIDIA Corporation " \
"GP100GL [Tesla P100 PCIe 12GB] [10de:15f7] (rev a1)"
class stdout(object):

View File

@ -2,7 +2,7 @@
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking!=0.13.0,<0.14,>=0.12.0 # Apache-2.0
hacking>=3.0,<3.1.0 # Apache-2.0
bandit>=1.6.0 # Apache-2.0
coverage>=3.6,!=4.4 # Apache-2.0

16
tox.ini
View File

@ -110,7 +110,7 @@ whitelist_externals = rm
[flake8]
filename = *.py,app.wsgi
show-source = True
ignore = E123,E125,H405
ignore = E123,E125,H405,W503,W504
builtins = _
enable-extensions = H106,H203,H904
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,*sqlalchemy/alembic/versions/*,demo/,releasenotes
@ -118,5 +118,15 @@ exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,*sqlalchemy/alembic/ver
[testenv:bandit]
commands = bandit -r cyborg -x cyborg/tests/* -n 5 -ll
[hacking]
local-check-factory = cyborg.hacking.checks.factory
[flake8:local-plugins]
extension =
M302 = checks:assert_equal_not_none
M310 = checks:use_timeutils_utcnow
M316 = checks:assert_true_isinstance
M322 = checks:no_mutable_default_args
M336 = checks:dict_constructor_with_list_copy
M338 = checks:assert_equal_in
M339 = checks:no_xrange
M340 = checks:check_explicit_underscore_import
M352 = checks:no_log_warn
paths = ./cyborg/hacking