jw-pkg/make/py-check.mk
Jan Lindemann 4bf6445412
py-check.mk: Add target py-check-bad-patterns
Add target py-check-bad-patterns, which currently looks for leftover
breakpoints and orphaned "export" comments on the closing line of a
function prototype.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 13:31:34 +02:00

93 lines
2.4 KiB
Makefile

.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-bad-patterns
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-bad-patterns:
if find $(PY_CHECK_ROOTS) -type f -name '*.py' -print0 | xargs -0 grep breakpoint; then exit 1; fi
if find $(PY_CHECK_ROOTS) -type f -name '*.py' -print0 | xargs -0 grep "^\s*).*#\s*export"; then exit 1; fi
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