diff --git a/cyborg/accelerator/drivers/aichip/huawei/ascend.py b/cyborg/accelerator/drivers/aichip/huawei/ascend.py index e27d34e6..96706868 100644 --- a/cyborg/accelerator/drivers/aichip/huawei/ascend.py +++ b/cyborg/accelerator/drivers/aichip/huawei/ascend.py @@ -24,12 +24,12 @@ from cyborg.common import constants from oslo_serialization import jsonutils -PCI_INFO_PATTERN = re.compile("(?P[0-9a-f]{4}:[0-9a-f]{2}:" - "[0-9a-f]{2}\.[0-9a-f]) " - "(?P.*) [\[].*]: (?P.*) .*" - "[\[](?P[0-9a-fA-F]" - "{4}):(?P[0-9a-fA-F]{4})].*" - "[(rev ](?P[0-9a-f]{2})") +PCI_INFO_PATTERN = re.compile(r"(?P[0-9a-f]{4}:[0-9a-f]{2}:" + r"[0-9a-f]{2}\.[0-9a-f]) " + r"(?P.*) [\[].*]: (?P.*) .*" + r"[\[](?P[0-9a-fA-F]" + r"{4}):(?P[0-9a-fA-F]{4})].*" + r"[(rev ](?P[0-9a-f]{2})") class AscendDriver(GenericDriver): diff --git a/cyborg/accelerator/drivers/fake.py b/cyborg/accelerator/drivers/fake.py index 396f53eb..33f540f2 100644 --- a/cyborg/accelerator/drivers/fake.py +++ b/cyborg/accelerator/drivers/fake.py @@ -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() diff --git a/cyborg/accelerator/drivers/fpga/intel/sysinfo.py b/cyborg/accelerator/drivers/fpga/intel/sysinfo.py index 2dd831a0..36b03d06 100644 --- a/cyborg/accelerator/drivers/fpga/intel/sysinfo.py +++ b/cyborg/accelerator/drivers/fpga/intel/sysinfo.py @@ -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} diff --git a/cyborg/accelerator/drivers/gpu/utils.py b/cyborg/accelerator/drivers/gpu/utils.py index 41088e39..dff452b5 100644 --- a/cyborg/accelerator/drivers/gpu/utils.py +++ b/cyborg/accelerator/drivers/gpu/utils.py @@ -34,11 +34,11 @@ import cyborg.privsep LOG = logging.getLogger(__name__) GPU_FLAGS = ["VGA compatible controller", "3D controller"] -GPU_INFO_PATTERN = re.compile("(?P[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:" - "[0-9a-fA-F]{2}\.[0-9a-fA-F]) " - "(?P.*) [\[].*]: (?P.*) .*" - "[\[](?P[0-9a-fA-F]" - "{4}):(?P[0-9a-fA-F]{4})].*") +GPU_INFO_PATTERN = re.compile(r"(?P[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:" + r"[0-9a-fA-F]{2}\.[0-9a-fA-F]) " + r"(?P.*) [\[].*]: (?P.*) .*" + r"[\[](?P[0-9a-fA-F]" + r"{4}):(?P[0-9a-fA-F]{4})].*") VENDOR_MAPS = {"10de": "nvidia", "102b": "matrox"} diff --git a/cyborg/accelerator/drivers/modules/generic.py b/cyborg/accelerator/drivers/modules/generic.py index a00f010e..f223a176 100644 --- a/cyborg/accelerator/drivers/modules/generic.py +++ b/cyborg/accelerator/drivers/modules/generic.py @@ -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 diff --git a/cyborg/api/controllers/types.py b/cyborg/api/controllers/types.py index 814bacf2..24b325fb 100644 --- a/cyborg/api/controllers/types.py +++ b/cyborg/api/controllers/types.py @@ -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) diff --git a/cyborg/api/middleware/auth_token.py b/cyborg/api/middleware/auth_token.py index 71a734ea..ab445d2c 100644 --- a/cyborg/api/middleware/auth_token.py +++ b/cyborg/api/middleware/auth_token.py @@ -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) diff --git a/cyborg/hacking/checks.py b/cyborg/hacking/checks.py index c114be32..4745a204 100644 --- a/cyborg/hacking/checks.py +++ b/cyborg/hacking/checks.py @@ -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) diff --git a/cyborg/quota.py b/cyborg/quota.py index 39ccc8e3..be82d8ec 100644 --- a/cyborg/quota.py +++ b/cyborg/quota.py @@ -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() diff --git a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py index 40891c9d..d0f0db5e 100644 --- a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py +++ b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py @@ -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): diff --git a/test-requirements.txt b/test-requirements.txt index f592ead7..bcf6fa9c 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -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 diff --git a/tox.ini b/tox.ini index b02966b6..7f0af9f6 100644 --- a/tox.ini +++ b/tox.ini @@ -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