Created
July 13, 2024 20:09
-
-
Save pszponder/cdc63bb64223f4c98b6dd9d31f9a9666 to your computer and use it in GitHub Desktop.
Python Makefile using uv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.ONESHELL: | |
# Define variables | |
VENV := .venv | |
BIN := $(VENV)/bin | |
PIP := $(BIN)/pip | |
PYTHON := $(BIN)/python | |
ACTIVATE := . ./$(BIN)/activate | |
APP := src/main.py | |
DOCKER_TAG := project_name # Change to your project name | |
# Define default target | |
.DEFAULT_GOAL := help | |
# ================================================ | |
# HELPERS | |
# ================================================ | |
# Default target | |
.PHONY: help | |
help: ## List available commands | |
@echo "Available commands:" | |
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " make %-15s - %s\n", $$1, $$2}' | |
# ================================================ | |
# Startup | |
# ================================================ | |
# Target to create virtual environment | |
.PHONY: venv | |
venv: ## Create a virtual environment | |
uv venv | |
# Target to install dependencies | |
.PHONY: install | |
install: venv ## Install dependencies | |
uv pip install -U -r requirements.txt | |
.PHONY: init | |
init: venv install ## Initialize python project (runs make venv & make install) | |
# ================================================ | |
# RUNNING | |
# ================================================ | |
.PHONY: run | |
run: ## Run the project | |
$(PYTHON) $(APP) | |
# ================================================ | |
# CLEANING | |
# ================================================ | |
.PHONY: clean | |
clean: ## Remove all temporary files | |
rm -rf .ruff_cache | |
rm -rf .pytest_cache | |
find . -type f -name *.pyc -delete | |
find . -type d -name __pycache__ -delete | |
.PHONY: cleanall | |
cleanall: clean ## Remove all temporary files and virtual environment | |
rm -rf $(VENV) | |
# ================================================ | |
# QUALITY CONTROL | |
# ================================================ | |
.PHONY: lint | |
lint: ## Lint code using ruff linter | |
$(BIN)/ruff check | |
.PHONY: format | |
format: ## Formats code using ruff formatter | |
$(BIN)/ruff format | |
.PHONY: tidy | |
tidy: lint format ## Lint & format code | |
# ================================================ | |
# DEPENDENCIES and PACKAGES | |
# ================================================ | |
.PHONY: update | |
update: ## Update requirements.txt with all installed python packages in venv | |
uv pip freeze > requirements.txt | |
.PHONY: add | |
add: ## Add python package(s) to requirements.txt | |
uv pip install $(filter-out $@,$(MAKECMDGOALS)) | |
uv pip freeze > requirements.txt | |
.PHONY: remove | |
remove: ## Remove python package(s) from requirements.txt | |
uv pip uninstall $(filter-out $@,$(MAKECMDGOALS)) | |
uv pip freeze > requirements.txt | |
.PHONY: upgrade | |
upgrade: ## Upgrade python package(s) in requirements.txt to latest version | |
sed -i 's/[~=]=/>=/' requirements.txt | |
uv pip install -U -r requirements.txt | |
uv pip freeze > requirements.txt | |
# ================================================ | |
# DOCKER | |
# ================================================ | |
.PHONY: build | |
build: ## Build docker image | |
docker build -t $(DOCKER_TAG) . | |
.PHONY: up | |
up: ## Run docker container | |
docker compose up -d | |
.PHONY: upbuild | |
upbuild: build up ## Build and run docker container | |
.PHONY: restart | |
restart: ## Restart docker container | |
docker compose restart | |
.PHONY: logs | |
logs: ## View docker container logs | |
docker compose logs | |
.PHONY: logsf | |
logsf: ## View docker container logs in real time | |
docker compose logs -f app --tail=100 | |
.PHONY: dclean | |
dclean: ## Remove docker container | |
docker rm -f $(DOCKER_TAG) | |
.PHONY: down | |
down: ## Stop docker container | |
docker compose down | |
.PHONY: downall | |
downall: dclean ## Stop and remove docker container | |
docker compose down -v --remove-orphans --rmi all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment