From c8362308bbb3cdff371cab923ea54edb0f1c2d9a Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Mon, 10 Aug 2020 09:33:07 -0400 Subject: [PATCH 1/1] Setup pre-commit hooks for Python code This sets up pre-commit hooks for the Python projects that are in this repo. This also resolves any lint issues in existing files. Change-Id: If5f8fba811759f6cbe44f04a1fbf1ce400a2ead7 Signed-off-by: Thanh Ha --- .pre-commit-config.yaml | 26 +++++++++++++++++++++++--- check_jjb_version.py | 23 ++++++++++++----------- check_prefix.py | 17 +++++++++-------- check_robot.py | 21 +++++++++++---------- docs/conf.py | 4 ++-- jjb/defaults.yaml | 2 +- 6 files changed, 58 insertions(+), 35 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f08ce2534..13f206b2d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,9 +4,6 @@ repos: rev: v2.5.0 hooks: - id: check-json - - id: flake8 - args: ['--max-line-length=88'] - exclude: docs/* - id: trailing-whitespace - repo: https://github.com/jorisroovers/gitlint @@ -24,6 +21,29 @@ repos: jenkins-config/.* )$ + - 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 + - repo: https://github.com/adrienverge/yamllint rev: v1.23.0 hooks: diff --git a/check_jjb_version.py b/check_jjb_version.py index e7737f934..6460889a9 100644 --- a/check_jjb_version.py +++ b/check_jjb_version.py @@ -9,7 +9,7 @@ ############################################################################## """Ensures that the jjb-version in tox and releng-jobs.yaml match.""" -__author__ = 'Thanh Ha' +__author__ = "Thanh Ha" import os @@ -18,25 +18,26 @@ import sys def check_jjb_version(tox_file, releng_jobs_file): - with open(tox_file, 'r') as _file: + """Check that JJB version matches in job cfg and tox.ini.""" + with open(tox_file, "r") as _file: for num, line in enumerate(_file, 1): - if re.search('env:JJB_VERSION:', line): - jjb_version_tox = line.rsplit(':', 1)[1].replace('}', '').strip() + if re.search("env:JJB_VERSION:", line): + jjb_version_tox = line.rsplit(":", 1)[1].replace("}", "").strip() break - with open(releng_jobs_file, 'r') as _file: + with open(releng_jobs_file, "r") as _file: for num, line in enumerate(_file, 1): - if re.search('jjb-version: ', line): - jjb_version = line.rsplit(':', 1)[1].strip() + if re.search("jjb-version: ", line): + jjb_version = line.rsplit(":", 1)[1].strip() break - print('JJB version in jjb/releng-jobs.yaml: {}'.format(jjb_version)) - print('JJB version in tox.ini: {}'.format(jjb_version_tox)) + print("JJB version in jjb/releng-jobs.yaml: {}".format(jjb_version)) + print("JJB version in tox.ini: {}".format(jjb_version_tox)) if jjb_version != jjb_version_tox: - print('ERROR: JJB version in jjb/releng-jobs.yaml and tox.ini MUST match.') + print("ERROR: JJB version in jjb/releng-jobs.yaml and tox.ini MUST match.") sys.exit(1) if __name__ == "__main__": - check_jjb_version('tox.ini', os.path.join('jjb', 'releng-jobs.yaml')) + check_jjb_version("tox.ini", os.path.join("jjb", "releng-jobs.yaml")) diff --git a/check_prefix.py b/check_prefix.py index d4adcbcea..08f83653d 100755 --- a/check_prefix.py +++ b/check_prefix.py @@ -12,7 +12,7 @@ The production prefix MUST always be a blank string. """ -__author__ = 'Thanh Ha' +__author__ = "Thanh Ha" import os @@ -21,17 +21,18 @@ import sys def check_prefix(filename): - with open(filename, 'r') as _file: + """Check if a prefix was checked into this repo.""" + with open(filename, "r") as _file: for num, line in enumerate(_file, 1): - if re.search('prefix:', line): - if "''" not in line: + if re.search("prefix:", line): + if '""' not in line: print( - 'ERROR: A non-blank prefix is defined in ' - 'jjb/defaults.yaml. The prefix MUST be set to blank ' - '\'\' in production!' + "ERROR: A non-blank prefix is defined in " + "jjb/defaults.yaml. The prefix MUST be set to blank " + '"" in production!' ) sys.exit(1) if __name__ == "__main__": - check_prefix(os.path.join('jjb', 'defaults.yaml')) + check_prefix(os.path.join("jjb", "defaults.yaml")) diff --git a/check_robot.py b/check_robot.py index 89e25120f..0dcadb865 100755 --- a/check_robot.py +++ b/check_robot.py @@ -7,13 +7,14 @@ # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v10.html ############################################################################## -# Ensures that we are only ever using one robot system -# -# Due to the way the Jenkins OpenStack Cloud plugin works we can only limit -# max parallel robot systems by the VM. So having multiple VM types makes it -# very difficult for us to properly limit the amount of parallel robot runs. +"""Ensures that we are only ever using one robot system. + +Due to the way the Jenkins OpenStack Cloud plugin works we can only limit +max parallel robot systems by the VM. So having multiple VM types makes it +very difficult for us to properly limit the amount of parallel robot runs. +""" -__author__ = 'Thanh Ha' +__author__ = "Thanh Ha" import fnmatch @@ -29,9 +30,9 @@ def get_robot_systems(filename): """ robots = set() - with open(filename, 'r') as _file: + with open(filename, "r") as _file: for num, line in enumerate(_file, 1): - if re.search('centos7-robot', line): + if re.search("centos7-robot", line): robots.add(line.rsplit(maxsplit=1)[1]) return robots @@ -39,8 +40,8 @@ def get_robot_systems(filename): if __name__ == "__main__": robots = [] - for root, dirnames, filenames in os.walk('jjb'): - for filename in fnmatch.filter(filenames, '*.yaml'): + for root, dirnames, filenames in os.walk("jjb"): + for filename in fnmatch.filter(filenames, "*.yaml"): robots += get_robot_systems(os.path.join(root, filename)) if len(robots) > 1: diff --git a/docs/conf.py b/docs/conf.py index 925316abf..2e70bbac2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -9,6 +9,6 @@ # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v10.html ############################################################################## +"""Configuration for Sphinx.""" -from docs_conf.conf import * - +from docs_conf.conf import * # noqa diff --git a/jjb/defaults.yaml b/jjb/defaults.yaml index ce46e1da2..0f93775a0 100644 --- a/jjb/defaults.yaml +++ b/jjb/defaults.yaml @@ -3,7 +3,7 @@ - defaults: name: global - prefix: '' # A prefix for job names sandbox for example: user- + prefix: "" # A prefix for job names sandbox for example: user- karaf-version: karaf4 build-days-to-keep: 30 -- 2.36.6