Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

[WIP] Use Poetry to manage Synapse's dependencies #12337

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8dc02f9
DEBUG: always run twisted trunk; don't raise bugs
Mar 30, 2022
211becb
DEBUG: build all debs
Mar 30, 2022
7d57414
DEPS: Specify deps in pyproject.toml
Feb 18, 2022
53e804f
DEPS: Add missing dependency on importlib_metadata
Mar 29, 2022
c50da48
DEPS: Dirty duplication to make `all` work
Mar 4, 2022
87ecb33
BUILDS: `poetry` in Synapse docker image
Mar 8, 2022
d820ff2
BUILDS: `poetry` in deb virtualenvs
H-Shay Mar 30, 2022
e53e99e
CI: Use common linting workflow
Mar 3, 2022
3de49f4
CI: Fix the "main" trial test runs
Mar 4, 2022
03e5c93
CI: Fix the portdb job to run under poetry
Mar 9, 2022
6ab6f61
CI: Fix the export-data job to run under poetry
Mar 9, 2022
0d8a54a
CI: Fix the olddeps job to run under poetry
Mar 10, 2022
d3386af
CI: Fix Twisted trunk to run under poetry
squahtx Mar 14, 2022
b03ffaa
Changelog
Feb 21, 2022
5f8db3e
DOCS: recommend poetry where appropriate
Mar 30, 2022
680dbc8
Undebug various changes
Mar 30, 2022
a3fb2b8
Fix typos spotted by Sean
Mar 31, 2022
4c780ab
Batch of suggestsion from Brendan, part 1
Mar 31, 2022
8469bd8
Batch of suggestsion from Brendan, part 2
Mar 31, 2022
5dc21d9
Recommend synapse_homeserver entrypoint in README
Mar 31, 2022
e60c8f3
More clearly explain which extras are omitted from `all`.
Mar 31, 2022
6fd43c4
Explain where requirements.txt comes from.
Mar 31, 2022
45a23c0
olddeps: echo patch to confirm sed is sensible
Mar 31, 2022
e96fd31
Olddeps: leave a TODO for caret bounds
Mar 31, 2022
d2df831
Merge remote-tracking branch 'origin/develop' into dmr/pyproject-poetry
Mar 31, 2022
0af56b8
Olddeps: cat patched pyproject, instead of diffing
Apr 1, 2022
2ff7fbf
Docker: `pip install --user poetry` path fix
Apr 1, 2022
18f91a7
Only upgrade pip in dhvirtualenv
Apr 4, 2022
f0d72ac
Install and run poetry export in dpkg-buildpackage
callahad Apr 6, 2022
60a6671
Merge branch 'develop' into dmr/pyproject-poetry
callahad Apr 6, 2022
a653c20
Bump locked version of prometheus-client to 0.14.0
callahad Apr 7, 2022
232a270
Clarify a CI workflow step for Twisted trunk
callahad Apr 7, 2022
9225764
Inline CI steps for installing Twisted trunk
callahad Apr 7, 2022
99892e7
Merge branch 'develop' into dmr/pyproject-poetry
callahad Apr 7, 2022
872dfb8
Minor tweaks to comments in response to reviews
callahad Apr 7, 2022
379944c
Merge branch 'develop' into dmr/pyproject-poetry
callahad Apr 8, 2022
fb611f1
Merge branch 'develop' into dmr/pyproject-poetry
Apr 11, 2022
0cf4705
Merge remote-tracking branch 'origin/develop' into dmr/pyproject-poetry
Apr 12, 2022
988ff61
Include some dockerfile changes from develop
Apr 14, 2022
badd5cf
We don't need README to export
Apr 14, 2022
443b94f
Merge remote-tracking branch 'origin/develop' into dmr/pyproject-poetry
Apr 14, 2022
cbdbcb0
Merge remote-tracking branch 'origin/develop' into dmr/pyproject-poetry
Apr 14, 2022
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
20 changes: 17 additions & 3 deletions .ci/patch_for_twisted_trunk.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#!/bin/sh

# replaces the dependency on Twisted in `python_dependencies` with trunk.
# replaces the dependency on Twisted with Twisted's trunk.
callahad marked this conversation as resolved.
Show resolved Hide resolved

set -e
set -xe
cd "$(dirname "$0")"/..

sed -i -e 's#"Twisted.*"#"Twisted @ git+https://github.com/twisted/twisted"#' synapse/python_dependencies.py
# We could make full use of the poetry machinery (rather than just `pip install ...`)
# so that we can catch dependency resolver problems that could arise from twisted
# bumping its dependencies...
# sed -ibackup -e 's!^Twisted = .*!Twisted = { git = "https://github.com/twisted/twisted.git", rev = "trunk" }!' pyproject.toml
# poetry lock --no-update
# poetry install --no-interaction --extras "all test"

# ...except we run into https://github.com/python-poetry/poetry/issues/5311, where
# poetry insists on installing an old version of treq, which isn't actually compatible
# with recent twisted releases. So let's just install twisted trunk using pip.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
poetry install --no-interaction --extras "all test"
poetry run pip install git+https://github.com/twisted/twisted.git@trunk

# Confirm the version of twisted in use
poetry run pip show twisted
22 changes: 8 additions & 14 deletions .ci/scripts/test_export_data_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@

# Test for the export-data admin command against sqlite and postgres

# Expects Synapse to have been already installed with `poetry install -e .[postgres]

set -xe
cd "$(dirname "$0")/../.."

echo "--- Install dependencies"

# Install dependencies for this test.
pip install psycopg2

# Install Synapse itself. This won't update any libraries.
pip install -e .

echo "--- Generate the signing key"

# Generate the server's signing key.
python -m synapse.app.homeserver --generate-keys -c .ci/sqlite-config.yaml
poetry run synapse_homeserver --generate-keys -c .ci/sqlite-config.yaml
callahad marked this conversation as resolved.
Show resolved Hide resolved

echo "--- Prepare test database"

# Make sure the SQLite3 database is using the latest schema and has no pending background update.
update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates

# Run the export-data command on the sqlite test database
python -m synapse.app.admin_cmd -c .ci/sqlite-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \
poetry run python -m synapse.app.admin_cmd -c .ci/sqlite-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \
--output-directory /tmp/export_data

# Test that the output directory exists and contains the rooms directory
Expand All @@ -37,14 +31,14 @@ else
fi

# Create the PostgreSQL database.
.ci/scripts/postgres_exec.py "CREATE DATABASE synapse"
poetry run .ci/scripts/postgres_exec.py "CREATE DATABASE synapse"

# Port the SQLite databse to postgres so we can check command works against postgres
echo "+++ Port SQLite3 databse to postgres"
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml

# Run the export-data command on postgres database
python -m synapse.app.admin_cmd -c .ci/postgres-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \
poetry run python -m synapse.app.admin_cmd -c .ci/postgres-config.yaml export-data @anon-20191002_181700-832:localhost:8800 \
--output-directory /tmp/export_data2

# Test that the output directory exists and contains the rooms directory
Expand Down
58 changes: 53 additions & 5 deletions .ci/scripts/test_old_deps.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
# this script is run by GitHub Actions in a plain `focal` container; it installs the
# minimal requirements for tox and hands over to the py3-old tox environment.
# this script is run by GitHub Actions in a plain `focal` container; it
# - installs the minimal system requirements, and poetry;
# - patches the project definition file to refer to old versions only;
# - creates a venv with these old versions using poetry; and finally
# - invokes `trial` to run the tests with old deps.

# Prevent tzdata from asking for user input
export DEBIAN_FRONTEND=noninteractive
Expand All @@ -9,12 +12,57 @@ set -ex

apt-get update
apt-get install -y \
python3 python3-dev python3-pip python3-venv \
libxml2-dev libxslt-dev xmlsec1 zlib1g-dev tox libjpeg-dev libwebp-dev
python3 python3-dev python3-pip python3-venv pipx \
libxml2-dev libxslt-dev xmlsec1 zlib1g-dev libjpeg-dev libwebp-dev

export LANG="C.UTF-8"

# Prevent virtualenv from auto-updating pip to an incompatible version
export VIRTUALENV_NO_DOWNLOAD=1

exec tox -e py3-old
# I'd prefer to use something like this
# https://github.com/python-poetry/poetry/issues/3527
# https://github.com/pypa/pip/issues/8085
# rather than this sed script. But that's an Opinion.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

# patch the project definitions in-place
# replace all lower bounds with exact bounds
# but make the pyopenssl 17.0, which can work against an
# OpenSSL 1.1 compiled cryptography (as older ones don't compile on Travis).
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
# delete all lines referring to psycopg2 --- so no testing of postgres support
# Omit systemd: we're not logging to journal here.

sed -i-backup \
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
-e "s/[~>]=/==/g" \
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
-e "/psycopg2/d" \
-e 's/pyOpenSSL = "==16.0.0"/pyOpenSSL = "==17.0.0"/' \
-e '/psycopg2/d' \
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
-e '/systemd/d' \
pyproject.toml
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

# Use poetry to do the installation. This ensures that the versions are all mutually
# compatible (as far the package metadata declares, anyway); pip's package resolver
# is more lax.
#
# Rather than `poetry install --no-dev`, we drop all dev dependencies from the
# toml file. This means we don't have to ensure compatibility between old deps and
# dev tools.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

pip install --user toml

REMOVE_DEV_DEPENDENCIES="
import toml
with open('pyproject.toml', 'r') as f:
data = toml.loads(f.read())

del data['tool']['poetry']['dev-dependencies']

with open('pyproject.toml', 'w') as f:
toml.dump(data, f)
"
python3 -c "$REMOVE_DEV_DEPENDENCIES"

pipx install poetry==1.1.12
~/.local/bin/poetry lock
~/.local/bin/poetry install -E "all test"
~/.local/bin/poetry run trial -j2 tests
25 changes: 9 additions & 16 deletions .ci/scripts/test_synapse_port_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,32 @@
# - runs the port script on a prepopulated test sqlite db
# - also runs it against an new sqlite db

# Expects Synapse to have been already installed with `poetry install -e .[postgres]

set -xe
cd "$(dirname "$0")/../.."

echo "--- Install dependencies"

# Install dependencies for this test.
pip install psycopg2 coverage coverage-enable-subprocess

# Install Synapse itself. This won't update any libraries.
pip install -e .

echo "--- Generate the signing key"

# Generate the server's signing key.
python -m synapse.app.homeserver --generate-keys -c .ci/sqlite-config.yaml
poetry run synapse_homeserver --generate-keys -c .ci/sqlite-config.yaml

echo "--- Prepare test database"

# Make sure the SQLite3 database is using the latest schema and has no pending background update.
update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates

# Create the PostgreSQL database.
.ci/scripts/postgres_exec.py "CREATE DATABASE synapse"
poetry run .ci/scripts/postgres_exec.py "CREATE DATABASE synapse"

echo "+++ Run synapse_port_db against test database"
# TODO: this invocation of synapse_port_db (and others below) used to be prepended with `coverage run`,
# but coverage seems unable to find the entrypoints installed by `pip install -e .`.
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml

# We should be able to run twice against the same database.
echo "+++ Run synapse_port_db a second time"
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml

#####

Expand All @@ -48,12 +41,12 @@ echo "--- Prepare empty SQLite database"
# we do this by deleting the sqlite db, and then doing the same again.
rm .ci/test_db.db

update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates

# re-create the PostgreSQL database.
.ci/scripts/postgres_exec.py \
poetry run .ci/scripts/postgres_exec.py \
"DROP DATABASE synapse" \
"CREATE DATABASE synapse"

echo "+++ Run synapse_port_db against empty database"
synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# things to include
!docker
!synapse
!MANIFEST.in
!README.rst
!setup.py
!pyproject.toml
!poetry.lock

**/__pycache__
6 changes: 3 additions & 3 deletions .github/workflows/release-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
- id: set-distros
run: |
# if we're running from a tag, get the full list of distros; otherwise just use debian:sid
dists='["debian:sid"]'
if [[ $GITHUB_REF == refs/tags/* ]]; then
# dists='["debian:sid"]'
# if [[ $GITHUB_REF == refs/tags/* ]]; then
dists=$(scripts-dev/build_debian_packages.py --show-dists-json)
fi
# fi
echo "::set-output name=distros::$dists"
# map the step outputs to job outputs
outputs:
Expand Down
59 changes: 26 additions & 33 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,11 @@ jobs:
- run: scripts-dev/config-lint.sh

lint:
runs-on: ubuntu-latest
strategy:
matrix:
toxenv:
- "check_codestyle"
- "check_isort"
- "mypy"
- "packaging"

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install tox
- run: tox -e ${{ matrix.toxenv }}
# This does a vanilla `poetry install` - no extras. I'm slightly anxious
# that we might skip some typechecks on code that uses extras. However,
# I think the right way to fix this is to turn up mypy's strictness:
# disallow unknown imports and be accept fewer uses of `Any`.
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
uses: "matrix-org/backend-meta/.github/workflows/python-poetry-ci.yml@v1"

lint-crlf:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -72,23 +63,23 @@ jobs:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
database: ["sqlite"]
toxenv: ["py"]
extras: ["all"]
include:
# Newest Python without optional deps
- python-version: "3.10"
toxenv: "py-noextras"
extras: ""

# Oldest Python with PostgreSQL
- python-version: "3.7"
database: "postgres"
postgres-version: "10"
toxenv: "py"
extras: "all"

# Newest Python with newest PostgreSQL
- python-version: "3.10"
database: "postgres"
postgres-version: "14"
toxenv: "py"
extras: "all"

steps:
- uses: actions/checkout@v2
Expand All @@ -100,17 +91,16 @@ jobs:
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_INITDB_ARGS="--lc-collate C --lc-ctype C --encoding UTF8" \
postgres:${{ matrix.postgres-version }}
- uses: actions/setup-python@v2
- uses: matrix-org/setup-python-poetry@v1
with:
python-version: ${{ matrix.python-version }}
- run: pip install tox
extras: ${{ matrix.extras }}
- name: Await PostgreSQL
if: ${{ matrix.postgres-version }}
timeout-minutes: 2
run: until pg_isready -h localhost; do sleep 1; done
- run: tox -e ${{ matrix.toxenv }}
- run: poetry run trial --jobs=2 tests
env:
TRIAL_FLAGS: "--jobs=2"
SYNAPSE_POSTGRES: ${{ matrix.database == 'postgres' || '' }}
SYNAPSE_POSTGRES_HOST: localhost
SYNAPSE_POSTGRES_USER: postgres
Expand All @@ -129,18 +119,18 @@ jobs:
|| true

trial-olddeps:
# Note: sqlite only; no postgres
if: ${{ !cancelled() && !failure() }} # Allow previous steps to be skipped, but not fail
needs: linting-done
# needs: linting-done
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Test with old deps
uses: docker://ubuntu:focal # For old python and sqlite
# Note: focal seems to be using 3.8, but the oldest is 3.7?
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
with:
workdir: /github/workspace
entrypoint: .ci/scripts/test_old_deps.sh
env:
TRIAL_FLAGS: "--jobs=2"
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
- name: Dump logs
# Logs are most useful when the command fails, always include them.
if: ${{ always() }}
Expand All @@ -156,23 +146,24 @@ jobs:

trial-pypy:
# Very slow; only run if the branch name includes 'pypy'
# Note: sqlite only; no postgres. Completely untested since poetry move.
if: ${{ contains(github.ref, 'pypy') && !failure() && !cancelled() }}
needs: linting-done
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["pypy-3.7"]
extras: ["all"]

steps:
- uses: actions/checkout@v2
# TODO: why is this list different from above?
callahad marked this conversation as resolved.
Show resolved Hide resolved
- run: sudo apt-get -qq install xmlsec1 libxml2-dev libxslt-dev
- uses: actions/setup-python@v2
- uses: matrix-org/setup-python-poetry@v1
with:
python-version: ${{ matrix.python-version }}
- run: pip install tox
- run: tox -e py
env:
TRIAL_FLAGS: "--jobs=2"
extras: ${{ matrix.extras }}
- run: poetry run trial --jobs=2 tests
- name: Dump logs
# Logs are most useful when the command fails, always include them.
if: ${{ always() }}
Expand Down Expand Up @@ -271,9 +262,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: sudo apt-get -qq install xmlsec1
- uses: actions/setup-python@v2
- uses: matrix-org/setup-python-poetry@v1
with:
python-version: "3.9"
python-version: ${{ matrix.python-version }}
extras: "postgres"
- run: .ci/scripts/test_export_data_command.sh

portdb:
Expand Down Expand Up @@ -308,9 +300,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: sudo apt-get -qq install xmlsec1
- uses: actions/setup-python@v2
- uses: matrix-org/setup-python-poetry@v1
with:
python-version: ${{ matrix.python-version }}
extras: "postgres"
- run: .ci/scripts/test_synapse_port_db.sh

complement:
Expand Down
Loading