From 29abf14a67339e455729180cdf86067180f6c395 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 1 Sep 2026 11:07:21 +0200 Subject: [PATCH] py-ns-dir.mk: Support __init__.py generation For in-tree tests, the jw package is split across several projects, and each directory containing a py-ns-dir.mk contributes its subtree. So far, it's up to that directory how the contribution is handled. Usually an __init__.py with pkgutil.extend_path() is present in version control to glue these parts together. This commit makes py-ns-dir.mk handle the contribution method centrally by default unless PY_UPDATE_INIT_PY is set to false. As of this commit, this is the case by default, i.e. PY_UPDATE_INIT_PY is set to false in py-ns-dir.mk, maintaining the current behaviour. Downstream projects or modules can decide to have __init__.py centrally maintained. For this, they need to remove __init__.py from version control and set PY_UPDATE_INIT_PY to true in the respective Makefile. PY_UPDATE_INIT_PY = false is also set explicitly in src/python/jw/Makefile, because that file should never be generated. Bootstrapping the entire workspace hinges on it. Pending better testing, most notably of consistent in-tree testing functionality, PY_UPDATE_INIT_PY = true might become a global default in the future. Signed-off-by: Jan Lindemann --- make/py-ns-dir.mk | 23 +++++++++++++++++++++-- src/python/jw/Makefile | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/make/py-ns-dir.mk b/make/py-ns-dir.mk index 1c69d559..3529e721 100644 --- a/make/py-ns-dir.mk +++ b/make/py-ns-dir.mk @@ -1,7 +1,26 @@ +define INIT_PY +from pkgutil import extend_path + +__path__ = extend_path(__path__, __name__) +endef + +# The __init__.py machinery duplicates (but is independent from) py-mod.mk +PY_UPDATE_INIT_PY ?= true +PY_INSTALL_INIT_PY ?= false + +ifneq ($(PY_UPDATE_INIT_PY),false) + PY_GENERATED_PY += __init__.py +endif + include $(JWBDIR)/make/dirs.mk -all: +all: $(PY_GENERATED_PY) clean: py.clean py.clean: - rm -rf __pycache__ + rm -rf __pycache__ $(PY_GENERATED_PY) + +__init__.py: + @echo "Creating default namespace stub $@.tmp" + $(file >$@.tmp,$(INIT_PY)) + mv $@.tmp $@ diff --git a/src/python/jw/Makefile b/src/python/jw/Makefile index d99ad1c0..78727dfe 100644 --- a/src/python/jw/Makefile +++ b/src/python/jw/Makefile @@ -1,4 +1,6 @@ TOPDIR = ../../.. +PY_UPDATE_INIT_PY = false + include $(TOPDIR)/make/proj.mk include $(JWBDIR)/make/py-ns-dir.mk