From a997f09e5bd9ce98cc57f1a5380453ff167e47ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Beraud?= Date: Fri, 21 Aug 2020 15:12:08 +0200 Subject: [PATCH] Adding pre-commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduced changes: - pre-commit config and rules - Add pre-commit to pep8 gate, Flake8 is covered in the pre-commit hooks. - Applying fixes for pre-commit compliance in all code. Also commit hash will be used instead of version tags in pre-commit to prevend arbitrary code from running in developer's machines. pre-commit will be used to: - trailing whitespace; - Replaces or checks mixed line ending (mixed-line-ending); - Forbid files which have a UTF-8 byte-order marker (check-byte-order-marker); - Checks that non-binary executables have a proper shebang (check-executables-have-shebangs); - Check for files that contain merge conflict strings (check-merge-conflict); - Check for debugger imports and py37+ breakpoint() calls in python source (debug-statements); - Attempts to load all yaml files to verify syntax (check-yaml); - Run flake8 checks (flake8) (local) For further details about tests please refer to: https://github.com/pre-commit/pre-commit-hooks Change-Id: Ibd0c3d64fdc5c293d9d676d33eab828d9fde971f Co-authored-by: Moisés Guimarães de Medeiros --- .pre-commit-config.yaml | 35 +++++++++++++++++++ doc/source/conf.py | 13 +++++++ doc/source/reference/index.rst | 2 +- .../tests/drivers/test_amqp_driver.py | 12 +++---- oslo_messaging/tests/functional/utils.py | 14 ++++---- oslo_messaging/tests/notify/test_notifier.py | 1 - oslo_messaging/tests/rpc/test_client.py | 0 oslo_messaging/tests/rpc/test_dispatcher.py | 0 oslo_messaging/tests/test_transport.py | 0 test-requirements.txt | 1 + tools/simulator.py | 9 +++-- tox.ini | 2 +- 12 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 .pre-commit-config.yaml mode change 100755 => 100644 oslo_messaging/tests/notify/test_notifier.py mode change 100755 => 100644 oslo_messaging/tests/rpc/test_client.py mode change 100755 => 100644 oslo_messaging/tests/rpc/test_dispatcher.py mode change 100755 => 100644 oslo_messaging/tests/test_transport.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..9d9455674 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,35 @@ +# We from the Oslo project decided to pin repos based on the +# commit hash instead of the version tag to prevend arbitrary +# code from running in developer's machines. To update to a +# newer version, run `pre-commit autoupdate` and then replace +# the newer versions with their commit hash. + +default_language_version: + python: python3 + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: ebc15addedad713c86ef18ae9632c88e187dd0af # v3.1.0 + hooks: + - id: trailing-whitespace + # Replaces or checks mixed line ending + - id: mixed-line-ending + args: ['--fix', 'lf'] + exclude: '.*\.(svg)$' + # Forbid files which have a UTF-8 byte-order marker + - id: check-byte-order-marker + # Checks that non-binary executables have a proper shebang + - id: check-executables-have-shebangs + # Check for files that contain merge conflict strings. + - id: check-merge-conflict + # Check for debugger imports and py37+ breakpoint() + # calls in python source + - id: debug-statements + - id: check-yaml + files: .*\.(yaml|yml)$ + - repo: https://gitlab.com/pycqa/flake8 + rev: 181bb46098dddf7e2d45319ea654b4b4d58c2840 # 3.8.3 + hooks: + - id: flake8 + additional_dependencies: + - hacking>=3.0.1,<3.1.0 diff --git a/doc/source/conf.py b/doc/source/conf.py index f8bd9f94e..922598925 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,4 +1,17 @@ # -*- coding: utf-8 -*- +# Copyright (C) 2020 Red Hat, Inc. +# +# 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. # # Configuration file for the Sphinx documentation builder. # diff --git a/doc/source/reference/index.rst b/doc/source/reference/index.rst index 97fd328b6..3ab44fd7d 100644 --- a/doc/source/reference/index.rst +++ b/doc/source/reference/index.rst @@ -16,4 +16,4 @@ Reference notification_driver notification_listener serializer - exceptions + exceptions diff --git a/oslo_messaging/tests/drivers/test_amqp_driver.py b/oslo_messaging/tests/drivers/test_amqp_driver.py index 7f39da89a..186101413 100644 --- a/oslo_messaging/tests/drivers/test_amqp_driver.py +++ b/oslo_messaging/tests/drivers/test_amqp_driver.py @@ -1208,8 +1208,8 @@ class TestLinkRecovery(_AmqpBrokerTestCase): else: # unblock all link when RPC call is made link.add_capacity(10) - for l in self._blocked_links: - l.add_capacity(10) + for li in self._blocked_links: + li.add_capacity(10) self._broker.on_receiver_active = _on_active self._broker.on_credit_exhausted = lambda link: None @@ -1293,15 +1293,15 @@ class TestAddressing(test_utils.BaseTestCase): {"msg": topic}, 2.0) expected.append(topic) - for l in rl: - l.join(timeout=30) + for li in rl: + li.join(timeout=30) # anycast will not evenly distribute an odd number of msgs predicate = lambda: len(expected) == (nl[0].messages.qsize() + nl[1].messages.qsize()) _wait_until(predicate, 30) - for l in nl: - l.kill(timeout=30) + for li in nl: + li.kill(timeout=30) s1_payload = [m.message.get('msg') for m in rl[0].get_messages()] s2_payload = [m.message.get('msg') for m in rl[1].get_messages()] diff --git a/oslo_messaging/tests/functional/utils.py b/oslo_messaging/tests/functional/utils.py index 371c170a1..b5a7a1af3 100644 --- a/oslo_messaging/tests/functional/utils.py +++ b/oslo_messaging/tests/functional/utils.py @@ -280,13 +280,13 @@ class IsValidDistributionOf(object): def match(self, actual): errors = InvalidDistribution(self.original, actual) - received = [[i for i in l] for l in actual] + received = [[idx for idx in act] for act in actual] def _remove(obj, lists): - for l in lists: - if obj in l: - front = l[0] - l.remove(obj) + for li in lists: + if obj in li: + front = li[0] + li.remove(obj) return front return None @@ -296,8 +296,8 @@ class IsValidDistributionOf(object): errors.missing += item elif item != o: errors.wrong_order.append([item, o]) - for l in received: - errors.extra += l + for li in received: + errors.extra += li return errors or None diff --git a/oslo_messaging/tests/notify/test_notifier.py b/oslo_messaging/tests/notify/test_notifier.py old mode 100755 new mode 100644 index 832ee02ed..c36a4324b --- a/oslo_messaging/tests/notify/test_notifier.py +++ b/oslo_messaging/tests/notify/test_notifier.py @@ -1,4 +1,3 @@ - # Copyright 2013 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/oslo_messaging/tests/rpc/test_client.py b/oslo_messaging/tests/rpc/test_client.py old mode 100755 new mode 100644 diff --git a/oslo_messaging/tests/rpc/test_dispatcher.py b/oslo_messaging/tests/rpc/test_dispatcher.py old mode 100755 new mode 100644 diff --git a/oslo_messaging/tests/test_transport.py b/oslo_messaging/tests/test_transport.py old mode 100755 new mode 100644 diff --git a/test-requirements.txt b/test-requirements.txt index cbed7dcf1..983c1c918 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,6 +7,7 @@ hacking>=3.0.1,<3.1.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD stestr>=2.0.0 # Apache-2.0 +pre-commit>=2.6.0 # MIT testscenarios>=0.4 # Apache-2.0/BSD testtools>=2.2.0 # MIT oslotest>=3.2.0 # Apache-2.0 diff --git a/tools/simulator.py b/tools/simulator.py index ab2529b62..8b37f50d8 100755 --- a/tools/simulator.py +++ b/tools/simulator.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 # 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 @@ -10,8 +11,12 @@ # License for the specific language governing permissions and limitations # under the License. -import eventlet # noqa -eventlet.monkey_patch() # noqa +try: + # Avoid https://github.com/PyCQA/pycodestyle/issues/472 + import eventlet + eventlet.monkey_patch() +except ImportError: + raise import argparse import bisect diff --git a/tox.ini b/tox.ini index 9646cb274..bed65f9e5 100644 --- a/tox.ini +++ b/tox.ini @@ -19,7 +19,7 @@ commands = stestr run --slowest {posargs} [testenv:pep8] commands = - flake8 + pre-commit run -a # run security linter bandit -r oslo_messaging -x tests -n5