Skip to content
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

Migrate to UV #2282

Merged
merged 29 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a7a6621
uv spike
chris48s Sep 26, 2024
7d853ad
set requires-python to python 3.12.x
chris48s Oct 16, 2024
915e49e
allow prereleases in config file
chris48s Oct 16, 2024
7528927
we don't use black anymore
chris48s Oct 16, 2024
c9972e5
project metadata
chris48s Oct 16, 2024
73ae578
bump dc_django_utils
chris48s Oct 16, 2024
c7e9d0c
bump django
chris48s Oct 16, 2024
8a69cf2
whitespace
chris48s Oct 16, 2024
6d5d0d1
remove unnecessary package declarations
chris48s Oct 16, 2024
f554139
move debug toolbar to dev-dependencies, remove 'local' optional group
chris48s Oct 16, 2024
eefd6f3
delete old requirements files
chris48s Oct 16, 2024
1b0e3a6
update install docs
chris48s Oct 16, 2024
7ad181a
delete mock package
chris48s Oct 17, 2024
4022a60
safety: upgrade gunicorn
chris48s Oct 23, 2024
a7d51c9
safety: remove packages we don't use anymore, extend jinja2 ignore
chris48s Oct 23, 2024
d847929
install pyyaml in cdk group
chris48s Oct 23, 2024
315ee6b
CI build
chris48s Oct 23, 2024
1e57ca1
move uv install to script
chris48s Oct 23, 2024
c85d8a3
install packages using uv in deploy, move gevent to prod optional group
chris48s Oct 23, 2024
ea89bad
install uv in the global env with pip instead of curl bashing
chris48s Oct 23, 2024
d32a8a0
use normalized package names
chris48s Oct 23, 2024
32f302a
set up a sensible-ish dependabot config
chris48s Oct 23, 2024
69c7155
add a workflow to re-generate uv.lock on dependabot PRs
chris48s Oct 23, 2024
f43426b
bump versions
chris48s Oct 23, 2024
c5b78be
uv 0.4.27, dependency-groups
chris48s Oct 31, 2024
f3d3075
add uv cache dir to circle cache
chris48s Oct 31, 2024
f2138a2
install and run coveralls in one-liner
chris48s Oct 31, 2024
bd91e5c
remove pyproject.toml hash from cache key
chris48s Oct 31, 2024
165b0fd
tell uv to only use the python we specify in circle image
chris48s Nov 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
node-version: '18'

- restore_cache:
key: v3-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
key: v3-dependencies-{{ checksum "uv.lock" }}

- run:
name: Install node node_modules
Expand All @@ -35,22 +35,15 @@ jobs:
name: Install app dependencies
command: |
sudo apt update && sudo apt install -y gdal-bin python3-gdal python3-dev
pyenv local $(ls ~/.pyenv/versions/)
python -m venv .venv
deployscripts/install_uv.sh
uv venv --verbose --no-python-downloads
. .venv/bin/activate
pip install --upgrade pip
pip install coveralls wheel
pip install -r requirements/testing.txt
uv sync --group cdk --group production
- run:
name: Install Playwright
command: |
. .venv/bin/activate
playwright install
- run:
name: Install CDK Python dependencies
command: |
. .venv/bin/activate
pip install -r requirements/cdk.txt
- run:
name: Pip safety check
command: |
Expand All @@ -60,7 +53,8 @@ jobs:
paths:
- ./.venv
- ./node_modules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding $HOME/.cache/uv here will mean that uv doesn't need to re-download packages between runs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 9d49161

key: v3-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
- $HOME/.cache/uv
key: v3-dependencies-{{ checksum "uv.lock" }}

- run:
name: Pre-test checks
Expand All @@ -70,7 +64,8 @@ jobs:
python manage.py --version
python manage.py check
python manage.py makemigrations --check
pip check
uv lock --locked
chris48s marked this conversation as resolved.
Show resolved Hide resolved
uv pip check
ruff format . --check
ruff check .
git ls-files '*.html' | xargs djhtml --check
Expand All @@ -85,7 +80,7 @@ jobs:
name: Submit coverage
command: |
. .venv/bin/activate
coveralls
uv run --with coveralls coveralls

- store_artifacts:
path: test-results
Expand All @@ -107,20 +102,21 @@ jobs:
steps:
- checkout
- restore_cache:
key: v3-machine-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
key: v3-machine-dependencies-{{ checksum "uv.lock" }}
- run:
name: Install CDK Python dependencies
command: |
pyenv local $(ls -1 /opt/circleci/.pyenv/versions | grep 3.12)
python -m venv .venv
deployscripts/install_uv.sh
uv venv --verbose --no-python-downloads
. .venv/bin/activate
pip install -r requirements/cdk.txt
uv sync --only-group cdk

- save_cache:
paths:
- ./.venv
- ./node_modules
key: v3-machine-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
- $HOME/.cache/uv
key: v3-machine-dependencies-{{ checksum "uv.lock" }}

- run:
name: CDK version
Expand Down Expand Up @@ -156,17 +152,16 @@ jobs:
steps:
- checkout
- restore_cache:
key: v3-machine-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
key: v3-machine-dependencies-{{ checksum "uv.lock" }}
- run:
name: CDK deploy
command: |
pyenv local $(ls -1 /opt/circleci/.pyenv/versions | grep 3.12)
. .venv/bin/activate
npx cdk deploy --all --require-approval never --concurrency 3
- save_cache:
paths:
- .cdk.out
key: v3-machine-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
key: v3-machine-dependencies-{{ checksum "uv.lock" }}
- slack/notify:
event: fail
template: basic_fail_1
Expand All @@ -192,11 +187,10 @@ jobs:
- node/install:
node-version: '18'
- restore_cache:
key: v3-dependencies-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/cdk.txt" }}
key: v3-dependencies-{{ checksum "uv.lock" }}
- run:
name: "Code Deploy: Create deployment group"
command: |
pyenv local $(ls ~/.pyenv/versions/)
. .venv/bin/activate
python deployscripts/create_deployment_group.py
- run:
Expand Down
22 changes: 14 additions & 8 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
interval: "daily"
groups:
cdk-libs:
patterns:
- "aws-cdk*"
- "constructs"
boto-libs:
patterns:
- "boto*"
chris48s marked this conversation as resolved.
Show resolved Hide resolved
ignore:
- dependency-name: boto3
- dependency-name: "boto*"
update-types: ["version-update:semver-patch"]
- dependency-name: botocore
update-types: ["version-update:semver-patch"]
- dependency-name: django
- dependency-name: "django"
versions:
- ">= 3.a"
- "< 4.2"
# We don't want notifications about Django 5
# until 5.2 LTS is released in 2025
- ">= 5.0"
29 changes: 29 additions & 0 deletions .github/workflows/uv-lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: UV Lock
chris48s marked this conversation as resolved.
Show resolved Hide resolved

on:
push:
branches:
- 'dependabot/**'

permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- uses: actions/setup-python@v5
with:
python-version: '3.12'

- run: ./deployscripts/install_uv.sh

- run: uv lock

- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update uv.lock
8 changes: 1 addition & 7 deletions .safety-policy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ security: # configuration for the `safety check` command
65213: # vulnerability in the POLY1305 MAC algorithm on PowerPC CPUs
reason: we do not use the vulnerable function
expires: '2025-01-20' # Bump if still applies
66742: # Don't do stupid things with Black. We don't. Remove when we go to ruff
reason: we do not use loads of leading tabs
expires: '2025-01-20' # Bump if still applies
67599: # Apparently all versions of pip are bad.
reason: We need to be able to pip install things.
expires: '2024-08-20' # Bump if still applies
70612: # jinja2 version 3.1.4
reason: Only used in tests, no fixed version available, maintainer disputes it is a vuln
expires: '2024-10-20' # Bump if still applies
expires: '2025-01-20' # Bump if still applies
continue-on-vulnerability-error: False # Suppress non-zero exit codes when vulnerabilities are found. Enable this in pipelines and CI/CD processes if you want to pass builds that have vulnerabilities
8 changes: 5 additions & 3 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ EveryElection requires Python 3.x and Postgres.

## Install Python dependencies

Every Election uses [uv](https://docs.astral.sh/uv/) to manage python packages.
chris48s marked this conversation as resolved.
Show resolved Hide resolved
[Install uv](https://docs.astral.sh/uv/getting-started/installation/) first if you don't already have it. Then

```
pip install -U pip
pip install -r requirements.txt
pip install -r requirements/local.txt
uv sync
```

Gotcha: If you're having trouble installing psycopg2-binary on Apple silicon, set your library path before retrying the install:
```commandline
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/opt/openssl/lib
Expand Down
3 changes: 3 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ hooks:
# During the AfterInstall deployment lifecycle event, run the commands
# in the script specified in "location".
AfterInstall:
- location: deployscripts/install_uv.sh
timeout: 300
runas: root
- location: deployscripts/install_python_deps.sh
timeout: 300
runas: every_election
Expand Down
2 changes: 1 addition & 1 deletion cdk_settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"base_ami_id": "ami-0c40c7ef04b5ab961",
"recipe_version": "0.0.35"
"recipe_version": "0.0.41"
chris48s marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion cdk_stacks/components/add_user.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: AddUser
description: Adds a system user
schemaVersion: 1.0
component_version: 0.0.4
component_version: 0.0.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing.

When I pushed stuff to the dev AWS account to test it, the image builder threw errors saying there was already a different 0.0.4 of cdk_stacks/components/add_user.yml and already a different 0.0.2 of cdk_stacks/components/instance_connect.yml, so that's why I've bumped those two despite there being no other changes in those files.

I don't really know what a good pattern is for managing this stuff, but given I don't think there's really any downside to bumping a version even if nothing changed, keeping them aligned across AWS accounts seems sensible to me.

parameters:
- username:
type: string
Expand Down
9 changes: 4 additions & 5 deletions cdk_stacks/components/install_app.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: InstallApp
description: Installs the EE Django app
schemaVersion: 1.0
component_version: 0.0.22
component_version: 0.0.27
parameters:
- git_branch:
type: string
Expand Down Expand Up @@ -43,12 +43,11 @@ phases:
- git checkout {{ git_branch }}
- git pull --rebase origin {{ git_branch }}
- git branch
- python3 -m venv .venv
- ./deployscripts/install_uv.sh
chris48s marked this conversation as resolved.
Show resolved Hide resolved
- uv venv
- sudo chown -R {{ username }} /var/www/{{ username }}
- . .venv/bin/activate
- pip install -U pip
- pip install -r requirements/production.txt
- pip install gevent
- uv sync --group production --no-group dev --no-group cdk
- npm ci
- name: CreateEnvWriterFile
action: CreateFile
Expand Down
2 changes: 1 addition & 1 deletion cdk_stacks/components/instance_connect.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: InstallInstanceConnect
description: Installs AWS EC2 instance Connect
schemaVersion: 1.0
component_version: 0.0.2
component_version: 0.0.3
phases:
- name: build
steps:
Expand Down
6 changes: 2 additions & 4 deletions deployscripts/install_python_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ set -xeE

# should we delete the env and recreate?
cd /var/www/every_election/repo/
python3 -m venv .venv
uv venv
. .venv/bin/activate
pip install -U pip
pip install -r requirements/production.txt
pip install gevent
uv sync --group production --no-group dev --no-group cdk
10 changes: 10 additions & 0 deletions deployscripts/install_uv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -xeE

UV_CONSTRAINT=">=0.4.27,<0.5.0"

if [ "$CI" = "true" ]; then
pip install uv"$UV_CONSTRAINT"
else
sudo PIP_BREAK_SYSTEM_PACKAGES=1 pip install uv"$UV_CONSTRAINT"
fi
2 changes: 1 addition & 1 deletion every_election/apps/elections/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import mock
from unittest.mock import MagicMock

import mock
from django.test import TestCase
from elections import admin
from elections.models import ModerationHistory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import StringIO
from unittest import mock

import mock
from django.test import TestCase
from elections.management.commands import attach_posts_per_election_from_csv
from elections.models import Election
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import mock

from data_provider import base_data
from django.test import TestCase
from mock import mock
from organisations.boundaries.boundary_bot.scraper import (
LgbceScraper,
ScraperException,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from unittest import mock

from django.contrib.gis.geos import MultiPolygon
from django.test import TestCase
from organisations.boundaries.osni import OsniLayer
Expand Down
2 changes: 1 addition & 1 deletion every_election/apps/organisations/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from datetime import date
from unittest.mock import Mock, patch

import boto3
from botocore.exceptions import ClientError
from django.contrib.auth import get_user_model
from django.test import TestCase, override_settings
from django.urls import reverse
from elections.tests.factories import ElectedRoleFactory
from mock.mock import Mock, patch
from moto import mock_aws
from organisations.models import (
DivisionProblem,
Expand Down
Loading