Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(make): improve targets and prerequisites to avoid running a recipe unnecessarily #543

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,6 +8,11 @@ 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. 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
# whether this current run of `make` requires a Python virtual environment
Expand Down Expand Up @@ -106,7 +112,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
Expand All @@ -119,8 +126,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']}; \
Expand Down Expand Up @@ -162,14 +169,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are introducing back pre-commit running on staged files only (instead of all files)?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PACKAGE_DEPS contains all current Python files in the src/package/ folder, and if any of those changes since make last touched .venv/checked-on then we run the recipe again. Whether they’re staged or not shouldn’t make a difference.

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: .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

# Build a source distribution package and a binary wheel distribution artifact.
# When building these artifacts, we need the environment variable SOURCE_DATE_EPOCH
Expand Down Expand Up @@ -223,6 +234,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/
Expand Down