Skip to content

Instantly share code, notes, and snippets.

@toddbirchard
Last active July 20, 2023 19:17
Show Gist options
  • Save toddbirchard/edfe3f251c657116cd69eac6e9d657b1 to your computer and use it in GitHub Desktop.
Save toddbirchard/edfe3f251c657116cd69eac6e9d657b1 to your computer and use it in GitHub Desktop.
Python Makefile Template
PROJECT_NAME := $(shell basename $CURDIR)
VIRTUAL_ENVIRONMENT := $(CURDIR)/.venv
LOCAL_PYTHON := $(VIRTUAL_ENVIRONMENT)/bin/python3
PROJECT_ENTRY_POINT := $(shell $CURDIR)/main.py
define HELP
Manage $(PROJECT_NAME). Usage:
make run - Run $(PROJECT_NAME) locally.
make dev. - Run $(PROJECT_NAME) in development mode.
make install - Create local virtualenv & install dependencies.
make deploy - Set up project & run locally.
make update - Update dependencies via Poetry and output resulting `requirements.txt`.
make format - Run Python code formatter & sort dependencies.
make lint - Check code formatting with flake8.
make clean - Remove extraneous compiled files, caches, logs, etc.
endef
export HELP
.PHONY: run install deploy update format lint clean help
all help:
@echo "$$HELP"
env: $(VIRTUAL_ENVIRONMENT)
$(VIRTUAL_ENVIRONMENT):
if [ ! -d $(VIRTUAL_ENVIRONMENT) ]; then \
echo "Creating Python virtual environment..."; \
python3 -m venv $(VIRTUAL_ENVIRONMENT); \
fi
.PHONY: dev
dev: env
$(LOCAL_PYTHON) -m main:init_script --reload
.PHONY: run
run: env
$(LOCAL_PYTHON) -m main:init_script
.PHONY: install
install: env
$(LOCAL_PYTHON) -m pip install --upgrade pip setuptools wheel && \
$(LOCAL_PYTHON) -m pip install -r requirements.txt && \
echo "Installed dependencies in virtualenv \`${VIRTUAL_ENVIRONMENT}\`";
.PHONY: deploy
deploy:
make clean \
make install \
make run
.PHONY: test
test: env
$(LOCAL_PYTHON) -m \
coverage run -m pytest -v \
--disable-pytest-warnings && \
coverage html --title='Coverage Report' -d .reports && \
open .reports/index.html
.PHONY: update
update: env
$(LOCAL_PYTHON) -m pip install --upgrade pip setuptools wheel && \
poetry update && \
poetry export -f requirements.txt --output requirements.txt --without-hashes && \
echo "Updated dependencies in virtualenv \`${VIRTUAL_ENVIRONMENT}\`";
.PHONY: format
format: env
$(LOCAL_PYTHON) -m isort --multi-line=3 . \
$(LOCAL_PYTHON) -m && black .
.PHONY: lint
lint: env
$(LOCAL_PYTHON) -m flake8 . --count \
--select=E9,F63,F7,F82 \
--exclude .git,.github,__pycache__,.pytest_cache,.venv,logs,creds,.venv,docs,logs,.reports \
--show-source \
--statistics
.PHONY: clean
clean:
find . -name 'poetry.lock' -delete && \
find . -name '.coverage' -delete && \
find . -wholename '**/*.pyc' -delete && \
find . -type d -wholename '__pycache__' -exec rm -rf {} + && \
find . -type d -wholename '.venv' -exec rm -rf {} + && \
find . -type d -wholename '.pytest_cache' -exec rm -rf {} + && \
find . -type d -wholename '**/.pytest_cache' -exec rm -rf {} + && \
find . -type d -wholename './logs/*' -exec rm -rf {} + && \
find . -type d -wholename './.reports/*' -exec rm -rf {} +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment