py-check.mk: Add file
Add py-check.mk and use it from py-topdir.mk and py-rules.mk. This removes redundant check definitions, making sure that that checks run from a repo's subdirectory match the checks run from its toplevel directory. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
150866015a
commit
ee697d203d
4 changed files with 92 additions and 66 deletions
89
make/py-check.mk
Normal file
89
make/py-check.mk
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
.PHONY: \
|
||||
all \
|
||||
check \
|
||||
format \
|
||||
check-syntax \
|
||||
check-format \
|
||||
py-check \
|
||||
py-check-syntax \
|
||||
py-check-format \
|
||||
py-format \
|
||||
py-format-assignments \
|
||||
py-check-annotation-imports \
|
||||
py-format-annotation-imports \
|
||||
clean \
|
||||
clean.py-check
|
||||
|
||||
ifndef PY_CHECK_ROOTS
|
||||
PY_CHECK_ROOTS = .
|
||||
endif
|
||||
|
||||
ifndef PY_CHECK_MYPY
|
||||
PY_CHECK_MYPY := mypy
|
||||
endif
|
||||
|
||||
ifndef PY_CHECK_RUFF
|
||||
PY_CHECK_RUFF := $(shell which ruff 2>/dev/null)
|
||||
ifneq ($(PY_CHECK_RUFF),)
|
||||
PY_CHECK_RUFF += --config $(TOPDIR)/pyproject.toml
|
||||
endif
|
||||
endif
|
||||
|
||||
ifndef PY_CHECK_YAPF
|
||||
PY_CHECK_YAPF := $(firstword $(wildcard /usr/bin/yapf /usr/bin/yapf3))
|
||||
endif
|
||||
|
||||
ifndef PY_CHECK_PYRIGHT
|
||||
PY_CHECK_PYRIGHT := $(shell which pyright 2>/dev/null)
|
||||
TD_GENERATE_FILES += pyrightconfig.json
|
||||
endif
|
||||
|
||||
all:
|
||||
check: py-check
|
||||
format: py-format
|
||||
check-syntax: py-check-syntax
|
||||
check-format: py-check-format
|
||||
|
||||
py-check: py-check-syntax py-check-format
|
||||
|
||||
py-check-syntax:
|
||||
ifneq ($(PY_CHECK_RUFF),)
|
||||
$(PY_CHECK_RUFF) check $(addprefix --exclude ,$(PY_CHECK_EXCLUDE)) $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
$(PY_CHECK_MYPY) $(addprefix --exclude ,$(PY_CHECK_EXCLUDE)) $(PY_CHECK_ROOTS)
|
||||
ifneq ($(PY_CHECK_PYRIGHT),)
|
||||
$(PY_CHECK_PYRIGHT) $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
|
||||
py-check-format:
|
||||
ifneq ($(PY_CHECK_YAPF),)
|
||||
$(PY_CHECK_YAPF) --diff --recursive $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
|
||||
py-format:
|
||||
find . -type f -name '*.py' -print0 | \
|
||||
xargs -0 sed -i -E '1{/^# -\*- coding: utf-8 -\*-$$/{:a;N;/\n[[:space:]]*$$/ba;s/^# -\*- coding: utf-8 -\*-\n([[:space:]]*\n)*/ /;s/^ //}}'
|
||||
ifneq ($(PY_CHECK_YAPF),)
|
||||
$(PY_CHECK_YAPF) --in-place --recursive $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
|
||||
py-format-assignments:
|
||||
find $(PY_CHECK_ROOTS) \
|
||||
-path './.git' -prune -o \
|
||||
-type f -name '*.py' \
|
||||
-execdir /usr/bin/sed -i 's/^\(\s\+[a-zA-Z0-9_]\+\)=\([^,[:space:]]\+\)\([,(]\)*\s*$$/\1 = \2\3/g' {} '+'
|
||||
git diff --exit-code
|
||||
|
||||
py-check-annotation-imports:
|
||||
ifneq ($(PY_CHECK_RUFF),)
|
||||
$(PY_CHECK_RUFF) check --select TC,FA --diff --unsafe-fixes $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
|
||||
py-format-annotation-imports:
|
||||
ifneq ($(PY_CHECK_RUFF),)
|
||||
$(PY_CHECK_RUFF) check --select TC,FA --fix --unsafe-fixes $(PY_CHECK_ROOTS)
|
||||
endif
|
||||
|
||||
clean: clean.py-check
|
||||
clean.py-check:
|
||||
rm -rf .mypy_cache .ruff_cache .pytest_cache
|
||||
Loading…
Reference in a new issue