Skip to content

Commit

Permalink
chore: migrate OC Python projects to uv (SMR-8) (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaffter authored Feb 5, 2025
1 parent 8e7710a commit ea9758a
Show file tree
Hide file tree
Showing 21 changed files with 666 additions and 1,244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
.pdm-build

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
1 change: 0 additions & 1 deletion apps/openchallenges/data-lambda/.python-version

This file was deleted.

38 changes: 27 additions & 11 deletions apps/openchallenges/data-lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
FROM python:3.13.0-slim-bullseye AS builder
FROM ghcr.io/astral-sh/uv:0.5.14 AS uv

# Install the same version of Poetry as inside the dev container
RUN pip install --no-cache-dir poetry==1.8.3
# First, bundle the dependencies into the task root.
FROM public.ecr.aws/lambda/python:3.13 AS builder

# Enable bytecode compilation, to improve cold-start performance.
ENV UV_COMPILE_BYTECODE=1

# Disable installer metadata, to create a deterministic layer.
ENV UV_NO_INSTALLER_METADATA=1

# Enable copy mode to support bind mount caching.
ENV UV_LINK_MODE=copy

# Copy uv binary
COPY --from=uv /uv /bin/uv

# Copy dependency files
WORKDIR /app
COPY pyproject.toml poetry.lock ./
RUN poetry export --without-hashes --format=requirements.txt > requirements.txt
COPY pyproject.toml uv.lock ./

# Generate requirements.txt and install dependencies
RUN uv export --frozen --no-emit-workspace --no-dev --no-editable -o requirements.txt && \
uv pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

FROM public.ecr.aws/lambda/python:3.11
FROM public.ecr.aws/lambda/python:3.13

COPY --from=builder /app/requirements.txt ${LAMBDA_TASK_ROOT}/
COPY openchallenges_data_lambda/app.py ${LAMBDA_TASK_ROOT}/
# Copy the runtime dependencies from the builder stage.
COPY --from=builder ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}

RUN python3.11 -m pip install --no-cache-dir -r requirements.txt -t .
# Copy the application code.
COPY ./openchallenges_data_lambda ${LAMBDA_TASK_ROOT}/app

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]
CMD ["app.app.lambda_handler"]
13 changes: 0 additions & 13 deletions apps/openchallenges/data-lambda/install.sh

This file was deleted.

501 changes: 0 additions & 501 deletions apps/openchallenges/data-lambda/poetry.lock

This file was deleted.

2 changes: 1 addition & 1 deletion apps/openchallenges/data-lambda/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "./install.sh",
"command": "uv sync",
"cwd": "{projectRoot}"
}
},
Expand Down
32 changes: 21 additions & 11 deletions apps/openchallenges/data-lambda/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
[tool.poetry]
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[project]
authors = [
{name = "Verena Chung", email = "[email protected]"},
]
requires-python = "==3.13.1"
dependencies = [
"requests==2.32.3",
"gspread==6.1.4",
"pandas==2.2.3",
"numpy==2.1.0",
]
name = "openchallenges-data-lambda"
version = "0.1.0"
description = "A containerized AWS Lambda to import challenge data into the OC database"
authors = ["Verena Chung <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "3.11.10"
requests = "2.32.3"
gspread = "6.1.4"
pandas = "2.2.3"
numpy = "2.1.0"
[dependency-groups]
dev = []
prod = []
test = []

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.uv]
default-groups = []
286 changes: 286 additions & 0 deletions apps/openchallenges/data-lambda/uv.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion apps/openchallenges/edam-etl/.python-version

This file was deleted.

46 changes: 34 additions & 12 deletions apps/openchallenges/edam-etl/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
FROM python:3.12.0-slim
FROM ghcr.io/astral-sh/uv:0.5.14 AS uv

# First, bundle the dependencies into the task root.
FROM python:3.12.8-slim AS builder

# Enable bytecode compilation, to improve cold-start performance.
ENV UV_COMPILE_BYTECODE=1

# Disable installer metadata, to create a deterministic layer.
ENV UV_NO_INSTALLER_METADATA=1

# Enable copy mode to support bind mount caching.
ENV UV_LINK_MODE=copy

# Copy uv binary
COPY --from=uv /uv /bin/uv

# Copy dependency files
WORKDIR /app
COPY pyproject.toml uv.lock ./

# Generate requirements.txt and install dependencies
RUN uv export --frozen --no-emit-workspace --no-dev --no-editable -o requirements.txt && \
uv pip install -r requirements.txt --target "/app"

FROM python:3.12.8-slim

ARG USERNAME=app
ARG USER_UID=1000
ARG USER_GID=$USER_UID
ENV APP_DIR=/opt/app \
ENV APP_DIR=/app \
APP_USERNAME=${USERNAME}
ENV PYTHONPATH="${APP_DIR}"

RUN groupadd --gid "$USER_GID" "$USERNAME" \
&& useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USERNAME" \
# Install sudo
&& apt-get update -qq -y && export DEBIAN_FRONTEND=noninteractive \
&& apt-get install --no-install-recommends -qq -y \
gosu \
&& apt-get -y autoclean \
&& apt-get -y clean \
&& apt-get -y autoremove \
&& rm -rf /var/lib/apt/lists/*

WORKDIR ${APP_DIR}
COPY src src/
COPY pyproject.toml poetry.lock ./
# Copy the runtime dependencies from the builder stage.
COPY --from=builder ${APP_DIR} ${APP_DIR}

# Use the version of Poetry installed in the dev container.
# See /workspaces/sage-monorepo/tools/devcontainers/sage/.devcontainer/Dockerfile
RUN pip install --no-cache-dir poetry==1.6.1 \
&& poetry config --local virtualenvs.create false \
&& poetry install --with prod --no-root --no-interaction --no-ansi \
&& pip cache purge
# Copy the application code.
COPY ./src /${APP_DIR}/src
RUN chown -R ${APP_USERNAME}:${APP_USERNAME} ${APP_DIR}

WORKDIR /
COPY docker-entrypoint.sh ./
Expand Down
Loading

0 comments on commit ea9758a

Please sign in to comment.