Skip to content

Commit

Permalink
Set uv as package manager (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
acasajus authored Jan 20, 2025
1 parent f02ea25 commit 2918251
Show file tree
Hide file tree
Showing 9 changed files with 2,549 additions and 51 deletions.
45 changes: 26 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@ jobs:
- name: Check out repo
uses: actions/checkout@v3

- name: "Install rye"
id: setup-rye
uses: eifinger/setup-rye@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
version: '0.43.0'
checksum: 'ca702c3d93fd6ec76a1a0efaaa605e10736ee79a0674d241aad1bc0fe26f7d80'
# Install a specific version of uv.
version: "0.5.21"
enable-cache: true

- name: Install OS dependencies
run: |
sudo apt update
sudo apt install -y libre2-dev libpq-dev
- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Install dependencies
if: steps.setup-rye.outputs.cache-hit != 'true'
run: rye sync --no-lock
if: steps.setup-uv.outputs.cache-hit != 'true'
run: uv sync --locked --all-extras

- name: Check formatting & linting
run: |
rye run pre-commit run --all-files
uv run pre-commit run --all-files
test:
Expand Down Expand Up @@ -70,23 +74,27 @@ jobs:
- name: Check out repo
uses: actions/checkout@v3

- name: Install rye
id: setup-rye
uses: eifinger/setup-rye@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
version: '0.43.0'
checksum: 'ca702c3d93fd6ec76a1a0efaaa605e10736ee79a0674d241aad1bc0fe26f7d80'
# Install a specific version of uv.
version: "0.5.21"
enable-cache: true
cache-prefix: 'rye-cache'

- name: Install OS dependencies
run: |
sudo apt update
sudo apt install -y libre2-dev libpq-dev
- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"

- name: Install dependencies
if: steps.setup-rye.outputs.cache-hit != 'true'
run: rye sync --no-lock
if: steps.setup-uv.outputs.cache-hit != 'true'
run: uv sync --locked --all-extras


- name: Start Redis v6
uses: superchargejs/[email protected]
Expand All @@ -95,8 +103,7 @@ jobs:

- name: Run db migration
run: |
rye install alembic
CONFIG=tests/test.env rye run alembic upgrade head
CONFIG=tests/test.env uv run alembic upgrade head
- name: Prepare version file
run: |
Expand All @@ -105,7 +112,7 @@ jobs:
- name: Test with pytest
run: |
rye run pytest
uv run pytest
env:
GITHUB_ACTIONS_TEST: true

Expand Down
29 changes: 18 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ SimpleLogin backend consists of 2 main components:
## Install dependencies

The project requires:
- Python 3.10 and rye to manage dependencies
- Python 3.10 and uv to manage dependencies
- Node v10 for front-end.
- Postgres 13+

First, install all dependencies by running the following command.
Feel free to use `virtualenv` or similar tools to isolate development environment.

```bash
rye sync
uv sync
```

On Mac, sometimes you might need to install some other packages via `brew`:
Expand All @@ -55,7 +55,7 @@ brew install -s re2 pybind11
We use pre-commit to run all our linting and static analysis checks. Please run

```bash
rye run pre-commit install
uv run pre-commit install
```

To install it in your development environment.
Expand Down Expand Up @@ -160,25 +160,25 @@ Here are the small sum-ups of the directory structures and their roles:
The code is formatted using [ruff](https://github.com/astral-sh/ruff), to format the code, simply run

```
rye run ruff format .
uv run ruff format .
```

The code is also checked with `flake8`, make sure to run `flake8` before creating the pull request by

```bash
rye run flake8
uv run flake8
```

For HTML templates, we use `djlint`. Before creating a pull request, please run

```bash
rye run djlint --check templates
uv run djlint --check templates
```

If some files aren't properly formatted, you can format all files with

```bash
rye run djlint --reformat .
uv run djlint --reformat .
```

## Test sending email
Expand Down Expand Up @@ -236,11 +236,18 @@ There are several ways to setup Python and manage the project dependencies on Ma
# we haven't managed to make python 3.12 work
brew install python3.10

# Install rye using the official installation script, found on https://rye.astral.sh/guide/installation/
# make sure to update the PATH so python, pip point to Python3
# for us it can be done by adding "export PATH=/opt/homebrew/opt/[email protected]/libexec/bin:$PATH" to .zprofile

# Install the dependencies
rye sync
# Although pipx is the recommended way to install uv,
# install pipx via brew will automatically install python 3.12
# and uv will then use python 3.12
# so we recommend using uv this way instead
curl -sSL https://install.python-uv.org | python3 -

uv install

# activate the virtualenv and you should be good to go!
source .venv/bin/activate
```

```
22 changes: 12 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ RUN cd /code/static && npm ci

FROM --platform=linux/amd64 ubuntu:22.04

ARG RYE_VERSION="0.43.0"
ARG RYE_HASH="ca702c3d93fd6ec76a1a0efaaa605e10736ee79a0674d241aad1bc0fe26f7d80"
ARG UV_VERSION="0.5.21"
ARG UV_HASH="e108c300eafae22ad8e6d94519605530f18f8762eb58d2b98a617edfb5d088fc"

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
Expand All @@ -18,18 +18,20 @@ ENV PYTHONUNBUFFERED=1
WORKDIR /code

# Copy dependency files
COPY pyproject.toml requirements.lock requirements-dev.lock .python-version ./
COPY pyproject.toml uv.lock .python-version ./

# Install deps
RUN apt-get update \
&& apt-get install -y curl netcat-traditional gcc python3-dev gnupg git libre2-dev build-essential pkg-config cmake ninja-build bash clang \
&& curl -sSL "https://github.com/astral-sh/rye/releases/download/${RYE_VERSION}/rye-x86_64-linux.gz" > rye.gz \
&& echo "${RYE_HASH} rye.gz" | sha256sum -c - \
&& gunzip rye.gz \
&& chmod +x rye \
&& mv rye /usr/bin/rye \
&& rye toolchain fetch `cat .python-version` \
&& rye sync --no-lock --no-dev \
&& curl -sSL "https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/uv-x86_64-unknown-linux-gnu.tar.gz" > uv.tar.gz \
&& echo "${UV_HASH} uv.tar.gz" | sha256sum -c - \
&& tar xf uv.tar.gz -C /tmp/ \
&& mv /tmp/uv-x86_64-unknown-linux-gnu/uv /usr/bin/uv \
&& mv /tmp/uv-x86_64-unknown-linux-gnu/uvx /usr/bin/uvx \
&& rm -rf /tmp/uv* \
&& rm -f uv.tar.gz \
&& uv python install `cat .python-version` \
&& uv sync --locked \
&& apt-get autoremove -y \
&& apt-get purge -y curl netcat-traditional build-essential pkg-config cmake ninja-build python3-dev clang\
&& apt-get autoremove -y \
Expand Down
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ dependencies = [
"watchtower ~= 0.8.0",
"sqlalchemy-utils == 0.36.8",
"jwcrypto ~= 0.8",
"yacron>=0.19.0",
"yacron~=0.11.2",
"flask-debugtoolbar ~= 0.11.0",
"requests_oauthlib ~= 1.3.0",
"pyopenssl ~= 19.1.0",
"aiosmtpd ~= 1.2",
"dnspython == 2.6.1",
"dnspython==2.0.0",
"coloredlogs ~= 14.0",
"pycryptodome ~= 3.9.8",
"phpserialize ~= 1.3",
Expand Down Expand Up @@ -71,11 +71,12 @@ dependencies = [
"MarkupSafe~=1.1.1",
"cryptography ~= 37.0.1",
"SQLAlchemy ~= 1.3.24",
"redis ~= 4.5.3",
"redis==4.6.0",
"newrelic-telemetry-sdk ~= 0.5.0",
"aiospamc == 0.10",
"itsdangerous ~= 1.1.0",
"werkzeug ~= 1.0.1",
"alembic ~= 1.4.3",
]

[tool.black]
Expand Down Expand Up @@ -128,7 +129,7 @@ dev-dependencies = [
"pytest-cov ~= 3.0.0",
"pre-commit ~= 2.17.0",
"black ~= 22.1.0",
"djlint ~= 1.3.0",
"djlint==1.34.1",
"pylint ~= 2.14.4",
"ruff ~= 0.1.5",
]
Expand Down
4 changes: 2 additions & 2 deletions scripts/new-migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ docker run -p 25432:5432 --name ${container_name} -e POSTGRES_PASSWORD=postgres
sleep 3

# upgrade the DB to the latest stage and
env DB_URI=postgresql://postgres:[email protected]:25432/sl rye run alembic upgrade head
env DB_URI=postgresql://postgres:[email protected]:25432/sl uv run alembic upgrade head

# generate the migration script.
env DB_URI=postgresql://postgres:[email protected]:25432/sl rye run alembic revision --autogenerate $@
env DB_URI=postgresql://postgres:[email protected]:25432/sl uv run alembic revision --autogenerate $@

# remove the db
docker rm -f ${container_name}
4 changes: 2 additions & 2 deletions scripts/reset_local_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
export DB_URI=postgresql://myuser:mypassword@localhost:15432/simplelogin
echo 'drop schema public cascade; create schema public;' | psql $DB_URI

rye run alembic upgrade head
rye run flask dummy-data
uv run alembic upgrade head
uv run flask dummy-data
2 changes: 1 addition & 1 deletion scripts/reset_test_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
export DB_URI=postgresql://myuser:mypassword@localhost:15432/test
echo 'drop schema public cascade; create schema public;' | psql $DB_URI

rye run alembic upgrade head
uv run alembic upgrade head
4 changes: 2 additions & 2 deletions scripts/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ docker run -d --name sl-test-db -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test
sleep 3

# migrate the DB to the latest version
CONFIG=tests/test.env rye run alembic upgrade head
CONFIG=tests/test.env uv run alembic upgrade head

# run test
rye run pytest -c pytest.ci.ini
uv run pytest -c pytest.ci.ini

# Delete the test DB
docker rm -f sl-test-db
Loading

0 comments on commit 2918251

Please sign in to comment.