diff --git a/dev-scripts/check-all b/dev-scripts/check-all index 08f8d7485..66b0c3a1f 100755 --- a/dev-scripts/check-all +++ b/dev-scripts/check-all @@ -14,7 +14,6 @@ set -x set -u ./dev-scripts/check-bash -./dev-scripts/check-for-init-files ./dev-scripts/check-javascript ./dev-scripts/check-privilege-guard ./dev-scripts/check-python diff --git a/dev-scripts/check-for-init-py-files b/dev-scripts/check-for-init-py-files deleted file mode 100755 index 0851578d8..000000000 --- a/dev-scripts/check-for-init-py-files +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -# -# Test if __init__.py file exists in directory containing .py files. - -# Exit on first failure. -set -e -# Exit on unset variable. -set -u - -success=0 - -while read -r directory; do - if [[ ! -f "${directory}/__init__.py" ]]; then - printf "Directory missing __init__.py file: %s\n" "${directory}" >&2 - success=255 - fi -done < <( - find . \ - -type f \ - -name '*.py' \ - -not -path "./venv/*" \ - -not -path "./.git/*" \ - -exec dirname {} \; \ - | sort --unique -) - -exit "${success}" diff --git a/dev-scripts/check-python b/dev-scripts/check-python index b07c3367d..e28d901ba 100755 --- a/dev-scripts/check-python +++ b/dev-scripts/check-python @@ -11,44 +11,56 @@ set -x # Exit on unset variable. set -u -# Location of app source files. -SOURCE_DIR="app" - -ADDITIONAL_PY_SCRIPTS=() -ADDITIONAL_PY_SCRIPTS+=("scripts/render-template") -ADDITIONAL_PY_SCRIPTS+=("scripts/update-service") - -# Location of virtualenv. -VIRTUALENV_DIR=venv - -./dev-scripts/check-for-init-py-files - # Delete pyc files from previous builds. find . \ - -name "*.pyc" \ + -name '*.pyc' \ -type f \ - -not -path "./${VIRTUALENV_DIR}/*" \ + -not -path './venv/*' \ -delete # Run unit tests and calculate code coverage. # Load module `app.log` to initialize our custom logger. coverage run \ - --source "$SOURCE_DIR" \ - --omit "*_test.py" \ + --source app/ \ + --omit '*_test.py' \ --module \ - unittest discover --pattern "*_test.py" \ + unittest discover --pattern '*_test.py' \ app.log coverage report +# Check for Python init files. +INIT_FILES_OK=true +while read -r directory; do + if [[ ! -f "${directory}/__init__.py" ]]; then + printf "Directory missing __init__.py file: %s\n" "${directory}" >&2 + INIT_FILES_OK=false + fi +done < <( + find . \ + -type f \ + -name '*.py' \ + -not -path './venv/*' \ + -not -path './.git/*' \ + -exec dirname {} \; \ + | sort --unique +) +"${INIT_FILES_OK}" || exit 1 + # Check that source has correct formatting. yapf --diff --recursive ./ +# Gather names of all Python scripts in the scripts/ folder. +PYTHON_SCRIPTS=() +mapfile -t PYTHON_SCRIPTS < <(grep -rl '^#!.*python' scripts/) +readonly PYTHON_SCRIPTS + # Run static analysis for Python bugs/cruft. ruff check \ - "${SOURCE_DIR}/" \ - "${ADDITIONAL_PY_SCRIPTS[@]}" + app/ \ + "${PYTHON_SCRIPTS[@]}" # Check for other style violations. -PYTHONPATH="${SOURCE_DIR}" \ +PYTHONPATH='app/' \ pylint \ - "${SOURCE_DIR}" "${ADDITIONAL_PY_SCRIPTS[@]}" + app/ \ + "${PYTHON_SCRIPTS[@]}"