From ff204a8e6371678d855a5aad86ac722eee0c8163 Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 12:40:11 -0800 Subject: [PATCH 01/10] git: adding pre-commit configuration file Signed-off-by: Lee, Kin Long Kelvin --- .pre-commit-config.yaml | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f037426 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,53 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-json + - id: pretty-format-json + - id: debug-statements + - repo: https://github.com/asottile/setup-cfg-fmt + rev: v2.4.0 + hooks: + - id: setup-cfg-fmt + - repo: https://github.com/asottile/reorder-python-imports + rev: v3.10.0 + hooks: + - id: reorder-python-imports + args: [--py38-plus, --add-import, "from __future__ import annotations"] + - repo: https://github.com/asottile/add-trailing-comma + rev: v3.0.1 + hooks: + - id: add-trailing-comma + - repo: https://github.com/asottile/pyupgrade + rev: v3.10.1 + hooks: + - id: pyupgrade + args: [--py39-plus] + - repo: https://github.com/PyCQA/flake8 + rev: 6.1.0 + hooks: + - id: flake8 + additional_dependencies: + [ + flake8-bandit, + flake8-black, + flake8-debugger, + flake8-print, + flake8-use-fstring, + ] + - repo: https://github.com/pycqa/isort + rev: 5.12.0 + hooks: + - id: isort + name: isort (python) + - repo: https://github.com/psf/black + rev: 23.7.0 + hooks: + - id: black + language_version: python3.9 + - repo: https://github.com/MarcoGorelli/absolufy-imports + rev: v0.3.1 + hooks: + - id: absolufy-imports From 36a1a5b6d85124f1a293241ffbd73b2f18f55dfd Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 12:41:19 -0800 Subject: [PATCH 02/10] deps: adding requirements file for development Since `setup.py` doesn't have a good mechanism for differentiating between production and development dependencies, including this file as a way for contributors Signed-off-by: Lee, Kin Long Kelvin --- requirements_dev.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements_dev.txt diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..959c060 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1 @@ +pre-commit==3.5.0 From 01d24f52ff22fa6e0a49fc33f101d92ae4e0ba54 Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 12:45:50 -0800 Subject: [PATCH 03/10] script: removing obsolete git hook installation script Signed-off-by: Lee, Kin Long Kelvin --- setup_git_hooks.sh | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100755 setup_git_hooks.sh diff --git a/setup_git_hooks.sh b/setup_git_hooks.sh deleted file mode 100755 index 1b8f148..0000000 --- a/setup_git_hooks.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# Setup git hooks for style formatting and testing -echo " This script will help set up the git hooks for Code Base Investigator" -echo -echo " The hooks are configured using git config variables." -echo " Below are a description of the variables, and their possible values." -echo " hooks.autopep8 = {never, always}" -echo " - never = autopep8 is never run prior to creating a commit." -echo " - always = autopep8 is always run prior to creating a commit." -echo " hooks.pylint = {never, relaxed, strict}" -echo " - never = pylint is never run prior to creating a commit." -echo " - relaxed = pylint is always run prior to creating a commit, but errors do not prevent commits." -echo " - strict = pylint is always run prior to creating a commit, and errors prevent commits." -echo " hooks.tests = {never, always}" -echo " - never = unit tests are never run after creating a commit." -echo " - always = unit tests are always run after creating a commit." -echo -echo " The default settings are:" -echo " hooks.autopep8 = never" -echo " hooks.pylint = never" -echo " hooks.tests = never" -echo -echo " To change these use:" -echo " git config --replace-all hooks.autopep8 [value]" -echo " git config --replace-all hooks.pylint [value]" -echo " git config --replace-all hooks.tests [value]" - -git config --replace-all hooks.autopep8 never -git config --replace-all hooks.pylint never -git config --replace-all hooks.tests never - -ROOT=`git rev-parse --show-toplevel` - -cp ${ROOT}/hooks/* ${ROOT}/.git/hooks/. From a5e17e77f84a391a4943f203f46dbf941634dfc6 Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 12:46:18 -0800 Subject: [PATCH 04/10] script: removing previous git hook scripts Signed-off-by: Lee, Kin Long Kelvin --- hooks/post-commit | 16 --------- hooks/pre-commit | 92 ----------------------------------------------- 2 files changed, 108 deletions(-) delete mode 100755 hooks/post-commit delete mode 100755 hooks/pre-commit diff --git a/hooks/post-commit b/hooks/post-commit deleted file mode 100755 index 8a8bb74..0000000 --- a/hooks/post-commit +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -CONF_VAL=`git config --get hooks.tests` - -if [ $? != 0 ]; then - echo "WARNING: hooks.tests not defined in your git config settings." - CONF_VAL="never" -fi - -if [ "$CONF_VAL" == "never" ]; then - exit 0 -else - ROOT=`git rev-parse --show-toplevel` - cd $ROOT - python3 -m unittest discover ./tests/ -fi diff --git a/hooks/pre-commit b/hooks/pre-commit deleted file mode 100755 index 5fb83bb..0000000 --- a/hooks/pre-commit +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/bash - -run_pylint() { - # Runs pylint on the input list of files - # based on the configured settings in git - VER_REC="2.3.1" - CONF_VAL=`git config --get hooks.pylint` - - if [ $? != 0 ]; then - echo "WARNING: hooks.pylint not defined in your git config settings." - CONF_VAL="never" - fi - - # If we aren't going to run pylint, don't bother checking for it. - if [ "$CONF_VAL" == "never" ]; then - return 0 - fi - - # Make sure pylint is in our path - PYL=`which pylint` - if [ "$PYL" == "" ]; then - if [ "$CONF_VAL" == "relaxed"]; then - echo "No pylint found in your path. Skipping pylint." - return 0 - elif [ "$CONF_VAL" == "strict" ]; then - echo "No pylint found in your path. This will prevent a commit because hooks.pylint = strict" - exit 1 - fi - fi - - # Check the version of pylint - PYL_VER=`pylint --version | grep pylint | awk '{print $2}'` - if [ "$PYL_VER" != "$VER_REC" ]; then - echo "WARNING: Your pylint version (${PYL_VER}) differs from the recommended version (${VER_REC})" - fi - - if [ "$CONF_VAL" == "relaxed" ]; then - pylint -rn -sn $1 - elif [ "$CONF_VAL" == "strict" ]; then - pylint -rn -sn $1 - if [ $? != 0 ]; then - echo "ERROR: This commit will not be created until you remedy the issues identified by pylint." - exit 1 - fi - fi - return 0 -} - -run_autopep8() { - VER_REC="1.4.4" - CONF_VAL=`git config --get hooks.autopep8` - - if [ $? != 0 ]; then - echo "WARNING: hooks.autopep8 not defined in your git config settings." - CONF_VAL="never" - fi - - # If we aren't going to run autopep8, skip the checks - if [ "$CONF_VAL" == "never" ]; then - return 0 - fi - - # Ensure autopep8 is in our path - AP8=`which autopep8` - if [ "$AP8" == "" ]; then - if [ "$CONF_VAL" == "always" ]; then - echo "No autopep8 found in your path. This will prevent a commit because hooks.autopep8 = always" - exit 1 - fi - fi - - AP8_VER=`autopep8 --version | awk '{print $2}'` - if [ "$AP8_VER" != "$VER_REC" ]; then - echo "WARNING: Your autopep8 version (${AP8_VER}) differs from the recommended version (${VER_REC})" - fi - - if [ "$CONF_VAL" == "always" ]; then - autopep8 --in-place --aggressive --aggressive $1 - fi - - return 0 -} - -# Get modified files list, but limit it to only python files -FILES=`git status --short -uno *.py **/*.py | awk '{print $2}'` - -if [ "${FILES}" == "" ]; then - exit 0 -fi - -run_autopep8 "$FILES" -run_pylint "$FILES" From c1d2ae059583acdf6385dea3ca5923136f2749b0 Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 12:55:09 -0800 Subject: [PATCH 05/10] git: adding bandit to pre-commit workflow Signed-off-by: Lee, Kin Long Kelvin --- .pre-commit-config.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f037426..28b5fa9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -51,3 +51,12 @@ repos: rev: v0.3.1 hooks: - id: absolufy-imports + - repo: https://github.com/PyCQA/bandit + rev: 1.7.5 + hooks: + - id: bandit + name: bandit + language: python + language_version: python3 + types: [python3] + require_serial: true From a9bd58674f8b0362bea3787e89774f10b3589017 Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 13:18:29 -0800 Subject: [PATCH 06/10] git: removing redundant python import sorting Signed-off-by: Lee, Kin Long Kelvin --- .pre-commit-config.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 28b5fa9..768f7b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,11 +11,6 @@ repos: rev: v2.4.0 hooks: - id: setup-cfg-fmt - - repo: https://github.com/asottile/reorder-python-imports - rev: v3.10.0 - hooks: - - id: reorder-python-imports - args: [--py38-plus, --add-import, "from __future__ import annotations"] - repo: https://github.com/asottile/add-trailing-comma rev: v3.0.1 hooks: From ec9c5f6e264d8144549f62528de8502794e995dc Mon Sep 17 00:00:00 2001 From: "Lee, Kin Long Kelvin" Date: Thu, 30 Nov 2023 13:25:55 -0800 Subject: [PATCH 07/10] fix: not pinning black to python 3.9 Signed-off-by: Lee, Kin Long Kelvin --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 768f7b4..3dcccab 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,7 +41,7 @@ repos: rev: 23.7.0 hooks: - id: black - language_version: python3.9 + language_version: python - repo: https://github.com/MarcoGorelli/absolufy-imports rev: v0.3.1 hooks: From 2c28a4d629988d8c28f36ed05508fd24e653663c Mon Sep 17 00:00:00 2001 From: Kin Long Kelvin Lee Date: Fri, 1 Dec 2023 09:39:04 -0800 Subject: [PATCH 08/10] deps: removing requirements file for future toml port Signed-off-by: Kin Long Kelvin Lee --- requirements_dev.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 requirements_dev.txt diff --git a/requirements_dev.txt b/requirements_dev.txt deleted file mode 100644 index 959c060..0000000 --- a/requirements_dev.txt +++ /dev/null @@ -1 +0,0 @@ -pre-commit==3.5.0 From 24728d01b91a36f736bdb802251d946bea5213d5 Mon Sep 17 00:00:00 2001 From: Kin Long Kelvin Lee Date: Fri, 1 Dec 2023 10:20:30 -0800 Subject: [PATCH 09/10] docs: updating contributing docs with pre-commit change Signed-off-by: Kin Long Kelvin Lee --- CONTRIBUTING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 10429b5..3b4d67e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,4 +28,6 @@ A number of [Git hooks](./hooks) are available to assist contributors in adherin - post-commit: Run the full test suite after every commit. -See the [setup_git_hooks.sh script](./setup_git_hooks.sh) for more information on configuring these hooks. +We use `pre-commit` to manage commit workflows to ensure uniform code style, linting, and testing. +As part of getting started, install `pre-commit==3.5.0` and run `pre-commit install` which will +create virtual environments for development packages and install `git commit` hooks. From 61d979d6c2228edd07835a02896e6ee20cf5a6a9 Mon Sep 17 00:00:00 2001 From: Kin Long Kelvin Lee Date: Fri, 1 Dec 2023 11:25:17 -0800 Subject: [PATCH 10/10] docs: updating line length spec and removing pylint mentions Signed-off-by: Kin Long Kelvin Lee --- CONTRIBUTING.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3b4d67e..8eff614 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,21 +12,16 @@ Thanks for taking the time to contribute to Code Base Investigator! ## Submitting a Patch - Follow the style guidelines for all commits and code: - Limit the first line of Git commit messages to 50 characters, and other lines to 72 characters. - - Follow [PEP8](https://www.python.org/dev/peps/pep-0008/) for Python code, with a line limit of 99 characters and a comment/docstring limit of 72 characters. + - Follow [PEP8](https://www.python.org/dev/peps/pep-0008/) for Python code, with a line limit of 79 characters and a comment/docstring limit of 72 characters. `black` is part of the `pre-commit` hooks, and if installed should automatically format your code. - Ensure that the patched code passes all existing tests: ``` python3 -m unittest ``` -- Consider checking the quality of your code with [`pylint`](https://github.com/PyCQA/pylint). +- Please consider linting your code with `flake8`; alongside `black` for style, it is configured to be part of the `pre-commit` workflow. - If your patch introduces a new feature, consider adding a new test. - Open a new GitHub pull request with the patch. ### Hooks -A number of [Git hooks](./hooks) are available to assist contributors in adhering to the guidelines above: -- pre-commit: - Optionally run [`pylint`](https://github.com/PyCQA/pylint) and/or [`autopep8`](https://github.com/hhatto/autopep8) on every commit. -- post-commit: - Run the full test suite after every commit. We use `pre-commit` to manage commit workflows to ensure uniform code style, linting, and testing. As part of getting started, install `pre-commit==3.5.0` and run `pre-commit install` which will