diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 78a5c7fc7a..61ea4be128 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,10 +7,19 @@ repos: - id: check-yaml - id: check-added-large-files - id: check-ast - exclude: >- + exclude: &check_ast_exclusion >- (?x)^( tools/autotune_grid/xyz_to_vab/pyratemp.py| )$ + - id: check-ast + name: Check python ast (with Python 2.7) + language_version: python2.7 + stages: [manual] + - id: check-ast + name: Check python ast (with Python 3) + exclude: *check_ast_exclusion + language_version: python3 + stages: [manual] - repo: local hooks: - id: prettify diff --git a/tools/docker/scripts/install_python.sh b/tools/docker/scripts/install_python.sh index 419ea0c851..5e5ad12526 100755 --- a/tools/docker/scripts/install_python.sh +++ b/tools/docker/scripts/install_python.sh @@ -7,6 +7,8 @@ apt-get update -qq apt-get install -qq --no-install-recommends \ python \ python3 \ + libpython-stdlib \ + libpython3-stdlib \ python-pip \ python3-pip \ python-wheel \ @@ -20,7 +22,7 @@ rm -rf /var/lib/apt/lists/* # install python packages pip install --quiet numpy matplotlib requests -pip3 install --quiet numpy matplotlib requests +pip3 install --quiet numpy matplotlib requests pre-commit # install python2.6 echo "Building Python-2.6.9... " diff --git a/tools/docker/scripts/test_python.sh b/tools/docker/scripts/test_python.sh index 3cde8b6f40..aaa4eea9b3 100755 --- a/tools/docker/scripts/test_python.sh +++ b/tools/docker/scripts/test_python.sh @@ -16,11 +16,11 @@ function run_tests { echo "Skipping $i - it's Python3 only." continue fi - echo "Selftesting $i" + echo "Running $i" cd "$(dirname "$i")" SCRIPT=$(basename "$i") if ! $PYTHON -B "./${SCRIPT}" ; then - echo "Selftest of $i failed" + echo "Test $i failed" ERRORS=$((ERRORS+1)) fi set -e #re-enable error trapping @@ -28,6 +28,15 @@ function run_tests { done } +function run_pre_commit { + set +e #disable error trapping + if ! pre-commit run --all-files --hook-stage manual check-ast ; then + echo "Syntax check failed for either Python 2 or 3" + ERRORS=$((ERRORS+1)) + fi + set -e #re-enable error trapping +} + #=============================================================================== ERRORS=0 @@ -46,8 +55,11 @@ run_tests python2.7 "${ALL_TEST_SCRIPTS}" # python 3.x run_tests python3 "${ALL_TEST_SCRIPTS}" +# run manual pre-commit checks +run_pre_commit + # print report -NUM_TEST_SCRIPTS=$(echo "${ALL_TEST_SCRIPTS}" | wc -w) +NUM_TEST_SCRIPTS=$(( $(wc -w <<< "${ALL_TEST_SCRIPTS}") + 1 )) echo "" echo "Summary: Run ${NUM_TEST_SCRIPTS} Python test scripts, found ${ERRORS} errors." if [ $ERRORS == 0 ]; then