From ee04ce936421f815ca609cee89651624151b0468 Mon Sep 17 00:00:00 2001 From: Jens Troeger Date: Thu, 11 May 2023 22:18:45 +1000 Subject: [PATCH 1/4] fix(make): improve targets and prerequisites to avoid running a recipe unnecessarily --- Makefile | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index d1d306f9..941b732c 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,10 @@ SHELL := bash PACKAGE_NAME := package PACKAGE_VERSION := $(shell python -c $$'try: import $(PACKAGE_NAME); print($(PACKAGE_NAME).__version__);\nexcept: print("unknown");') +# Files that this package depends on are in the src/ folder, so collect them +# all into a list. +PACKAGE_DEPS := $(shell find src/$(PACKAGE_NAME) -type f -not -path "*/__pycache__/*") + # This variable contains the first goal that matches any of the listed goals # here, else it contains an empty string. The net effect is to filter out # whether this current run of `make` requires a Python virtual environment @@ -106,7 +110,8 @@ upgrade-quiet: # Generate a Software Bill of Materials (SBOM). .PHONY: sbom -sbom: requirements +sbom: requirements dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json +dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json: cyclonedx-py --force --requirements --format json --output dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json # Generate a requirements.txt file containing version and integrity hashes for all @@ -119,8 +124,8 @@ sbom: requirements # We also want to make sure that this package itself is added to the requirements.txt # file, and if possible even with proper hashes. .PHONY: requirements -requirements: requirements.txt -requirements.txt: pyproject.toml +requirements: requirements.txt dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-requirements.txt +requirements.txt dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-requirements.txt: .venv/upgraded-on echo -n "" > requirements.txt for pkg in $$(python -m pip freeze --local --disable-pip-version-check --exclude-editable); do \ pkg=$${pkg//[$$'\r\n']}; \ @@ -162,14 +167,18 @@ check-mypy: pre-commit run mypy --all-files check-actionlint: pre-commit run actionlint --all-files -check: +check: .venv/checked-on +.venv/checked-on: $(PACKAGE_DEPS) pre-commit run --all-files + echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/checked-on # Run all unit tests. The --files option avoids stashing but passes files; however, # the hook setup itself does not pass files to pytest (see .pre-commit-config.yaml). .PHONY: test -test: +test: check .venv/tested-on +.venv/tested-on: $(PACKAGE_DEPS) pre-commit run pytest --hook-stage push --files tests/ + echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/tested-on # Build a source distribution package and a binary wheel distribution artifact. # When building these artifacts, we need the environment variable SOURCE_DATE_EPOCH From 7f10dde14171f1760a866bc60eff7786390586b3 Mon Sep 17 00:00:00 2001 From: Jens Troeger Date: Fri, 12 May 2023 07:00:19 +1000 Subject: [PATCH 2/4] fix: add tests to package deps --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 941b732c..517420f1 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,9 @@ PACKAGE_NAME := package PACKAGE_VERSION := $(shell python -c $$'try: import $(PACKAGE_NAME); print($(PACKAGE_NAME).__version__);\nexcept: print("unknown");') # Files that this package depends on are in the src/ folder, so collect them -# all into a list. -PACKAGE_DEPS := $(shell find src/$(PACKAGE_NAME) -type f -not -path "*/__pycache__/*") +# all into a list. Also collect everything needed for testing this package. +PACKAGE_DEPS := $(shell find src/$(PACKAGE_NAME)/ -type f -not -path "*/__pycache__/*") +PACKAGE_DEPS_TEST := $(shell find tests/ -type f -not -path "*/__pycache__/*") # This variable contains the first goal that matches any of the listed goals # here, else it contains an empty string. The net effect is to filter out @@ -176,7 +177,7 @@ check: .venv/checked-on # the hook setup itself does not pass files to pytest (see .pre-commit-config.yaml). .PHONY: test test: check .venv/tested-on -.venv/tested-on: $(PACKAGE_DEPS) +.venv/tested-on: $(PACKAGE_DEPS) $(PACKAGE_DEPS_TEST) pre-commit run pytest --hook-stage push --files tests/ echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/tested-on @@ -232,6 +233,7 @@ prune: .PHONY: dist-clean clean dist-clean: rm -fr dist/* + rm -f .venv/checked-on .venv/tested-on rm -f requirements.txt clean: dist-clean rm -fr .coverage .hypothesis/ .mypy_cache/ .pytest_cache/ From c39cc3c858ad13916d33f6b82a757fe85f6c0bf0 Mon Sep 17 00:00:00 2001 From: Jens Troeger Date: Mon, 15 May 2023 09:53:22 +1000 Subject: [PATCH 3/4] fix(make): fix regression for `make test` --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 517420f1..bab66ab7 100644 --- a/Makefile +++ b/Makefile @@ -176,7 +176,7 @@ check: .venv/checked-on # Run all unit tests. The --files option avoids stashing but passes files; however, # the hook setup itself does not pass files to pytest (see .pre-commit-config.yaml). .PHONY: test -test: check .venv/tested-on +test: .venv/tested-on .venv/tested-on: $(PACKAGE_DEPS) $(PACKAGE_DEPS_TEST) pre-commit run pytest --hook-stage push --files tests/ echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/tested-on From 2a4ac4171cff85db47c1a53da0a0829aeebea7f2 Mon Sep 17 00:00:00 2001 From: Jens Troeger Date: Fri, 2 Jun 2023 06:26:33 +1000 Subject: [PATCH 4/4] chore: add useful link --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index bab66ab7..36a14632 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +# Find an extensive introduction to Makefiles here: https://makefiletutorial.com/ # Use bash as the shell when executing a rule's recipe. For more details: # https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html