Skip to content

Commit

Permalink
chore: switch to UV
Browse files Browse the repository at this point in the history
Close #236
  • Loading branch information
Jenselme committed Dec 22, 2024
1 parent 710a574 commit 91afd9d
Show file tree
Hide file tree
Showing 23 changed files with 2,324 additions and 3,657 deletions.
6 changes: 3 additions & 3 deletions .devcontainer/complete-creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ set -o nounset
# For VSCode extension used to edit sphinx projects.
pip install esbonio

python manage.py createcachetable
python manage.py migrate
uv run python manage.py createcachetable
uv run python manage.py migrate

npm install

Expand All @@ -21,7 +21,7 @@ function setup-pre-commit() {
sleep 30
done

pre-commit install --hook-type pre-commit --hook-type pre-push
uv run pre-commit install --hook-type pre-commit --hook-type pre-push
}

# Run in the backend to allow VSCode to configure git.
Expand Down
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
.pre-commit-config.yaml
.readthedocs.yml
.travis.yml
venv
venv/
.venv/
.git/
devops/envs/
.pytest_cache/
Expand Down
3 changes: 2 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dotenv devops/envs/local/django
dotenv devops/envs/local/pycharm

layout poetry
uv sync
source .venv/bin/activate
71 changes: 44 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,66 @@ concurrency:
cancel-in-progress: true

jobs:
linter:
lint-and-test:
runs-on: ubuntu-latest
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_DB: legadilo
POSTGRES_USER: django
POSTGRES_PASSWORD: django_passwd
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432

env:
IS_PRODUCTION: "false"
PYTHONUNBUFFERED: "1"
PYTHONDEVMODE: "1"
USE_DOCKER: "no"
DATABASE_URL: "postgres://django:django_passwd@localhost:5432/legadilo"

steps:
- name: Checkout Code Repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
python-version: "3.12"
# Install a specific version of uv.
version: "0.5.8"

- name: Install poetry
run: pip install poetry==1.8.2
- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Install typing deps
run: poetry install --with typing
- name: Install the project
run: uv sync --dev --locked

# Consider using pre-commit.ci for open source project
- name: Run pre-commit
uses: pre-commit/[email protected]

# With no caching at all the entire ci process takes 4m 30s to complete!
pytest:
runs-on: ubuntu-latest

steps:
- name: Checkout Code Repository
uses: actions/checkout@v4

- name: Build the Stack
run: docker compose -f local.yml build
run: uv run pre-commit run -a

- name: Create cache table
run: docker compose -f local.yml run --rm django python manage.py createcachetable
run: uv run python manage.py createcachetable

- name: Run DB Migrations
run: docker compose -f local.yml run --rm django python manage.py migrate
run: uv run python manage.py migrate

- name: Run Django Tests
run: docker compose -f local.yml run --rm django pytest --cov --cov-report term:skip-covered --cov-fail-under=90

- name: Tear down the Stack
run: docker compose -f local.yml down
run: uv run pytest --cov --cov-report term:skip-covered --cov-fail-under=90

- name: Test build production images
run: |
Expand Down
4 changes: 2 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/runConfigurations/Run_full_test_suite.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/runConfigurations/Runserver__with_rich_logs_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/runConfigurations/_template__of_py_test.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ repos:
- IS_PRODUCTION=True
- DJANGO_SECRET_KEY=test-key
- DJANGO_ADMIN_URL=admin/
- poetry
- uv
- run
- mypy
- legadilo
Expand All @@ -141,7 +141,7 @@ repos:
name: pytest
entry: env
args:
- poetry
- uv
- run
- pytest
language: system
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

## Unreleased

## 24.12.5

- Add link to changelog in footer.
- Improve existing links in footer.
- Allow you to add a custom script.
- Can fetch feed without an explicit full site URL.
- Can force feeds to refresh in the admin.
- Can refresh a reading list no mobile easily, without going to the top of the page or opening the reading list selector.
- Switch to [`uv`](docs.astral.sh/uv/) to manage dependencies.

## 24.12.4

Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ You will have to start Django with the provided run target.

### Using Pycharm

By default, everything is set up to develop locally with Pycharm. So you will need docker (for the database), poetry, Python 3.12 and nodeJS 20+ installed for this to work. Django will be started automatically. On the first run, you must run `npm install` to install a few JS deps.
By default, everything is set up to develop locally with Pycharm. So you will need docker (for the database), [uv](https://docs.astral.sh/uv/), Python 3.12 and nodeJS 20+ installed for this to work.
Django will be started automatically.
On the first run, you must run `npm install` to install a few JS deps and `uv run pre-commit install --hook-type pre-commit --hook-type pre-push` to configure `pre-commit`.

You should also be able to use devcontainers but the support is more recent and isn’t as good as in VSCode according to my tests. See [here](https://www.jetbrains.com/help/pycharm/connect-to-devcontainer.html) for more.
You should also be able to use devcontainers but the support is more recent and isn’t as good as in VSCode according to my tests.
See [here](https://www.jetbrains.com/help/pycharm/connect-to-devcontainer.html) for more.

### Project structure

Expand All @@ -46,16 +49,16 @@ You should also be able to use devcontainers but the support is more recent and
- `.decontainer/`, `.idea/`, `.vscode/` and `.editorconfig` are editors configuration.
- `.eslintrc.json`, `prettierrc.json` and `.stylelintrc.json` contains the JS/CSS linters and formatters configurations.
- `pyproject.toml` defines the Python dependencies and is used to configure Python linting tools.
- `poetry.lock` and `package-lock.json` are used to lock the dependencies.
- `uv.lock` and `package-lock.json` are used to lock the dependencies.

### Basic Commands

All these commands must be run at the root of the project!

- Run the server: `python manage.py runserver`
- Create migrations files after updating models: `python manage.py makemigrations`
- Apply migrations: `python manage.py migrate`
- Create a _superuser_: `python manage.py createsuperuser`
- Run the server: `uv run python manage.py runserver`
- Create migrations files after updating models: `uv run python manage.py makemigrations`
- Apply migrations: `uv run python manage.py migrate`
- Create a _superuser_: `uv run python manage.py createsuperuser`

### Email Server

Expand All @@ -71,7 +74,7 @@ With Mailpit running, to view messages that are sent by your application, open y
- To create a **normal user account**, just go to Sign Up and fill out the form. Once you submit it, you'll see a "Verify Your E-mail Address" page. Go to Mailpit to see a simulated email verification message. Copy the link into your browser. Now the user's email should be verified and ready to go.
- To create a **superuser account**, use this command:

$ python manage.py createsuperuser
$ uv run python manage.py createsuperuser

For convenience, you can keep your normal user logged in on Chrome and your superuser logged in on Firefox (or similar), so that you can see how the site behaves for both kinds of users.

Expand Down
20 changes: 13 additions & 7 deletions devops/compose/local/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ ARG BUILD_ENVIRONMENT=local
ARG APP_HOME=/app
ARG PYTHONDEVMODE=1

ENV UV_LINK_MODE copy
ENV UV_COMPILE_BYTECODE 1
ENV UV_PYTHON_DOWNLOADS never
ENV UV_PROJECT_ENVIRONMENT ${APP_HOME}/.venv
ENV UV_PYTHON /usr/local/bin/python
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONDEVMODE ${PYTHONDEVMODE}
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
ENV CREATE_VENV 0
# make sure we use the virtualenv by default
ENV PATH "${APP_HOME}/.venv/bin:$PATH"

WORKDIR ${APP_HOME}

COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

COPY ./devops/compose/production/django/setup-container.sh /
RUN chmod +x /setup-container.sh
RUN /setup-container.sh
Expand All @@ -25,12 +33,10 @@ RUN chmod +x /start.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/start.sh"]

COPY ./devops/compose/production/django/setup-project.sh /
COPY pyproject.toml .
COPY poetry.lock .
COPY README.md .
RUN chmod +x /setup-project.sh
RUN /setup-project.sh
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project --dev

COPY ./devops/compose/production/django/setup-django.sh /
RUN chmod +x /setup-django.sh
Expand Down
12 changes: 6 additions & 6 deletions devops/compose/local/docs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN apt-get update && apt-get install --no-install-recommends -y \
git \
# To run the Makefile
make \
# Translations dependencies
Expand All @@ -19,12 +20,11 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY pyproject.toml .
COPY poetry.lock .
RUN pip install -U pip && \
pip install poetry==1.8.2 && \
poetry config virtualenvs.create false && \
poetry install --with dev --with typing --with doc
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project --dev --group doc

COPY ./devops/compose/local/docs/start.sh /start-docs.sh
RUN chmod +x /start-docs.sh
Expand Down
2 changes: 1 addition & 1 deletion devops/compose/local/docs/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -o errexit
set -o pipefail
set -o nounset

exec make livehtml
exec uv run make livehtml
Loading

0 comments on commit 91afd9d

Please sign in to comment.