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

Use uv as python package manager #715

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Linter configs
pyproject.toml
.flake8

# Python requirements files
Expand Down
1 change: 1 addition & 0 deletions server/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
124 changes: 57 additions & 67 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,83 +1,73 @@
ARG ALPINE_VERSION=3.13


FROM alpine:$ALPINE_VERSION as prereqs
WORKDIR /opt/app

RUN apk --no-cache add \
python3 \
python3-dev \
py3-pip \
build-base \
libheif \
libheif-dev \
libavif \
libavif-dev \
ffmpeg \
# from requirements.txt:
py3-yaml \
py3-psycopg2 \
py3-sqlalchemy \
py3-certifi \
py3-numpy \
py3-pillow \
py3-pynacl \
py3-tz \
py3-pyrfc3339
RUN pip3 install --no-cache-dir --disable-pip-version-check \
"alembic>=0.8.5" \
"coloredlogs==5.0" \
"pyheif==0.6.1" \
"heif-image-plugin>=0.3.2" \
yt-dlp \
"pillow-avif-plugin~=1.1.0"
RUN apk --no-cache del py3-pip

COPY ./ /opt/app/
RUN rm -rf /opt/app/szurubooru/tests


FROM --platform=$BUILDPLATFORM prereqs as testing
WORKDIR /opt/app

RUN apk --no-cache add \
py3-pip \
py3-pytest \
py3-pytest-cov \
postgresql \
&& pip3 install --no-cache-dir --disable-pip-version-check \
pytest-pgsql \
freezegun \
&& apk --no-cache del py3-pip \
&& addgroup app \
&& adduser -SDH -h /opt/app -g '' -G app app \
&& chown app:app /opt/app

COPY --chown=app:app ./szurubooru/tests /opt/app/szurubooru/tests/
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS prereqs

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update && \
apt-get install --no-install-recommends \
-y ffmpeg && \
rm -rf /var/lib/apt/lists/*

# Install the project into `/app`
WORKDIR /app

# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Install the project's dependencies using the lockfile and settings
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-dev

# Then, add the rest of the project source code and install it
# Installing separately from its dependencies allows optimal layer caching
ADD . /app
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev

ENV PATH="/app/.venv/bin:$PATH"
# Reset the entrypoint, don't invoke `uv`
ENTRYPOINT []


FROM prereqs AS testing

RUN apt-get -y update && \
apt-get install --no-install-recommends \
-y postgresql && \
rm -rf /var/lib/apt/lists/* \
&& groupadd app \
&& useradd -g app app \
&& chown app:app /app

WORKDIR /app

# Install dev dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen
# COPY ./szurubooru/tests /opt/app/szurubooru/tests/

ENV TEST_ENVIRONMENT="true"
USER app
ENTRYPOINT ["pytest", "--tb=short"]
CMD ["szurubooru/"]


FROM prereqs as release
WORKDIR /opt/app
FROM prereqs AS release
WORKDIR /app

ARG PUID=1000
ARG PGID=1000

RUN apk --no-cache add \
dumb-init \
py3-setuptools \
py3-waitress \
&& mkdir -p /opt/app /data \
&& addgroup -g ${PGID} app \
&& adduser -SDH -h /opt/app -g '' -G app -u ${PUID} app \
&& chown -R app:app /opt/app /data
RUN mkdir -p /data \
&& groupadd -g ${PGID} app \
&& useradd -r -u ${PUID} -g app app \
&& chown -R app:app /app /data

USER app
CMD ["/opt/app/docker-start.sh"]
CMD ["/app/docker-start.sh"]

ARG PORT=6666
ENV PORT=${PORT}
Expand Down
5 changes: 0 additions & 5 deletions server/dev-requirements.txt

This file was deleted.

6 changes: 3 additions & 3 deletions server/docker-start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/dumb-init /bin/sh
#!/bin/bash
set -e
cd /opt/app
# cd /opt/app

alembic upgrade head

echo "Starting szurubooru API on port ${PORT} - Running on ${THREADS} threads"
exec waitress-serve-3 --port ${PORT} --threads ${THREADS} szurubooru.facade:app
exec gunicorn --bind 0.0.0.0:${PORT} --threads ${THREADS} szurubooru.facade:app
47 changes: 46 additions & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
[project]
name = "szurubooru"
version = "2.5"
requires-python = ">=3.12"
dependencies = [
"alembic>=0.8.5",
"certifi>=2017.11.5",
"coloredlogs>=5.0",
"gunicorn>=23.0.0",
"heif-image-plugin>=0.3.2",
"numpy>=1.8.2",
"pillow>=4.3.0",
"pillow-avif-plugin>=1.1.0",
"psycopg2-binary>=2.6.1",
"pyheif>=0.6.1",
"pynacl>=1.2.1",
"pyrfc3339>=1.0",
"pytz>=2018.3",
"pyyaml>=3.11",
"sqlalchemy>=1.0.12,<1.4",
"yt-dlp>=2025.2.19",
]

[dependency-groups]
dev = [
"freezegun>=0.3.6",
"pycodestyle>=2.0.0",
"pytest>=2.9.1",
"pytest-cov>=2.2.1",
"pytest-pgsql>=1.1.1",
]

[tool.black]
line-length = 79

[tool.isort]
known_first_party = ["szurubooru"]
known_third_party = ["PIL", "alembic", "coloredlogs", "freezegun", "nacl", "numpy", "pyrfc3339", "pytest", "pytz", "sqlalchemy", "yaml", "youtube_dl"]
known_third_party = [
"PIL",
"alembic",
"coloredlogs",
"freezegun",
"nacl",
"numpy",
"pyrfc3339",
"pytest",
"pytz",
"sqlalchemy",
"yaml",
"youtube_dl",
]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
Expand Down
15 changes: 0 additions & 15 deletions server/requirements.txt

This file was deleted.

Loading
Loading