Skip to content

Commit

Permalink
Merge pull request #49 from numerique-gouv/poetry
Browse files Browse the repository at this point in the history
Passage à poetry
  • Loading branch information
Ash-Crow authored Nov 21, 2023
2 parents 821b7e8 + d0d169b commit 072dec0
Show file tree
Hide file tree
Showing 13 changed files with 1,788 additions and 106 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
SECRET_KEY=
DEBUG=False
HOST_PROTO=http
HOST_URL=localhost
HOST_URL=localhost # use 0.0.0.0 for Docker
ALLOWED_HOSTS=localhost, 0.0.0.0
HOST_PORT=8000

USE_DOCKER=0 # Set 1 to use Docker

DATABASE_NAME=djdb
DATABASE_USER=dju
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
PYTHONPATH: .
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.10", "3.11"]
services:
postgres:
image: postgres:14-alpine
Expand All @@ -28,7 +28,9 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: 🌍 Install dependencies
run: |
pip install -r requirements.txt
python -m pip install --upgrade pip
pip install poetry
poetry install --no-root
- name: 📄 Copy empty .env.test to .env
run: |
cp .env.test .env
Expand All @@ -37,10 +39,9 @@ jobs:
make quality
- name: 🚧 Check pending migrations
run: |
django-admin makemigrations --check --dry-run --noinput
poetry run django-admin makemigrations --check --dry-run --noinput
- name: 🤹‍ Run the unit tests
run: |
make test-unit
env:
DJANGO_DEBUG: True
USE_VENV: 1
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
Expand Down
28 changes: 18 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
FROM python:3.10

EXPOSE 8000
EXPOSE ${HOST_PORT}

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV GECKODRIVER_URL=https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux32.tar.gz
ENV APP_DIR="/app"

# For behave tests
RUN apt-get update && apt-get install -y --no-install-recommends firefox-esr
RUN wget -qO- $GECKODRIVER_URL | tar xvz -C /usr/bin/
# Configure Poetry
ENV POETRY_VERSION=1.7.0
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VENV=/opt/poetry-venv
ENV POETRY_CACHE_DIR=/opt/.cache

# Add new user to run the whole thing as non-root.
RUN set -ex \
&& addgroup app \
&& adduser --ingroup app --home $APP_DIR --disabled-password app;
&& adduser --ingroup app --home ${APP_DIR} --disabled-password app;

RUN python -m pip install --upgrade pip
# Install poetry separated from system interpreter
RUN python3 -m venv ${POETRY_VENV} \
&& ${POETRY_VENV}/bin/pip install -U pip setuptools \
&& ${POETRY_VENV}/bin/pip install poetry==${POETRY_VERSION}

# Add `poetry` to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"

WORKDIR $APP_DIR

COPY requirements.txt .
RUN pip install -r requirements.txt
COPY pyproject.toml poetry.lock .
RUN poetry install

COPY --chown=app:app . .

RUN python manage.py collectstatic --no-input
RUN poetry run python manage.py collectstatic --no-input

USER app

ENTRYPOINT ["./entrypoint.sh"]

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
CMD ["poetry", "run", "python", "manage.py", "runserver", "0.0.0.0:${HOST_PORT}"]
43 changes: 27 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
ifeq ($(USE_VENV),1)
EXEC_CMD :=
else
# Loading environment variables
ifneq (,$(wildcard ./.env))
include .env
export
endif

ifeq ($(USE_DOCKER),1)
EXEC_CMD := docker-compose exec -ti web
else
EXEC_CMD :=
endif

.PHONY: web-prompt
Expand All @@ -10,22 +16,27 @@ web-prompt:

.PHONY: test-unit
test-unit:
$(EXEC_CMD) python manage.py test --settings config.settings_test

.PHONY: test-e2e
test-e2e:
$(EXEC_CMD) python manage.py behave --settings config.settings_test

.PHONY: test
test: test-e2e test-unit
$(EXEC_CMD) poetry run python manage.py test --settings config.settings_test

.PHONY: quality
quality:
$(EXEC_CMD) black --check --exclude=venv .
$(EXEC_CMD) isort --check --skip-glob="**/migrations" --extend-skip-glob="venv" .
$(EXEC_CMD) flake8 --count --show-source --statistics --exclude=venv .
$(EXEC_CMD) poetry run black --check --exclude=venv .
$(EXEC_CMD) poetry run isort --check --skip-glob="**/migrations" --extend-skip-glob="venv" .
$(EXEC_CMD) poetry run flake8 --count --show-source --statistics --exclude="venv,**/migrations" .

.PHONY: fix
fix:
$(EXEC_CMD) black --exclude=venv .
$(EXEC_CMD) isort --skip-glob="**/migrations" --extend-skip-glob="venv" .
$(EXEC_CMD) poetry run black --exclude=venv .
$(EXEC_CMD) poetry run isort --skip-glob="**/migrations" --extend-skip-glob="venv" .


.PHONY: init
init:
$(EXEC_CMD) poetry install
$(EXEC_CMD) poetry run pre-commit install
$(EXEC_CMD) poetry run python manage.py migrate
$(EXEC_CMD) poetry run python manage.py collectstatic --noinput

.PHONY: runserver
runserver:
$(EXEC_CMD) poetry run python manage.py runserver $(HOST_URL):$(HOST_PORT)
62 changes: 21 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,49 @@ pre-commit run --all-files

Le projet peut se lancer en local ou avec Docker.

### En local

#### Créer un environnement virtuel

```
# Configurer et activer l'environnement virtuel
python -m venv venv
. venv/bin/activate
# Installer les packages requis
pip install -r requirements.txt
```

#### Copier les variables d'environnement
### Dans tous les cas, copier les variables d’environnement

- Copier le fichier
```
cp .env.example .env
```

#### Lancer le serveur

- Générer la `SECRET_KEY`
```
python manage.py runserver
python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"
```

#### Lancer les migrations
- Mettre les valeurs pertinentes dans le fichier `.env`

```
python manage.py migrate
```
### En local
#### Installer poetry s’il ne l’est pas

#### Effectuer les tests
Cf. la [documentation de poetry](https://python-poetry.org/docs/#installation)

D'abord installer les dépendances de test :
#### Installer le projet

```sh
pip install -r requirements.txt
- La commande suivante installe les dépendances, fait les migrations et collecte les fichiers
```
make init
```

Les tests unitaires peuvent être lancés avec `make test-units`, les
tests E2E avec `make test-e2e`, les deux avec `make test`.

Pour les tests E2E, si vous n'utilisez pas Docker, il vous faudra
[Firefox](https://www.mozilla.org/fr/firefox/download/thanks/) et
[`geckodriver`](https://github.com/mozilla/geckodriver/releases)
accessibles sur votre machine pour lancer les tests E2E. Sur MacOS,
vous pouvez les installer via [brew](https://brew.sh/) avec la commande: `brew install geckodriver`.
#### Lancer le serveur

Vous pouvez également générer un rapport sur la couverture de tests :
```sh
coverage run manage.py test --settings config.settings_test
```
make runserver
```

### via Docker

#### Copier les variables d'environnement
#### Lancer les containers

```sh
cp .env.example .env
docker compose up
```

#### Lancer les containers
### Effectuer les tests
Les tests unitaires peuvent être lancés avec `make test-unit`.

Vous pouvez également générer un rapport sur la couverture de tests :
```sh
docker-compose up
coverage run manage.py test --settings config.settings_test
```
6 changes: 2 additions & 4 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True if os.getenv("DEBUG") == "True" else False

HOST_URL = os.getenv("HOST_URL", "127.0.0.1, localhost")

ALLOWED_HOSTS = HOST_URL.replace(" ", "").split(",")
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "127.0.0.1, localhost").replace(" ", "").split(",")

# Application definition

Expand Down Expand Up @@ -195,7 +193,7 @@

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
WAGTAILADMIN_BASE_URL = f"{os.getenv('HOST_PROTO', 'https')}://{HOST_URL[-1]}"
WAGTAILADMIN_BASE_URL = f"{os.getenv('HOST_PROTO', 'https')}://{os.getenv('HOST_URL', 'localhost')}"

# Disable Gravatar service
WAGTAIL_GRAVATAR_PROVIDER_URL = None
Expand Down
15 changes: 8 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ services:
db:
image: postgres:14.5-alpine
environment:
POSTGRES_USER: dju
POSTGRES_PASSWORD: djpwd
POSTGRES_DB: djdb
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
PGPORT: ${DATABASE_PORT}
ports:
- "5432:5432"
- "${DATABASE_PORT}:${DATABASE_PORT}"
volumes:
- postgres_data:/var/lib/postgresql/data

web:
build: .
environment:
DATABASE_URL: postgres://dju:djpwd@db:5432/djdb
DATABASE_URL: postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@db:${DATABASE_PORT}/${DATABASE_NAME}
ports:
- "8000:8000"
- "${HOST_PORT}:${HOST_PORT}"
volumes:
- .:/app
depends_on:
- db

volumes:
postgres_data:
postgres_data:
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -l
set -ex

python manage.py migrate
export PATH="${PATH}:${POETRY_VENV}/bin"
poetry run python manage.py migrate

exec "$@"
Loading

0 comments on commit 072dec0

Please sign in to comment.