Skip to content

Commit

Permalink
ci: organize pre-commit config
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 23, 2024
1 parent 6fcb22b commit b479a96
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,5 @@ tags
.ruff_cache/
coverage.*
.coverage.*

!scripts/
94 changes: 47 additions & 47 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
repos:
- hooks:
- args:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: '2c9f875913ee60ca25ce70243dc24d5b6415598c' # frozen: v4.6.0
hooks:
- id: trailing-whitespace
args:
- --markdown-linebreak-ext=md,markdown
id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
Expand All @@ -17,72 +19,70 @@ repos:
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- args:
- id: mixed-line-ending
args:
- --fix=lf
id: mixed-line-ending
- exclude: ^\.(vscode|devcontainer)/
id: check-json
- args:
- id: check-json
exclude: ^\.(vscode|devcontainer)/
- id: pretty-format-json
exclude: ^\.(vscode|devcontainer)/
args:
- --indent
- '4'
- --autofix
- --no-sort-keys
exclude: ^\.(vscode|devcontainer)/
id: pretty-format-json
- id: check-toml
repo: https://github.com/pre-commit/pre-commit-hooks
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c
- repo: https://github.com/bwhmather/ssort
rev: 202524364c6ba81752b4fa9c77f16c9ccd5a5e28 # frozen: 0.13.0
rev: '202524364c6ba81752b4fa9c77f16c9ccd5a5e28' # frozen: 0.13.0
hooks:
- id: ssort
- hooks:
- repo: https://github.com/psf/black
rev: '3702ba224ecffbcec30af640c149f231d90aebdb' # frozen: 24.4.2
hooks:
- id: black
repo: https://github.com/psf/black
rev: 3702ba224ecffbcec30af640c149f231d90aebdb # frozen: 24.4.2
- hooks:
- repo: https://github.com/pycqa/isort
rev: 'c235f5e450b4b84e58d114ed4c589cbf454175a3' # frozen: 5.13.2
hooks:
- id: isort
repo: https://github.com/pycqa/isort
rev: c235f5e450b4b84e58d114ed4c589cbf454175a3
- hooks:
- repo: https://github.com/pre-commit/pygrep-hooks
rev: '3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316' # frozen: v1.10.0
hooks:
- id: python-no-eval
- id: python-no-log-warn
repo: https://github.com/pre-commit/pygrep-hooks
rev: 3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316
- hooks:
- repo: https://github.com/ikamensh/flynt
rev: '651c822fdcf45fffcf9743dc755085f32acb65e3' # frozen: 1.0.1
hooks:
- id: flynt
repo: https://github.com/ikamensh/flynt
rev: 651c822fdcf45fffcf9743dc755085f32acb65e3
- hooks:
- args:
- repo: https://github.com/asottile/pyupgrade
rev: '12af25eb252deaaecb6b259df40d01f42e716dc3' # frozen: v3.15.2
hooks:
- id: pyupgrade
args:
- --py38-plus
id: pyupgrade
repo: https://github.com/asottile/pyupgrade
rev: 12af25eb252deaaecb6b259df40d01f42e716dc3
- hooks:
- additional_dependencies:
- repo: https://github.com/MarcoGorelli/auto-walrus
rev: 'ba0661836089a532a710f68713a3e3e53f7f0113' # frozen: 0.3.4
hooks:
- id: auto-walrus
additional_dependencies:
- tomli ; python_version < '3.11'
id: auto-walrus
repo: https://github.com/MarcoGorelli/auto-walrus
rev: ba0661836089a532a710f68713a3e3e53f7f0113 # frozen: 0.3.4
- hooks:
- additional_dependencies:
- repo: https://github.com/codespell-project/codespell
rev: '193cd7d27cd571f79358af09a8fb8997e54f8fff' # frozen: v2.3.0
hooks:
- id: codespell
additional_dependencies:
- tomli ; python_version < '3.11'
id: codespell
repo: https://github.com/codespell-project/codespell
rev: 6e41aba91fb32e9feb741a6258eefeb9c6e4a482
- hooks:
- repo: https://github.com/commitizen-tools/commitizen
rev: 'e9aa5d979ea6fd14dcf59c6bd3836bef17d386c1' # frozen: v3.27.0
hooks:
- id: commitizen
repo: https://github.com/commitizen-tools/commitizen
rev: c2bbc92202e9080adcdf8fa2e99c9e4406a1f684 # frozen: v3.26.0
- hooks:
- entry: hatch run types:check
id: mypy
- repo: local
hooks:
- id: mypy
entry: hatch run types:check
language: system
name: mypy
types:
- python
repo: local
- repo: https://github.com/andreoliwa/nitpick
rev: '36d4912df1f0bd2ed8cc535b57c5ba3b09ce3d46' # frozen: v0.35.0
hooks:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ dependencies = [
"pre-commit",
"mypy>=1.8",
"types-regex",
"ruamel.yaml",
]

[tool.hatch.envs.default.scripts]
setup-dev = ["hatch run pre-commit install"]
fix-pre-commit = ["hatch run python3 -m scripts.fix_pre_commit"]

[tool.hatch.envs.hatch-test]
default-args = ["tests", "polymatch"]
Expand Down
3 changes: 3 additions & 0 deletions scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2018-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT
56 changes: 56 additions & 0 deletions scripts/fix_pre_commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2018-present linuxdaemon <linuxdaemon.irc@gmail.com>
#
# SPDX-License-Identifier: MIT

from collections import OrderedDict

from ruamel.yaml import YAML
from ruamel.yaml.comments import Comment, CommentedMap, CommentedSeq

yaml = YAML(typ="rt")

yaml.preserve_quotes = True
yaml.sequence_dash_offset = 2
yaml.map_indent = 2
yaml.sequence_indent = 4


def main() -> None:
with open(".pre-commit-config.yaml") as f:
data = yaml.load(f)

repos: CommentedSeq = data["repos"]
for j in range(len(repos)):
repo: CommentedMap = repos[j]
new_repo = repo.copy_attributes(CommentedMap())

for v in ("repo", "rev"):
if v in repo:
old = repo.pop(v)
# print(type(old))
new_repo[v] = old

for k, v in repo.items():
new_repo[k] = v

hooks = new_repo["hooks"]
for i in range(len(hooks)):
hook = hooks[i]
new_hook = hook.copy_attributes(CommentedMap())
for k in ("id", "exclude"):
if k in hook:
new_hook[k] = hook.pop(k)

for k, v in hook.items():
new_hook[k] = v

hooks[i] = new_hook

repos[j] = new_repo

with open(".pre-commit-config.yaml", "w") as f:
yaml.dump(data, f)


if __name__ == "__main__":
main()

0 comments on commit b479a96

Please sign in to comment.