add pre-commit and adjust tox to utilize it

Added pre-commit configs to match what is used in other Ironic
projects and adjusted tox to utilize it for any checks. Removed the
separate codespell job since that will be included in the default pep8.

Change-Id: Idaad2ab953e23d7780a0a49407889e85650c94cd
This commit is contained in:
Doug Goldstein 2025-03-28 07:19:08 -05:00
parent b610783177
commit b66252c50a
No known key found for this signature in database
6 changed files with 106 additions and 24 deletions

61
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,61 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: mixed-line-ending
args: ['--fix', 'lf']
exclude: |
(?x)(
.*.svg$|
)
- id: fix-byte-order-marker
- id: check-merge-conflict
- id: debug-statements
- id: check-json
files: .*\.json$
- id: check-yaml
files: .*\.(yaml|yml)$
exclude: releasenotes/.*$
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
hooks:
- id: remove-tabs
exclude: '.*\.(svg)$'
- repo: https://opendev.org/openstack/hacking
rev: 6.1.0
hooks:
- id: hacking
additional_dependencies: []
exclude: '^(doc|releasenotes|tools)/.*$'
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
args: [--write-changes]
- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: v1.0.0
hooks:
- id: sphinx-lint
args: [--enable=default-role]
files: ^doc/|releasenotes|api-ref
- repo: https://opendev.org/openstack/bashate
rev: 2.1.0
hooks:
- id: bashate
args: ["-iE006,E044", "-eE005,E042"]
name: bashate
description: This hook runs bashate for linting shell scripts
entry: bashate
language: python
types: [shell]
- repo: https://github.com/PyCQA/doc8
rev: v1.1.2
hooks:
- id: doc8
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
hooks:
- id: ruff
args: ['--fix', '--unsafe-fixes']

View file

@ -1,3 +1,33 @@
[build-system]
requires = ["pbr>=6.0.0", "setuptools>=64.0.0"]
build-backend = "pbr.build"
[tool.doc8]
ignore = ["D001"]
[tool.ruff]
line-length = 79
target-version = "py37"
[tool.ruff.lint]
select = [
"E", # pycodestyle (error)
"F", # pyflakes
"G", # flake8-logging-format
"LOG", # flake8-logging
"S", # flake8-bandit
]
[tool.ruff.lint.per-file-ignores]
"networking_baremetal/agent/ironic_neutron_agent.py" = [
"E402", # we need to monkey patch before import
]
"networking_baremetal/drivers/netconf/openconfig.py" = [
"S311", # we don't need a secure random choice here
"S314", # keep using xml since that's been in use
]
"networking_baremetal/tests/unit/drivers/netconf/test_openconfig.py" = [
"S101", # test uses assert
]

View file

@ -0,0 +1,4 @@
---
other:
- |
Add support for using pre-commit to run checks more easily for contributors.

View file

@ -48,4 +48,4 @@ quiet-level = 4
# Words to ignore:
# assertIn: Python's unittest method
ignore-words-list = assertIn
skip = ./releasenotes/notes/force-exit-on-comm-failure-d0a584af6a3bb373.yaml
skip = releasenotes/notes/force-exit-on-comm-failure-d0a584af6a3bb373.yaml

31
tox.ini
View file

@ -22,18 +22,16 @@ passenv =
NO_PROXY
[testenv:pep8]
deps =
bashate~=2.1.0 # Apache-2.0
flake8-import-order~=0.18.0 # LGPLv3
hacking~=6.1.0 # Apache-2.0
pycodestyle>=2.0.0,<3.0.0 # MIT
allowlist_externals = bash
{toxinidir}/tools/run_bashate.sh
commands =
bash tools/flake8wrap.sh {posargs}
# Run bashate during pep8 runs to ensure violations are caught by
# the check and gate queues.
{toxinidir}/tools/run_bashate.sh {toxinidir}/devstack
deps = pre-commit
allowlist_externals = pre-commit
commands = pre-commit run --all-files --show-diff-on-failure {posargs}
[testenv:codespell]
description =
Run codespell to check spelling
deps = pre-commit
allowlist_externals = pre-commit
commands = pre-commit run --all-files --show-diff-on-failure {posargs}
[testenv:venv]
commands = {posargs}
@ -106,12 +104,3 @@ application-import-names = networking_baremetal
filename = *.py
per-file-ignores =
networking_baremetal/agent/ironic_neutron_agent.py:E402
[testenv:codespell]
description =
Run codespell to check spelling
deps = codespell
# note(JayF): {posargs} lets us run `tox -ecodespell -- -w` to get codespell
# to correct spelling issues in our code it's aware of.
commands =
codespell {posargs}

View file

@ -7,8 +7,6 @@
check:
jobs:
- networking-baremetal-multitenant-vlans
- networking-baremetal-tox-codespell:
voting: false
gate:
jobs:
- networking-baremetal-multitenant-vlans