From 83fc5344248d56a42bbcd3eb9a8bb02754c37a36 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Sat, 8 Aug 2020 15:32:43 -0400 Subject: [PATCH] Migrate all Python linters to pre-commit Switch Python related Coala linters over to pre-commit hooks. We would like to migrate off of the Coala platform as it has not had a release in years. Issue: RELENG-2642 Signed-off-by: Thanh Ha Change-Id: I1a20839a070b9923f72ac86317b8b5d9889ebe9f --- .coafile | 9 --------- .pre-commit-config.yaml | 26 ++++++++++++++++++++++++++ check-best-practices.py | 24 ++++++++++++------------ docs/conf.py | 35 ++++++++++++++++++++--------------- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/.coafile b/.coafile index b7909cd..38f6f36 100644 --- a/.coafile +++ b/.coafile @@ -22,12 +22,3 @@ files = docs/**/*.rst bears = MarkdownBear,SpaceConsistencyBear,WriteGoodLintBear files = **.md, **.markdown use_spaces = true - -[all.Python] -bears = BanditBear, - PEP8Bear, - PyCommentedCodeBear, - PyDocStyleBear, - PyFlakesBear, - PyImportSortBear -files = *.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aeafb5d..507970b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,7 @@ --- +default_language_version: + python: python3 + repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.1.0 @@ -12,3 +15,26 @@ repos: rev: v0.13.1 hooks: - id: gitlint + + - repo: https://github.com/ambv/black + rev: stable + hooks: + - id: black + + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.3 + hooks: + - id: flake8 + args: ["--max-line-length=88"] + + - repo: https://github.com/pycqa/bandit + rev: 1.6.2 + hooks: + - id: bandit + # Bandit does not need to run on test code + exclude: tests/* + + - repo: https://github.com/pycqa/pydocstyle + rev: 5.0.2 + hooks: + - id: pydocstyle diff --git a/check-best-practices.py b/check-best-practices.py index 97ac5fd..c22b5bb 100644 --- a/check-best-practices.py +++ b/check-best-practices.py @@ -10,7 +10,7 @@ ############################################################################## """Scan documentation for bad practices.""" -__author__ = 'Thanh Ha' +__author__ = "Thanh Ha" import fnmatch @@ -27,19 +27,19 @@ def check_sudo_pip(filename): counter = 0 print("Scanning {}".format(filename)) - with open(filename, 'r') as _file: + with open(filename, "r") as _file: for num, line in enumerate(_file, 1): - if re.search('sudo pip', line): + if re.search("sudo pip", line): counter += 1 - print('{}: {}'.format(num, line)) + print("{}: {}".format(num, line)) if counter: - print('ERROR: pip should never be used as a sudo command.') - print('Consider one of the following solutions:') - print('1. Use a virtualenv') - print(' (https://virtualenv.pypa.io/en/stable/)') - print('2. Use PEP370 instead via pip\'s --user parameter.') - print(' (https://www.python.org/dev/peps/pep-0370/)') + print("ERROR: pip should never be used as a sudo command.") + print("Consider one of the following solutions:") + print("1. Use a virtualenv") + print(" (https://virtualenv.pypa.io/en/stable/)") + print("2. Use PEP370 instead via pip's --user parameter.") + print(" (https://www.python.org/dev/peps/pep-0370/)") return True return False @@ -47,8 +47,8 @@ def check_sudo_pip(filename): if __name__ == "__main__": counter = 0 - for root, dirnames, filenames in os.walk('docs'): - for filename in fnmatch.filter(filenames, '*.rst'): + for root, dirnames, filenames in os.walk("docs"): + for filename in fnmatch.filter(filenames, "*.rst"): if check_sudo_pip(os.path.join(root, filename)): counter += 1 diff --git a/docs/conf.py b/docs/conf.py index 8367072..e01ef1a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Linux Foundation Release Engineering Tools documentation build configuration file, created by -# sphinx-quickstart on Sat Mar 4 12:20:05 2017. +# Linux Foundation Release Engineering Tools documentation build configuration +# file, created by sphinx-quickstart on Sat Mar 4 12:20:05 2017. # # This file is execfile()d with the current directory set to its # containing dir. @@ -16,25 +16,30 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -# +"""Configuration file for Sphinx.""" -from docs_conf.conf import * +from docs_conf.conf import * # noqa -intersphinx_mapping['ansible'] = ('https://docs.ansible.com/ansible/latest/', None) +intersphinx_mapping["ansible"] = ( # noqa + "https://docs.ansible.com/ansible/latest/", + None, +) linkcheck_ignore = [ - 'https://gerrit.linuxfoundation.org/infra/releng/docs-conf', - 'https://gerrit.linuxfoundation.org/infra/#/settings/http-password', - 'https://jenkins.acumos.org.*', - 'https://build.automotivelinux.org.*', - 'https://build.automotivelinux.org/sandbox', - 'https://.*.example.org.*', - 'https://git.opendaylight.org/gerrit/#/settings/gpg-keys', - 'https://wiki.debian.org/meetbot', # SNI link needs Python 2.7.9+ - 'https://iotivity.biterg.io' + "https://gerrit.linuxfoundation.org/infra/releng/docs-conf", + "https://gerrit.linuxfoundation.org/infra/#/settings/http-password", + "https://jenkins.acumos.org.*", + "https://build.automotivelinux.org.*", + "https://build.automotivelinux.org/sandbox", + "https://.*.example.org.*", + "https://git.opendaylight.org/gerrit/#/settings/gpg-keys", + "https://wiki.debian.org/meetbot", # SNI link needs Python 2.7.9+ + "https://iotivity.biterg.io", ] -sphinx_tabs_valid_builders = ['linkcheck'] +sphinx_tabs_valid_builders = ["linkcheck"] + def setup(app): + """Injects the report issue ribbon.""" app.add_stylesheet("css/ribbon.css") -- 2.16.6