Skip to content

Commit

Permalink
switch from poetry to uv
Browse files Browse the repository at this point in the history
  • Loading branch information
hrodmn committed Oct 17, 2024
1 parent 2ab8f1f commit c9d7368
Show file tree
Hide file tree
Showing 8 changed files with 2,527 additions and 3,458 deletions.
61 changes: 30 additions & 31 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,37 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Install Poetry
uses: Gr1N/setup-poetry@v9
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
poetry-version: "1.8.0"

- name: Set up Python
uses: actions/setup-python@v5
env:
POETRY_VIRTUALENVS_IN_PROJECT: "true"
with:
python-version: "3.12"
cache: poetry
cache-dependency-path: src/server/poetry.lock
version: "0.4.22"
enable-cache: true

- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}

- name: Install dependencies
working-directory: src/server
run: |
uv sync --all-extras
- name: Install dependencies including dev dependencies
- name: run pre-commit
if: ${{ matrix.python-version == env.LATEST_PY_VERSION }}
working-directory: src/server
run: poetry install --with dev
run: |
uv run pre-commit run --all-files
- name: Run Tests
- name: Run tests
working-directory: src/server
run: poetry run pytest
run: uv run pytest

check-client-changes:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -107,20 +112,15 @@ jobs:
- name: Check out repository
uses: actions/checkout@v4

- name: Install Poetry
uses: Gr1N/setup-poetry@v9
with:
poetry-version: "1.8.0"

- name: Set up Python
uses: actions/setup-python@v5
env:
POETRY_VIRTUALENVS_IN_PROJECT: "true"
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
python-version: "3.12"
cache: poetry
cache-dependency-path: src/server/poetry.lock

version: "0.4.22"
enable-cache: true

- name: Set up Python 3.12
run: uv python install 3.12

- name: Set tag version
id: tag
run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
Expand All @@ -134,8 +134,7 @@ jobs:
if: env.tag == env.version
working-directory: src/server
run: |
poetry self add poetry-version-plugin
poetry build
uv build
- name: Publish to pypi
uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
63 changes: 14 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ items.

- [Prerequisites](#prerequisites)
- [Setup Using Docker](#setup-using-docker)
- [Setup Using Virtual Environment and Poetry](#setup-using-virtual-environment-and-poetry)
- [Setup Using `uv`](#setup-using-uv)
- [API Endpoints](#api-endpoints)
- [Example API usage](#example-api-usage)

Expand Down Expand Up @@ -55,74 +55,39 @@ same endpoints:
- Backend (FastAPI): `http://localhost:8000`
- Frontend (React): `http://localhost:3000`

## Setup Using Virtual Environment and Poetry
## Setup Using `uv`

### 1. Create and Activate a Virtual Environment
### 1. Install `uv`

It is recommended to use a virtual environment to manage your project dependencies.

Create a virtual environment:
Ensure you have uv installed. If not, install it:

```bash
python3 -m venv venv
```

Activate the virtual environment:

- **For Unix/MacOS:**

```bash
source venv/bin/activate
```

- **For Windows:**

```bash
venv\Scripts\activate
```
# install uv

### 2. Install Poetry
# unix
curl -LsSf https://astral.sh/uv/install.sh | sh

Ensure you have Poetry installed. If not, install it:

```bash
curl -sSL https://install.python-poetry.org | python3 -
# or on windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
```

Add Poetry to your system's PATH:
- **For Unix/MacOS:**
```bash
export PATH="$HOME/.local/bin:$PATH"
```
Add the above line to your shell configuration file (`~/.bashrc`,
`~/.bash_profile`, or `~/.zshrc`) to make it permanent.
- **For Windows:**
Add the path to your `PATH` environment variable through the system environment
variable settings.
### 3. Install Dependencies
### 2. Install Dependencies

Navigate to the `src/server` directory and install the dependencies using Poetry:
Navigate to the `src/server` directory and install the dependencies using `uv`:

```bash
cd src/server
poetry install
uv sync --all-extras
```

### 4. Run the Application
### 3. Run the Application

Navigate to the `src/server` directory, activate your virtual environment, and
run the FastAPI server using Uvicorn:

```bash
cd src/server
source venv/bin/activate # or `venv\Scripts\activate` for Windows
poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
```

Navigate to the `src/client` directory and start the React development server:
Expand Down
12 changes: 8 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ services:
build:
context: ./src/server
dockerfile: ./docker/Dockerfile
target: local
container_name: federated-collection-discovery-server
ports:
- "8000:8000"
volumes:
- ./src/server/federated_collection_discovery:/work/federated_collection_discovery
develop:
watch:
- action: sync+restart
path: ./src/server/federated_collection_discovery/
target: /app/federated_collection_discovery/
- action: rebuild
path: ./pyproject.toml
environment:
- PYTHONUNBUFFERED=1
- PORT=8000
- FEDERATED_STAC_API_URLS=https://stac.maap-project.org/,https://openveda.cloud/api/stac/,https://catalogue.dataspace.copernicus.eu/stac
- FEDERATED_CMR_URLS=https://cmr.earthdata.nasa.gov/search/
client:
Expand Down
1 change: 1 addition & 0 deletions src/server/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
33 changes: 10 additions & 23 deletions src/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,25 @@ git clone git@github.com:developmentseed/federated-collection-discovery.git
cd federated-collection-discovery
```

### Install Poetry
### Install `uv`

Ensure you have Poetry installed. If not, install it:
Ensure you have uv installed. If not, install it:

```bash
curl -sSL https://install.python-poetry.org | python3 -
```

Add Poetry to your system's PATH:

- **For Unix/MacOS:**
# install uv

```bash
export PATH="$HOME/.local/bin:$PATH"
```

Add the above line to your shell configuration file (`~/.bashrc`,
`~/.bash_profile`, or `~/.zshrc`) to make it permanent.
# unix
curl -LsSf https://astral.sh/uv/install.sh | sh

- **For Windows:**

Add the path to your `PATH` environment variable through the system environment
variable settings.
# or on windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
```

### Install
### Install `federated_collection_discovery`

Navigate to the `src/server` directory and install the dependencies (in a virtual
environment) using Poetry:

```bash
python3 -m venv env
source env/bin/activate
cd src/server
poetry install
uv sync --all-extras
```
72 changes: 8 additions & 64 deletions src/server/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,13 @@
# Based on https://github.com/michaeloliverx/python-poetry-docker-example/blob/master/docker/Dockerfile
# Uses multi-stage builds requiring Docker 17.05 or higher
# See https://docs.docker.com/develop/develop-images/multistage-build/
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

ARG PYTHON_VERSION=3.12
ARG POETRY_VERSION=1.8.3
WORKDIR /app

# Creating a python base with shared environment variables
FROM python:${PYTHON_VERSION}-slim AS python-base
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"
COPY README.md pyproject.toml uv.lock ./
COPY federated_collection_discovery/ ./federated_collection_discovery/

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
RUN uv sync --frozen --extra uvicorn


# builder-base is used to build dependencies
FROM python-base AS builder-base
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
build-essential

# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://install.python-poetry.org | python3 -

# We copy our Python requirements here to cache them
# and install only runtime deps using poetry
WORKDIR $PYSETUP_PATH
COPY ./poetry.lock ./pyproject.toml ./
RUN poetry install --no-root --no-dev


# 'local' stage installs all dev deps and can be used to develop code.
# For example using docker-compose to mount local volume under /app
FROM python-base AS local
ENV FASTAPI_ENV=local

# Copying poetry and venv into image
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH

# Copying in our entrypoint
COPY ./docker/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

# venv already has runtime deps installed we get a quicker install
# WORKDIR $PYSETUP_PATH
# RUN poetry install

WORKDIR /work

COPY ./poetry.lock ./pyproject.toml ./
# will get overwritten by mounted vol if you run with docker compose
# note: not re-installing since we already have the runtime deps from builder-base
COPY ./federated_collection_discovery ./federated_collection_discovery/
RUN poetry install --no-dev -E uvicorn

EXPOSE 8000
ENTRYPOINT /docker-entrypoint.sh $0 $@
CMD ["poetry", "run", "uvicorn", "federated_collection_discovery.main:app", "--host=0.0.0.0", "--port=8000", "--reload"]
ENV HOST 0.0.0.0
ENV PORT 80
CMD uv run uvicorn federated_collection_discovery.main:app --host ${HOST} --port ${PORT} --log-level debug

Loading

0 comments on commit c9d7368

Please sign in to comment.