diff --git a/make/py-check.mk b/make/py-check.mk new file mode 100644 index 00000000..05687d7c --- /dev/null +++ b/make/py-check.mk @@ -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 diff --git a/make/py-defs.mk b/make/py-defs.mk index de6da233..a947ef26 100644 --- a/make/py-defs.mk +++ b/make/py-defs.mk @@ -19,8 +19,6 @@ else PY_SITE_PACKAGES_PATH := $(shell $(PYTHON) -c "import site; print([d for d in site.getsitepackages() if d.find('/local/') == -1][0])") endif -PY_MYPY ?= mypy --ignore-missing-imports --no-namespace-packages - PY_SRC_PY ?= $(wildcard *.py) PY_ALL_PY = $(PY_SRC_PY) diff --git a/make/py-rules.mk b/make/py-rules.mk index ce6f16a3..acf463ff 100644 --- a/make/py-rules.mk +++ b/make/py-rules.mk @@ -29,8 +29,7 @@ $(PY_INSTALL_DIR_PY)/%.pyc: %.pyc endif $(INSTALL) -p -m $(PYMODMODE) -o $(PYMODOWNER) -g $(PYMODGROUP) $< $@ -check: - $(PY_MYPY) $(shell /bin/bash $(JWB_SCRIPT_DIR)/scm.sh ls-files | grep '\.py$$') +include $(JWBDIR)/make/py-check.mk $(PY_INSTALL_DIR_PY)/py.typed: py.typed $(INSTALL) -p -m $(PYMODMODE) -o $(PYMODOWNER) -g $(PYMODGROUP) $< $@ diff --git a/make/py-topdir.mk b/make/py-topdir.mk index 65f170f3..c6b0f3ac 100644 --- a/make/py-topdir.mk +++ b/make/py-topdir.mk @@ -11,71 +11,9 @@ ifndef PY_CHECK_ROOTS PY_CHECK_ROOTS = $(wildcard $(TOPDIR)/src $(TOPDIR)/tools) endif -ifndef PY_CHECK_RUFF - PY_CHECK_RUFF := $(shell which ruff 2>/dev/null) - ifneq ($(PY_CHECK_RUFF),) - PY_CHECK_RUFF += --config 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: - -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 - mypy $(addprefix --exclude ,$(PY_CHECK_EXCLUDE)) $(PY_CHECK_ROOTS) -ifneq ($(PY_CHECK_PYRIGHT),) - 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.topdir clean.topdir: clean.py-check -clean.py-check: - rm -rf .mypy_cache .ruff_cache .pytest_cache pyproject.toml: $(PYTHON) $(JWB_SCRIPT_DIR)/jw-pkg.py -p $(PROJECTS_DIR) -t $(TOPDIR) --topdir-format unaltered projects create-file --format tmpl \ @@ -86,3 +24,5 @@ pyrightconfig.json: $(PYTHON) $(JWB_SCRIPT_DIR)/jw-pkg.py -p $(PROJECTS_DIR) -t $(TOPDIR) --topdir-format unaltered projects create-file --format pyright \ --field base=$(JW_PKG_CONF_BASE_DIR)/project/pyrightconfig-base.json $(addprefix --field include=,$(wildcard src/python tools/python)) $(PROJECT) > $@.tmp mv $@.tmp $@ + +include $(JWBDIR)/make/py-check.mk