Setup pre-commit hooks for Python code 09/92009/2
authorThanh Ha <zxiiro@gmail.com>
Mon, 10 Aug 2020 13:33:07 +0000 (09:33 -0400)
committerThanh Ha <zxiiro@gmail.com>
Mon, 10 Aug 2020 14:16:24 +0000 (10:16 -0400)
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 <zxiiro@gmail.com>
.pre-commit-config.yaml
check_jjb_version.py
check_prefix.py
check_robot.py
docs/conf.py
jjb/defaults.yaml

index f08ce2534cef9e31611b75b8ea53ac2f1f0d7488..13f206b2d62c1c744cea4b17acb6217eb631a171 100644 (file)
@@ -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:
index e7737f9348373d2d221ac5267b7b5c428642cad7..6460889a92c2ab35e495f14af20e1dc75bf25e5a 100644 (file)
@@ -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"))
index d4adcbcea69ad0f41cae3081bfa09acd881c4f4c..08f83653d98f7b34a79baaf22e66ee783baf114b 100755 (executable)
@@ -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"))
index 89e25120fb9ea7f5da8688528db22f1e7b16f68c..0dcadb865eec22b34141f27c995ce918735e58a9 100755 (executable)
@@ -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:
index 925316abfa248333bf07605e24fc4a9ad928c78e..2e70bbac2174d7071552892a0df62a22c425481a 100644 (file)
@@ -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
index ce46e1da289e2b5bf443892da2ca9ed9401ea377..0f93775a0e6870db234c9cadcc06838b18eb0b80 100644 (file)
@@ -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