|
| 1 | +# Configuration: |
| 2 | +# Only set these if they have not been defined via command-line |
| 3 | +# such as: make VENV_NAME=my_custom_venv |
| 4 | +VENV_NAME ?= venv |
| 5 | +PYTHON ?= python3 |
| 6 | +PYPIRC = $(HOME)/.pypirc |
| 7 | + |
| 8 | +# Extract PROJECT_NAME from setup.py using helper script |
| 9 | +PROJECT_NAME := $(shell $(PYTHON) get_project_name.py) |
| 10 | + |
| 11 | +# Default target |
| 12 | +.DEFAULT_GOAL := test-publish |
| 13 | + |
| 14 | +# exclude special targets that start with a dot (like .PHONY) |
| 15 | +# exclude pattern rules that use % (like %.o: %.c) |
| 16 | +show-make-targets: |
| 17 | + @grep -E '^[^.%].*[^ ]:' Makefile | cut -d: -f1 | grep -i '^[a-z]' |
| 18 | + |
| 19 | +# Show the project name found in setup.py |
| 20 | +show-project-name: |
| 21 | + @echo $(PROJECT_NAME) |
| 22 | + |
| 23 | +# Check for ~/.pypirc |
| 24 | +check-pypirc: |
| 25 | + @if [ ! -f $(PYPIRC) ]; then \ |
| 26 | + echo "Error: $(PYPIRC) not found. Please create it with your PyPI credentials."; \ |
| 27 | + exit 1; \ |
| 28 | + fi |
| 29 | + |
| 30 | +# Create and configure virtual environment |
| 31 | +$(VENV_NAME): |
| 32 | + $(PYTHON) -m venv $(VENV_NAME) |
| 33 | + ./$(VENV_NAME)/bin/pip install --upgrade pip |
| 34 | + ./$(VENV_NAME)/bin/pip install setuptools wheel twine |
| 35 | + |
| 36 | +# Build distribution |
| 37 | +build: $(VENV_NAME) |
| 38 | + ./$(VENV_NAME)/bin/python setup.py sdist bdist_wheel |
| 39 | + |
| 40 | +# Clean build artifacts and virtual environment |
| 41 | +clean: |
| 42 | + rm -rf build/ |
| 43 | + rm -rf dist/ |
| 44 | + rm -rf *.egg-info/ |
| 45 | + rm -rf $(VENV_NAME)/ |
| 46 | + rm -rf __pycache__/ |
| 47 | + rm -rf test-install-venv/ |
| 48 | + rm -f *.whl.metadata .??*~ |
| 49 | + find . -type d -name "__pycache__" -exec rm -r "{}" + |
| 50 | + find . -type f -name "*.pyc" -delete |
| 51 | + command pip cache purge |
| 52 | + |
| 53 | +# Test PyPI targets |
| 54 | +test-publish: clean check-pypirc $(VENV_NAME) build |
| 55 | + ./$(VENV_NAME)/bin/twine upload --verbose --repository testpypi dist/* |
| 56 | + |
| 57 | +test-install: clean |
| 58 | + $(PYTHON) -m venv test-install-venv |
| 59 | + ./test-install-venv/bin/pip install --force-reinstall --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ $(PROJECT_NAME) |
| 60 | + |
| 61 | +# Production PyPI targets |
| 62 | +prod-publish: clean check-pypirc $(VENV_NAME) build |
| 63 | + @echo "Are you sure you want to publish to production PyPI? [y/N] " && read ans && [ $${ans:-N} = y ] |
| 64 | + ./$(VENV_NAME)/bin/twine --verbose upload dist/* |
| 65 | + |
| 66 | +prod-install: clean |
| 67 | + $(PYTHON) -m venv prod-install-venv |
| 68 | + ./prod-install-venv/bin/pip install --force-reinstall $(PROJECT_NAME) |
| 69 | + |
| 70 | +# Declare targets that don't create a file of the same name |
| 71 | +.PHONY: show-make-targets show-project-name check-pypirc build clean test-publish test-install prod-publish prod-install |
0 commit comments