From e6b876c531c59827bfdbec64a1eb2f54f7143c38 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Fri, 17 Jan 2025 17:15:59 +0100 Subject: [PATCH] githooks: Replace pre-commit tool with simple shell script It's basically impossible to run mypy with pre-commit because it will ignore the repo config and check all files changed the commit. The dbusmock templates are special and mypy fails on them so we use the mypy config to skip them. This introduces a new script which checks the python tests and installs all the required tools into a python virtual environment. It also changes to pre-commit hook to call the script if any of the files in tests changed and uses git stash push/pop to get the test a clean state of the repo. --- .githooks/check-tests-python.sh | 23 ++++++++++++++ .githooks/pre-commit | 47 ++++++++++++++++++---------- .github/workflows/Containerfile | 4 +-- .github/workflows/build-and-test.yml | 6 ++-- .github/workflows/container.yml | 2 +- .pre-commit-config.yaml | 10 ------ 6 files changed, 58 insertions(+), 34 deletions(-) create mode 100755 .githooks/check-tests-python.sh delete mode 100644 .pre-commit-config.yaml diff --git a/.githooks/check-tests-python.sh b/.githooks/check-tests-python.sh new file mode 100755 index 000000000..e09fd2f80 --- /dev/null +++ b/.githooks/check-tests-python.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -euo pipefail + +PYTHON=${PYTHON:-python3} + +HERE="$(cd "$(dirname "$0")" && pwd)" +VENV="${HERE}/.venv" + +[ -d $VENV ] || $PYTHON -m venv $VENV +source $VENV/bin/activate + + +$PYTHON -m ruff --version >/dev/null || $PYTHON -m pip install ruff +$PYTHON -m mypy --version >/dev/null || $PYTHON -m pip install mypy +$PYTHON -m pytest --version >/dev/null || $PYTHON -m pip install pytest + +cd tests + +python3 -m ruff check . || CHECK_FAILED=1 +python3 -m ruff format --check . || CHECK_FAILED=1 +mypy . || CHECK_FAILED=1 + +[ -z ${CHECK_FAILED+x} ] || exit 1 diff --git a/.githooks/pre-commit b/.githooks/pre-commit index a78365acb..65c89a208 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,20 +1,33 @@ #!/usr/bin/env bash -# File generated by pre-commit: https://pre-commit.com -# ID: 138fd403232d2ddd5efb44317e38bf03 - -# start templated -INSTALL_PYTHON=/usr/bin/python3 -ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit) -# end templated +set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" -ARGS+=(--hook-dir "$HERE" -- "$@") - -if [ -x "$INSTALL_PYTHON" ]; then - exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}" -elif command -v pre-commit > /dev/null; then - exec pre-commit "${ARGS[@]}" -else - echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2 - exit 1 -fi +CHECK_TEST_PYTHON="${HERE}/check-tests-python.sh" + +path_unchanged() +{ + ! git diff --cached --name-only | grep $1 >/dev/null +} + +check() +{ + if [ -z ${PRE_COMMIT_STASH+x} ]; then + PRE_COMMIT_STASH=1 + + clean_up () + { + git stash pop -q + } + + if ! git diff -s --exit-code; then + git stash push -q --keep-index + trap clean_up EXIT + fi + fi + + $@ +} + +path_unchanged ^tests/ || check $CHECK_TEST_PYTHON || CHECK_FAILED=1 + +[ -z ${CHECK_FAILED+x} ] || exit 1 diff --git a/.github/workflows/Containerfile b/.github/workflows/Containerfile index ad7e899dc..ff33cd2f5 100644 --- a/.github/workflows/Containerfile +++ b/.github/workflows/Containerfile @@ -61,8 +61,8 @@ RUN apt install -y --no-install-recommends python3-pip RUN pip install --user --break-system-packages furo">=2024.04.27" \ sphinx-copybutton sphinxext-opengraph matplotlib -# Install pre-commit -RUN pip install --user --break-system-packages pre-commit +# Install venv for githook checks +RUN apt install -y --no-install-recommends python3-venv # Install qdbusxml2cpp RUN apt install -y --no-install-recommends qt6-base-dev-tools diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 652d5f167..11ca3fa3e 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -38,11 +38,9 @@ jobs: - name: Check out xdg-desktop-portal uses: actions/checkout@v4 - - name: Run pre-commit hooks + - name: Run checks run: | - export PYTHONPATH="/root/.local/lib/python$(python3 -c 'import sys; print("{}.{}".format(*sys.version_info))')/site-packages:$PYTHONPATH" - export PATH="/root/.local/bin:$PATH" - pre-commit run --show-diff-on-failure --color=always --all-files + .githooks/check-tests-python.sh - name: Check POTFILES.in run: .github/workflows/check-potfiles.sh diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 7ecf61d3b..693691387 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -1,5 +1,5 @@ env: - IMAGE_TAG: 20241211-1 + IMAGE_TAG: 2025-01-17.1 on: workflow_call: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 9752aedb3..000000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,10 +0,0 @@ -repos: -- repo: https://github.com/astral-sh/ruff-pre-commit - # Ruff version. - rev: v0.7.0 - hooks: - # Run the linter. - - id: ruff - # Run the formatter. - - id: ruff-format - args: [ --check ]