-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add UV for project management, Ruff for formatting. #1391
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
base: master
Are you sure you want to change the base?
Changes from all commits
424061c
a1170b4
c121e52
f928e0b
93568a6
4fcf3a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 3.14 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,66 +4,59 @@ docker_config = --settings=config.docker-compose | |
| gql_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml | ||
| gqlv1beta_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml -f Resources/compose/docker-compose-prod-graphql-v1beta.yml | ||
|
|
||
| # Auto-detect Python and pip commands | ||
| PYTHON := $(shell which python3 2>/dev/null || which python 2>/dev/null || echo python3) | ||
| PIP := $(shell which pip3 2>/dev/null || which pip 2>/dev/null || echo pip3) | ||
|
|
||
| .PHONY: help | ||
| .SILENT: | ||
|
|
||
| help: | ||
| @grep -E '^[a-zA-Z_-]+:.*?# .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | ||
|
|
||
| install: # Install base requirements to run project | ||
| $(PIP) install -r requirements.txt | ||
|
|
||
| dev-install: # Install developer requirements + base requirements | ||
| $(PIP) install -r test-requirements.txt | ||
| uv sync | ||
|
|
||
| setup: # Set up the project database | ||
| $(PYTHON) manage.py migrate ${local_config} | ||
| uv run manage.py migrate ${local_config} | ||
|
|
||
| build-db: # Build database | ||
| echo "from data.v2.build import build_all; build_all()" | $(PYTHON) manage.py shell ${local_config} | ||
| echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell ${local_config} | ||
|
|
||
| wipe-sqlite-db: # Delete's the project database | ||
| rm -rf db.sqlite3 | ||
|
|
||
| serve: # Run the project locally | ||
| $(PYTHON) manage.py runserver ${local_config} | ||
| uv run manage.py runserver ${local_config} | ||
|
|
||
| test: # Run tests | ||
| $(PYTHON) manage.py test ${local_config} | ||
| uv run manage.py test ${local_config} | ||
|
|
||
| clean: # Remove any pyc files | ||
| find . -type f -name '*.pyc' -delete | ||
|
|
||
| migrate: # Run any outstanding migrations | ||
| $(PYTHON) manage.py migrate ${local_config} | ||
| uv run manage.py migrate ${local_config} | ||
|
|
||
| make-migrations: # Create migrations files if schema has changed | ||
| $(PYTHON) manage.py makemigrations ${local_config} | ||
| uv run manage.py makemigrations ${local_config} | ||
|
|
||
| shell: # Load a shell | ||
| $(PYTHON) manage.py shell ${local_config} | ||
| uv run manage.py shell ${local_config} | ||
|
|
||
| openapi-generate: | ||
| $(PYTHON) manage.py spectacular --color --file openapi.yml ${local_config} | ||
| uv run manage.py spectacular --color --file openapi.yml ${local_config} | ||
|
|
||
| docker-up: # (Docker) Create services/volumes/networks | ||
| docker compose up -d | ||
|
|
||
| docker-migrate: # (Docker) Run any pending migrations | ||
| docker compose exec -T app python manage.py migrate ${docker_config} | ||
| docker compose --verbose exec -T app uv run manage.py migrate ${docker_config} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the |
||
|
|
||
| docker-build-db: # (Docker) Build the database | ||
| docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${docker_config}' | ||
| docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell ${docker_config}' | ||
|
|
||
| docker-make-migrations: # (Docker) Create migrations files if schema has changed | ||
| docker compose exec -T app sh -c 'python manage.py makemigrations ${docker_config}' | ||
| docker compose exec -T app sh -c 'uv run manage.py makemigrations ${docker_config}' | ||
|
|
||
| docker-flush-db: # (Docker) Removes all the data present in the database but preserves tables and migrations | ||
| docker compose exec -T app sh -c 'python manage.py flush --no-input ${docker_config}' | ||
| docker compose exec -T app sh -c 'uv run manage.py flush --no-input ${docker_config}' | ||
|
|
||
| docker-destroy-db: # (Docker) Removes the volume where the database is installed on, alongside to the container itself | ||
| docker rm -f pokeapi_db_1 | ||
|
|
@@ -79,18 +72,18 @@ docker-down: # (Docker) Stop and removes containers and networks | |
| docker compose down | ||
|
|
||
| docker-test: # (Docker) Run tests | ||
| docker compose exec -T app python manage.py test ${local_config} | ||
| docker compose exec -T app uv run manage.py test ${local_config} | ||
|
|
||
| docker-prod: | ||
| docker compose -f docker-compose.yml -f docker-compose.override.yml -f Resources/compose/docker-compose-prod-graphql.yml up -d | ||
|
|
||
| docker-setup: docker-up docker-migrate docker-build-db # (Docker) Start services, prepare the latest DB schema, populate the DB | ||
|
|
||
| format: # Format the source code | ||
| black . --extend-exclude '.+/scripts/.+' | ||
| uv run ruff check . --fix --extend-exclude '.+/scripts/.+' | ||
|
|
||
| format-check: # Check the source code has been formatted | ||
| black . --check --extend-exclude '.+/scripts/.+' | ||
| uv run ruff check . --extend-exclude '.+/scripts/.+' | ||
|
|
||
| pull: | ||
| git checkout master | ||
|
|
@@ -137,10 +130,10 @@ kustomize-ga-apply: # (Kustomize) Run kubectl apply -k on the connected k8s clu | |
| kubectl apply -k Resources/k8s/kustomize/ga/ | ||
|
|
||
| k8s-migrate: # (k8s) Run any pending migrations | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- python manage.py migrate ${docker_config} | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- uv run manage.py migrate ${docker_config} | ||
|
|
||
| k8s-build-db: # (k8s) Build the database | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${docker_config}' | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell ${docker_config}' | ||
|
|
||
| k8s-delete: # (k8s) Delete pokeapi namespace | ||
| kubectl delete namespace pokeapi | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,9 @@ A RESTful API for Pokémon - [pokeapi.co](https://pokeapi.co) | |
|
|
||
| - Download this source code into a working directory, be sure to use the flag `--recurse-submodules` to clone also our submodules. | ||
|
|
||
| - Install the requirements using pip: | ||
| - Install [uv](https://docs.astral.sh/uv/getting-started/installation/) for Python environment management. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't paricularily like adding new toold to projects. I don't see the benefits of using My argument is: Python officially already ships with uv-like features. It has
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One very good added benefit of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: "forcing everyone to install it on their machines" I'd recommend actually configuring the core tools via
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uv is becoming an industry standard, I use it at my company (3,000+ python engineers, one of the biggest Django projects in the world, we even have a few Python and Django core devs here). Why uv?
For years PokeAPI has been a good example of a great API project, and it has lagged behind in being a great example of a Python project, and it could be one with a bit of TLC - which is what my aim is this year. I think providing up to date tools is a win win for us (easier to maintain) and also people browsing our code (they see what is standard). |
||
|
|
||
| - Install the requirements using the Makefile commands: | ||
|
|
||
| ```sh | ||
| make install | ||
|
|
@@ -97,8 +99,8 @@ If you don't have `make` on your machine you can use the following commands | |
|
|
||
| ```sh | ||
| docker compose up -d | ||
| docker compose exec -T app python manage.py migrate --settings=config.docker-compose | ||
| docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose' | ||
| docker compose exec -T app uv run manage.py migrate --settings=config.docker-compose | ||
| docker compose exec -T app sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell --settings=config.docker-compose' | ||
| ``` | ||
|
|
||
| Browse [localhost/api/v2/](http://localhost/api/v2/) or [localhost/api/v2/pokemon/bulbasaur/](http://localhost/api/v2/pokemon/bulbasaur/) on port `80`. | ||
|
|
@@ -149,8 +151,8 @@ Configure `kubectl` to point to a cluster and then run the following commands to | |
| kubectl apply -k Resources/k8s/kustomize/base/ | ||
| kubectl config set-context --current --namespace pokeapi # (Optional) Set pokeapi ns as the working ns | ||
| # Wait for the cluster to spin up | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- python manage.py migrate --settings=config.docker-compose # Migrate the DB | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell --settings=config.docker-compose' # Build the db | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- uv run manage.py migrate --settings=config.docker-compose # Migrate the DB | ||
| kubectl exec --namespace pokeapi deployment/pokeapi -- sh -c 'echo "from data.v2.build import build_all; build_all()" | uv run manage.py shell --settings=config.docker-compose' # Build the db | ||
| kubectl wait --namespace pokeapi --timeout=120s --for=condition=complete job/load-graphql # Wait for Graphql configuration job to finish | ||
| ``` | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,21 @@ ENV PYTHONUNBUFFERED=1 | |
| RUN mkdir /code | ||
| WORKDIR /code | ||
|
|
||
| ADD requirements.txt /code/ | ||
| # Install UV | ||
| RUN apk add --no-cache curl | ||
| RUN curl -LsSf https://astral.sh/uv/install.sh | sh | ||
|
|
||
| # Add UV to PATH for this stage | ||
| ENV PATH="/root/.local/bin:$PATH" | ||
|
|
||
| # Copy dependency files | ||
| ADD pyproject.toml uv.lock* /code/ | ||
|
|
||
| # Install dependencies | ||
| RUN apk add --no-cache postgresql-libs libstdc++ | ||
| RUN apk add --no-cache --virtual .build-deps gcc g++ musl-dev \ | ||
| postgresql-dev binutils rust cargo && \ | ||
| python3 -m pip install -r requirements.txt --no-cache-dir | ||
| uv sync --frozen | ||
|
|
||
| FROM python:3.13.7-alpine | ||
|
|
||
|
|
@@ -19,15 +29,23 @@ ENV DJANGO_SETTINGS_MODULE='config.docker-compose' | |
| RUN mkdir /code | ||
| WORKDIR /code | ||
|
|
||
| COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages | ||
| COPY --from=builder /usr/local/bin /usr/local/bin | ||
| # Copy UV and virtual environment from builder | ||
| COPY --from=builder /root/.local /root/.local | ||
| COPY --from=builder /code/.venv /code/.venv | ||
|
|
||
| # Add UV to PATH | ||
| ENV PATH="/root/.local/bin:$PATH" | ||
|
|
||
| # Install runtime dependencies | ||
| RUN apk add --no-cache postgresql-libs libstdc++ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we need this two packages? We didn't need them before. Only for building the app at line 9
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will try and drop them and see if it works |
||
|
|
||
| # Copy application code | ||
| ADD . /code/ | ||
|
|
||
| RUN addgroup -g 1000 -S pokeapi && \ | ||
| adduser -u 1000 -S pokeapi -G pokeapi | ||
| USER pokeapi | ||
|
|
||
| CMD ["gunicorn", "config.wsgi:application", "-c", "gunicorn.conf.py"] | ||
| CMD ["uv", "run", "gunicorn", "config.wsgi:application", "-c", "gunicorn.conf.py"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way it'll use the venv to run, without any activate step |
||
|
|
||
| EXPOSE 80 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like a target that checks if
uvis installed and having it running before the other targets that requires it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea!