diff --git a/pyproject.toml b/pyproject.toml index d40b6ff..60c04d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,6 +108,7 @@ types-setuptools = "^65.7.0" types-tabulate = "^0.9.0.1" types-tqdm = "^4.65.0.1" typos = "^1.22.0" +yq = "^3.4.3" [tool.poetry.extras] examples = [ @@ -298,6 +299,9 @@ shell = "pyproject-fmt --check ." [tool.poe.tasks.typecheck] shell = "mypy ." +[tool.poe.tasks.check_pre_commit_consistency] +shell = "./scripts/check_pre_commit_consistency.sh" + [tool.poe.tasks.check_api_models] shell = "./scripts/api_models.py check" @@ -312,6 +316,7 @@ shell = "typos ." [tool.poe.tasks] lint = [ + "check_pre_commit_consistency", "check_api_models", "docstring_coverage", "ruff_check", diff --git a/scripts/check_pre_commit_consistency.sh b/scripts/check_pre_commit_consistency.sh new file mode 100755 index 0000000..9dc4252 --- /dev/null +++ b/scripts/check_pre_commit_consistency.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +# (C) Copyright Alpine Quantum Technologies GmbH 2024 +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE.txt file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +# Check consistency between versions of the pre-commit hooks +# and the packages installed by poetry. + +set -euo pipefail + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +# Retrieve the version of a Python package installed by Poetry. +installed_package_version() { + package_name="$1" + # FIXME: install poetry-export plugin to not need to silence the warning here. + package_version=$(poetry export \ + --only dev \ + --format requirements.txt \ + --without-hashes \ + --without-urls 2> /dev/null | \ + sed -nr "s/$package_name==([0-9][0-9.]*).*/\1/p") + echo "$package_version" +} + +# Retrieve the version of a pre-commit hook. +pre_commit_hook_version() { + tool_name="$1" + config_path="$SCRIPT_DIR/../.pre-commit-config.yaml" + hook_version=$(yq -r ".repos[] | select(.repo | test(\"$tool_name\")).rev" "$config_path") + hook_version=${hook_version#v} + echo "$hook_version" +} + +tools=(ruff typos pyproject-fmt interrogate) +exit_code=0 + +for tool in "${tools[@]}"; do + package=$(installed_package_version "$tool") + hook=$(pre_commit_hook_version "$tool") + echo -n "$tool: package=$package hook=$hook " + if [ -n "$package" ] && [ -n "$hook" ] && [ "$package" = "$hook" ]; then + echo " OK" + else + echo " FAIL" + exit_code=1 + fi +done + +exit "$exit_code"