Skip to content

Commit

Permalink
Replace pip and pip-tools with uv (mozilla#3357)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored Sep 20, 2024
1 parent 491399a commit e3e5935
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tag-admin/dist/
translate/dist/
coverage/
docs/_build/
docs/venv/
docs/*venv/
package-lock.json
specs/

Expand Down
20 changes: 11 additions & 9 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ on:
jobs:
test:
name: pytest
env:
UV_SYSTEM_PYTHON: 1
runs-on: ubuntu-latest
services:
postgres:
Expand All @@ -35,24 +37,24 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: pip
cache-dependency-path: |
requirements/default.txt
requirements/dev.txt
requirements/lint.txt
requirements/test.txt
- name: Set up uv
uses: astral-sh/setup-uv@v2
with:
enable-cache: true
version: "0.4.13"
- run: >
pip install
uv pip install
-r requirements/default.txt
-r requirements/dev.txt
-r requirements/test.txt
-r requirements/lint.txt
# Set environment variables
- run: pip install pytest-dotenv
- run: uv pip install pytest-dotenv
- run: >
sed
-e 's#^DATABASE_URL=.*#DATABASE_URL=postgres://pontoon:pontoon@localhost/pontoon#'
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/py-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Set up uv
uses: astral-sh/setup-uv@v2
with:
enable-cache: true
version: "0.4.13"
- name: Install Dependencies
run: |
pip install -U pip
pip install -r requirements/lint.txt
run: uv pip install -r requirements/lint.txt
env:
UV_SYSTEM_PYTHON: 1
- name: ruff lint
run: ruff check pontoon
- name: ruff format
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*.py[co]
*.sw[po]
/pontoon/coverage.xml
pip-log.txt
docs/_gh-pages
build.py
build
Expand All @@ -22,7 +21,7 @@ tmp/*
/static/
/.env
docs/_build
venv
*venv
/media/
.cache/
coverage/
Expand Down
20 changes: 11 additions & 9 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,12 @@ Direct dependencies for Pontoon are distributed across four files:
3. ``requirements/test.in``: Testing
4. ``requirements/lint.in``: Linting

In order to pin and hash the direct and indirect dependencies, we use `pip-compile <https://pypi.org/project/pip-tools/>`_,
which yields corresponding ``*.txt`` files. These ``*.txt`` files contain all direct and indirect dependencies,
and can be used for installation with ``pip``. After any change to the ``*.in`` files,
you should run the following command to update all ``requirements/*.txt`` files.
In order to pin and hash the direct and indirect dependencies, we use
`uv pip compile <https://docs.astral.sh/uv/#the-pip-interface>`_, which yields
corresponding ``*.txt`` files. These ``*.txt`` files contain all direct and
indirect dependencies, and can be used for installation with ``uv pip``. After any
change to the ``*.in`` files, you should run the following command to update all
``requirements/*.txt`` files.

.. code-block:: shell
Expand All @@ -247,7 +249,7 @@ Once you are done adding, removing or updating requirements, rebuild your docker
If there are problems, it'll tell you.

To upgrade existing dependencies within the given constraints of the input
files, you can pass options through to the ``pip-compile`` invocations, i.e.
files, you can pass options through to the ``uv pip compile`` invocations, i.e.

.. code-block:: shell
Expand All @@ -265,9 +267,9 @@ a virtualenv to build docs, do this:
.. code-block:: shell
$ cd docs/
$ virtualenv venv
$ source venv/bin/activate
$ pip install --require-hashes -r requirements.txt
$ uv venv
$ source .venv/bin/activate
$ uv pip install -r requirements.txt
Then, to build the docs, run this:

Expand Down Expand Up @@ -378,7 +380,7 @@ steps, as they don't affect your setup if nothing has changed:
git pull origin main
# Install new dependencies or update existing ones.
pip install -U --force --require-hashes -r requirements/default.txt
uv pip install -U --force -r requirements/default.txt
# Run database migrations.
python manage.py migrate
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ help:
@echo " dumpdb Create a postgres database dump with timestamp used as file name"
@echo " loaddb Load a database dump into postgres, file name in DB_DUMP_FILE"
@echo " sync-projects Runs the synchronization task on all projects"
@echo " requirements Compiles all requirements files with pip-compile\n"
@echo " requirements Compiles all requirements files with uv pip compile\n"

translate/dist:
make build-translate
Expand Down Expand Up @@ -143,5 +143,5 @@ sync-projects:

requirements:
# Pass --upgrade to upgrade all dependencies
# The arguments are passed through to pip-compile
# The arguments are passed through to `uv pip compile`
"${DC}" run --rm server //app/docker/compile_requirements.sh ${opts}
6 changes: 4 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ RUN apt-get update \
# Clean up what can be cleaned up.
&& apt-get autoremove -y

# Install uv
COPY --from=ghcr.io/astral-sh/uv:0.4.13 /uv /bin/uv

WORKDIR /app

# Install Pontoon Python requirements
COPY requirements/* /app/requirements/
RUN pip install -U 'pip>=8' && \
pip install --no-cache-dir --require-hashes -r requirements/default.txt -r requirements/dev.txt -r requirements/test.txt -r requirements/lint.txt
RUN uv pip install --system -r requirements/default.txt -r requirements/dev.txt -r requirements/test.txt -r requirements/lint.txt

# Create the app user
RUN groupadd -r --gid=${GROUP_ID} pontoon && useradd --uid=${USER_ID} --no-log-init -r -m -g pontoon pontoon
Expand Down
10 changes: 5 additions & 5 deletions docker/compile_requirements.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

# This compiles all requirements files with pip-compile.
# This compiles all requirements files with uv pip compile.
# You should always use this script, because dev.txt and test.txt depend on default.txt.

export CUSTOM_COMPILE_COMMAND="./docker/compile_requirements.sh"

pip-compile --generate-hashes --resolver=backtracking $@ requirements/default.in
pip-compile --generate-hashes --resolver=backtracking $@ requirements/dev.in
pip-compile --generate-hashes --resolver=backtracking $@ requirements/lint.in
pip-compile --generate-hashes --resolver=backtracking $@ requirements/test.in
uv pip compile --generate-hashes $@ requirements/default.in
uv pip compile --generate-hashes $@ requirements/dev.in
uv pip compile --generate-hashes $@ requirements/lint.in
uv pip compile --generate-hashes $@ requirements/test.in
41 changes: 17 additions & 24 deletions docs/dev/setup-virtualenv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Prerequisites
This guide assumes you have already installed and set up the following:

1. `Git <https://git-scm.com>`__
2. `Python 3.11 <https://www.python.org>`__, `pip`_, and `virtualenv`_
3. `Node.js 18 <https://nodejs.org>`__ and `npm 9 <https://www.npmjs.com>`__ or
2. `Python 3.11 <https://www.python.org>`__
3. `uv <https://docs.astral.sh/uv/getting-started/installation/#standalone-installer>`_
4. `Node.js 18 <https://nodejs.org>`__ and `npm 9 <https://www.npmjs.com>`__ or
later
4. `PostgreSQL 15 <http://www.postgresql.org>`__
5. `PostgreSQL 15 <http://www.postgresql.org>`__

These docs assume a Unix-like operating system, although the site should, in
theory, run on Windows as well. All the example commands given below are
Expand All @@ -33,21 +34,19 @@ following commands:
sudo apt install -y dirmngr ca-certificates software-properties-common apt-transport-https lsb-release curl
curl -fSsL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg > /dev/null
echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main | sudo tee /etc/apt/sources.list.d/postgresql.list
# This is needed to install Python 3.11 (default is 3.12)
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y git python3-pip python3.11-dev python-is-python3 virtualenv postgresql-client-15 postgresql-15 libxml2-dev libxslt1-dev libmemcached-dev libpq-dev nodejs npm
sudo apt install -y git python3-dev python-is-python3 virtualenv postgresql-client-15 postgresql-15 libxml2-dev libxslt1-dev libmemcached-dev libpq-dev nodejs npm
# Set Python 3.11 as default
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.cargo/env
# Start PostgreSQL server
/etc/init.d/postgresql start
.. _pip: https://pip.pypa.io/en/stable/
.. _virtualenv: https://virtualenv.pypa.io/en/latest

Installation
------------
1. Clone this repository_ or your fork_:
Expand All @@ -57,31 +56,25 @@ Installation
git clone https://github.com/mozilla/pontoon.git
cd pontoon
2. Create a virtualenv for Pontoon and activate it:
2. Create a virtualenv for Pontoon with Python 3.11 and activate it:

.. code-block:: bash
virtualenv venv
source venv/bin/activate
uv python install 3.11
uv venv --python 3.11
# Activate virtualenv
source .venv/bin/activate
.. note::

Whenever you want to work on Pontoon in a new terminal you'll have to
re-activate the virtualenv. Read the virtualenv_ documentation to learn
more about how virtualenv works.

3. Install the dependencies using the latest version of pip_:

.. code-block:: bash
pip install --require-hashes -r requirements/default.txt -r requirements/dev.txt -r requirements/test.txt
re-activate the virtualenv.

On Ubuntu 24.04, you will need to install ``setuptools`` within the
virtualenv if the command above fails.
3. Install the dependencies:

.. code-block:: bash
pip install -U setuptools
uv pip install -r requirements/default.txt -r requirements/dev.txt -r requirements/test.txt
4. Create your database, using the following set of commands.

Expand Down
1 change: 0 additions & 1 deletion requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@
django-debug-toolbar==4.3.0
django-extensions==3.2.3
django-sslserver==0.22
pip-tools==7.4.1
34 changes: 0 additions & 34 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ asgiref==3.8.1 \
# via
# -c requirements/default.txt
# django
build==1.2.1 \
--hash=sha256:526263f4870c26f26c433545579475377b2b7588b6f1eac76a001e873ae3e19d \
--hash=sha256:75e10f767a433d9a86e50d83f418e83efc18ede923ee5ff7df93b6cb0306c5d4
# via pip-tools
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
# via
# -c requirements/default.txt
# pip-tools
django==4.2.11 \
--hash=sha256:6e6ff3db2d8dd0c986b4eec8554c8e4f919b5c1ff62a5b4390c17aff2ed6e5c4 \
--hash=sha256:ddc24a0a8280a0430baa37aff11f28574720af05888c62b7cfe71d219f4599d3
Expand All @@ -39,34 +29,10 @@ django-extensions==3.2.3 \
django-sslserver==0.22 \
--hash=sha256:c598a363d2ccdc2421c08ddb3d8b0973f80e8e47a3a5b74e4a2896f21c2947c5
# via -r requirements/dev.in
packaging==24.0 \
--hash=sha256:2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5 \
--hash=sha256:eb82c5e3e56209074766e6885bb04b8c38a0c015d0a30036ebe7ece34c9989e9
# via build
pip-tools==7.4.1 \
--hash=sha256:4c690e5fbae2f21e87843e89c26191f0d9454f362d8acdbd695716493ec8b3a9 \
--hash=sha256:864826f5073864450e24dbeeb85ce3920cdfb09848a3d69ebf537b521f14bcc9
# via -r requirements/dev.in
pyproject-hooks==1.0.0 \
--hash=sha256:283c11acd6b928d2f6a7c73fa0d01cb2bdc5f07c57a2eeb6e83d5e56b97976f8 \
--hash=sha256:f271b298b97f5955d53fb12b72c1fb1948c22c1a6b70b315c54cedaca0264ef5
# via
# build
# pip-tools
sqlparse==0.4.4 \
--hash=sha256:5430a4fe2ac7d0f93e66f1efc6e1338a41884b7ddf2a350cedd20ccc4d9d28f3 \
--hash=sha256:d446183e84b8349fa3061f0fe7f06ca94ba65b426946ffebe6e3e8295332420c
# via
# -c requirements/default.txt
# django
# django-debug-toolbar
wheel==0.43.0 \
--hash=sha256:465ef92c69fa5c5da2d1cf8ac40559a8c940886afcef87dcf14b9470862f1d85 \
--hash=sha256:55c570405f142630c6b9f72fe09d9b67cf1477fcf543ae5b8dcb1f5b7377da81
# via pip-tools

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes and the requirement is not
# satisfied by a package already installed. Consider using the --allow-unsafe flag.
# pip
# setuptools

0 comments on commit e3e5935

Please sign in to comment.