diff --git a/.ci/ansible/build_container.yaml b/.ci/ansible/build_container.yaml index c380b430a55..93ce3ebbfba 100644 --- a/.ci/ansible/build_container.yaml +++ b/.ci/ansible/build_container.yaml @@ -23,5 +23,5 @@ - name: "Clean image cache" docker_prune: - images : true + images: true ... diff --git a/.ci/scripts/check_bump.sh b/.ci/scripts/check_bump.sh new file mode 100755 index 00000000000..d77132982fe --- /dev/null +++ b/.ci/scripts/check_bump.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# WARNING: DO NOT EDIT! +# +# This file was generated by plugin_template, and is managed by it. Please use +# './plugin-template --github pulpcore' to update this file. +# +# For more info visit https://github.com/pulp/plugin_template + +set -eux +bump-my-version bump release --dry-run --allow-dirty +bump-my-version show-bump diff --git a/.ci/scripts/check_gettext.sh b/.ci/scripts/check_gettext.sh index d11ff4f5041..d3753f3e426 100755 --- a/.ci/scripts/check_gettext.sh +++ b/.ci/scripts/check_gettext.sh @@ -10,12 +10,25 @@ # make sure this script runs at the repo root cd "$(dirname "$(realpath -e "$0")")"/../.. -set -uv +set -u -MATCHES=$(grep -n -r --include \*.py "_(f") +PATTERN="_(f[\"\']" -if [ $? -ne 1 ]; then +if [ $# -gt 0 ]; then + # check files provided via arguments + RESULT=$(grep -n "$PATTERN" "$@") + EXIT_CODE=$? +else + # original behavior (check all) + set -v + RESULT=$(grep -n -r --include \*.py "$PATTERN") + EXIT_CODE=$? +fi + +# grep returns 1 if it doesn't find a match +if [ $EXIT_CODE -eq 0 ]; then printf "\nERROR: Detected mix of f-strings and gettext:\n" - echo "$MATCHES" + echo "$RESULT" exit 1 fi +exit 0 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3c446189145..7f24996065f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,41 +31,22 @@ jobs: - name: "Install python dependencies" run: | echo ::group::PYDEPS - pip install -r lint_requirements.txt + pip install pre-commit echo ::endgroup:: - - name: "Lint workflow files" - run: | - yamllint -s -d '{extends: relaxed, rules: {line-length: disable}}' .github/workflows - - - name: "Verify bump version config" - run: | - bump-my-version bump --dry-run release - bump-my-version show-bump - - # run black separately from flake8 to get a diff - - name: "Run black" - run: | - black --version - black --check --diff . - - # Lint code. - - name: "Run flake8" - run: | - flake8 - - - name: "Run extra lint checks" - run: | - [ ! -x .ci/scripts/extra_linting.sh ] || .ci/scripts/extra_linting.sh - - - name: "Check for any files unintentionally left out of MANIFEST.in" - run: | - check-manifest - - - name: "Verify requirements files" - run: | - python .ci/scripts/check_requirements.py - - - name: "Check for common gettext problems" - run: | - sh .ci/scripts/check_gettext.sh + # cache example from: https://pre-commit.com/#github-actions-example + - name: set PY + run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV + - uses: actions/cache@v5 + with: + path: ~/.cache/pre-commit + key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }} + - name: Run pre-commit + shell: "bash" + env: + PY_COLORS: "1" + FORCE_COLOR: "1" + PRE_COMMIT_COLOR: "always" + TERM: "xterm-256color" + run: | + pre-commit run --all-files -v diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000000..bd8fc2c4712 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,70 @@ +# WARNING: DO NOT EDIT! +# +# This file was generated by plugin_template, and is managed by it. Please use +# './plugin-template --github pulpcore' to update this file. +# +# For more info visit https://github.com/pulp/plugin_template + +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: 'https://github.com/pre-commit/pre-commit-hooks' + rev: 'v3.2.0' + hooks: + - id: check-yaml + - id: check-added-large-files + + - repo: 'https://github.com/pycqa/flake8' + rev: '7.3.0' + hooks: + - id: flake8 + args: ["--config", ".flake8"] + language_version: '3.11' + + - repo: 'https://github.com/psf/black' + rev: '24.3.0' + hooks: + - id: black + language_version: "3.11" + pass_filenames: false + args: ["--extend-exclude", ".*venv.*", "."] + additional_dependencies: ["flake8-black"] + + - repo: 'https://github.com/adrienverge/yamllint' + rev: 'v1.37.1' + hooks: + - id: yamllint + + - repo: 'https://github.com/mgedmin/check-manifest' + rev: '0.51' + hooks: + - id: check-manifest + + # Custom checks + - repo: local + hooks: + - id: check-bump + name: check-bump + language: python + entry: .ci/scripts/check_bump.sh + pass_filenames: false + additional_dependencies: ["bump-my-version"] + + - id: check-requirements + name: check-requirements + language: python + entry: python .ci/scripts/check_requirements.py + pass_filenames: false + additional_dependencies: ["packaging"] + + - id: check-gettext + name: check-gettext + language: unsupported_script + types: [python] + entry: .ci/scripts/check_gettext.sh + + - id: extra-lint + name: extra-lint + language: unsupported + types: [python] + entry: sh -c "[ ! -x .ci/scripts/extra_linting.sh ] || .ci/scripts/extra_linting.sh" diff --git a/.yamllint.yaml b/.yamllint.yaml new file mode 100644 index 00000000000..d05c5f4c5a4 --- /dev/null +++ b/.yamllint.yaml @@ -0,0 +1,10 @@ +# WARNING: DO NOT EDIT! +# +# This file was generated by plugin_template, and is managed by it. Please use +# './plugin-template --github pulpcore' to update this file. +# +# For more info visit https://github.com/pulp/plugin_template + +extends: relaxed +rules: + line-length: disable diff --git a/lint_requirements.txt b/lint_requirements.txt index 859b6ac9ad8..bcad90863b7 100644 --- a/lint_requirements.txt +++ b/lint_requirements.txt @@ -5,10 +5,13 @@ # # For more info visit https://github.com/pulp/plugin_template +# CI linting uses pre-commit +# keeping here for compatibility black==24.3.0 -bump-my-version -check-manifest -flake8 +check-manifest==0.51 +flake8==7.3.0 +yamllint==1.37.1 + flake8-black +bump-my-version packaging -yamllint diff --git a/pyproject.toml b/pyproject.toml index ac1acf902b2..da31aca63f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -199,6 +199,8 @@ ignore = [ "lint_requirements.txt", ".flake8", "pulpcore/tests/functional/assets/**", + ".yamllint.yaml", + ".pre-commit-config.yaml", ] [tool.bumpversion]