Skip to content

Commit

Permalink
Replace pdm to uv for faster dependency management (#347)
Browse files Browse the repository at this point in the history
* feat: migrate from pdm to uv

* build: replace pdm with uv, add pre-commit hook to lock deps

* build: replace all usage of pdm with uv

* build:  replace pdm with uv for faster dependency managementreplace all usage of pdm with uv

* feat: update entry point to run with uv

* update: users deps login_required

---------

Co-authored-by: Niraj Adhikari <[email protected]>
  • Loading branch information
Pradip-p and nrjadkry authored Nov 29, 2024
1 parent f7f641e commit ffd14c6
Show file tree
Hide file tree
Showing 8 changed files with 2,162 additions and 2,269 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SITE_NAME=${SITE_NAME:-"DTM-Drone Tasking Manager"}
BASE_URL=${BASE_URL:-http://localhost:8000/api}
API_URL_V1=${API_URL_V1:-http://localhost:8000/api}
NODE_ODM_URL=${NODE_ODM_URL:-http://odm-api:3000}
COG_URL=${COG_URL}
COG_URL=${COG_URL:-"http://localhost:9090/api/v1/buckets/dtm-data/objects/download?prefix="}


### ODM ###
Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ repos:
stages: [pre-commit, pre-push, manual]
args: ["--maxkb=10240"]

# Deps: ensure Python uv lockfile is up to date
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.5.2
hooks:
- id: uv-lock
files: src/backend/pyproject.toml
args: [--project, src/backend]

# Versioning: Commit messages & changelog
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.31.0
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ services:
- .env
networks:
- dtm-network
entrypoint: ["pdm", "run", "alembic", "upgrade", "head"]
entrypoint: ["uv", "run", "alembic", "upgrade", "head"]
restart: "no"
52 changes: 38 additions & 14 deletions src/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,43 @@ RUN set -ex \
"libgdal-dev" \
&& rm -rf /var/lib/apt/lists/*

# Install PDM
RUN pip install -U pdm
# Disable update check
ENV PDM_CHECK_UPDATE=false

WORKDIR /project

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

# Ensure executables are at the front of the path
ENV PATH="/bin:$PATH"

# - Silence uv complaining about not being able to use hard links,
# - tell uv to byte-compile packages for faster application startups,
# - prevent uv from accidentally downloading isolated Python builds,
# - use a temp dir instead of cache during install,
# - select system python version,
# - declare `/opt/python` as the target for `uv sync` (i.e. instead of .venv).
ENV LANG=en_US.UTF-8 \

Check warning on line 39 in src/backend/Dockerfile

View workflow job for this annotation

GitHub Actions / backend-build / build-image

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$PYTHON_BASE' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8 \
UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_NO_CACHE=1 \
UV_PYTHON="python$PYTHON_BASE" \
UV_PROJECT_ENVIRONMENT=/opt/python
STOPSIGNAL SIGINT

# Copy files needed for building the project
COPY ./pyproject.toml /project/
COPY ./pdm.lock /project/
COPY ./pyproject.toml ./alembic.ini ./uv.lock /project/

# Install dependencies using uv (caching to improve speed for repeated builds)
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=uv.lock,target=uv.lock \
uv sync --frozen --no-install-project

# Install dependencies and project into the local packages directory
RUN pdm install --check --prod --no-editable
# Sync the project again using uv, ensuring all project dependencies are installed
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync

# Run stage (final stage)
FROM python:$PYTHON_BASE AS service
Expand All @@ -61,17 +85,17 @@ RUN set -ex \
"libgdal-dev" \
&& rm -rf /var/lib/apt/lists/*

# Retrieve packages from build stage
COPY --from=build /project/.venv/ /project/.venv
# Retrieve global Python packages and dependencies from the build stage
COPY --from=build /opt/python /opt/python

# Set working directory
WORKDIR /project/src/backend

# Set environment PATH
ENV PATH="/project/.venv/bin:$PATH"
ENV PATH="/opt/python/bin:$PATH"

# Install PDM in runtime stage
RUN pip install -U pdm
# Install uv in runtime stage
COPY --from=ghcr.io/astral-sh/uv:0.5.2 /uv /bin/uv

# Copy the source files to the final image
COPY . /project/src/backend
Expand Down
16 changes: 8 additions & 8 deletions src/backend/app/users/user_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ async def login_required(
request: Request, access_token: str = Security(APIKeyHeader(name="access-token"))
) -> AuthUser:
"""Dependency to inject into endpoints requiring login."""
if settings.DEBUG:
return AuthUser(
id="6da91a51-5efd-40c9-a9c4-b66465a75fbe",
email="[email protected]",
name="admin",
profile_img="",
role="",
)
# if settings.DEBUG:
# return AuthUser(
# id="6da91a51-5efd-40c9-a9c4-b66465a75fbe",
# email="[email protected]",
# name="admin",
# profile_img="",
# role="",
# )

if not access_token:
raise HTTPException(status_code=401, detail="No access token provided")
Expand Down
2,237 changes: 0 additions & 2,237 deletions src/backend/pdm.lock

This file was deleted.

10 changes: 2 additions & 8 deletions src/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [
requires-python = ">=3.11"
license = {text = "GPL-3.0-only"}

[project.optional-dependencies]
[dependency-groups]
dev = [
"pre-commit>=4.0.1",
"commitizen>=3.30.0",
Expand Down Expand Up @@ -74,10 +74,4 @@ monitoring = [
"opentelemetry-instrumentation-requests>=0.48b0",
]

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"


[tool.pdm]
distribution = true
[tool.uv]
Loading

0 comments on commit ffd14c6

Please sign in to comment.