Skip to content

Commit

Permalink
feat: replaced poetry with uv (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeKrop committed Aug 24, 2024
1 parent 3c99216 commit 842a684
Show file tree
Hide file tree
Showing 9 changed files with 1,233 additions and 1,415 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ jobs:
strategy:
matrix:
python-version: ["3.12"]
poetry-version: [1.8.2]
uv-version: ["0.3.3"]

steps:
- name: Build project
uses: TeKrop/install-python-poetry-project@v3
with:
python-version: ${{ matrix.python-version }}
poetry-version: ${{ matrix.poetry-version }}
- uses: actions/checkout@v4

- name: Set up uv
run: curl -LsSf https://astral.sh/uv/${{ matrix.uv-version }}/install.sh | sh

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

- name: Install the project
run: uv sync --frozen --no-cache

- name: Run Ruff code analysis
run: ruff check .
run: uv run ruff check .

- name: Run tests suite
run: |
PYTHONPATH=app/ python -m pytest -v --cov-fail-under=80 --cov-report=html --cov=app/ tests/
PYTHONPATH=app/ uv run python -m pytest -v --cov-fail-under=80 --cov-report=html --cov=app/ tests/
PERCENT=$(cat htmlcov/index.html | grep "pc_cov" | awk -F '>' '{print $2}' | awk -F '%' '{print $1}')
echo "COVERAGE=$PERCENT" >> $GITHUB_ENV
Expand Down
37 changes: 13 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
# Build arguments
ARG PYTHON_VERSION=3.12
ARG POETRY_VERSION=1.8.2
ARG UV_VERSION=0.3.3

FROM python:${PYTHON_VERSION}-alpine AS main
# Create a temporary stage to pull the uv binary
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv-stage

WORKDIR /code
# Main stage
FROM python:${PYTHON_VERSION}-alpine AS main

# Environment variables
ARG POETRY_VERSION
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=${POETRY_VERSION}

# Install required system packages and install poetry
RUN apk add build-base && \
apk add libffi-dev && \
pip install poetry==$POETRY_VERSION
# Copy the uv binary from the temporary stage to the main stage
COPY --from=uv-stage /uv /bin/uv

# Copy only requirements (caching in Docker layer)
COPY pyproject.toml /code/
COPY poetry.lock /code/
COPY pyproject.toml uv.lock /code/

# Install dependencies
RUN poetry config virtualenvs.create false && \
poetry install --only main --no-interaction --no-ansi
# Sync the project into a new environment (no dev dependencies)
WORKDIR /code
RUN uv sync --frozen --no-cache --no-dev

# Copy code and static folders
COPY ./app /code/app
Expand All @@ -39,5 +28,5 @@ RUN chmod +x /etc/crontabs/root

# For dev image, copy the tests and install necessary dependencies
FROM main as dev
RUN poetry install --only dev --no-interaction --no-ansi
COPY ./tests /code/tests
RUN uv sync --frozen --no-cache
COPY ./tests /code/tests
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ build: ## Build project images

start: ## Run OverFastAPI application (dev mode)
@echo "Launching OverFastAPI (dev mode)..."
$(DOCKER_RUN) fastapi dev app/main.py --host 0.0.0.0
$(DOCKER_RUN) uv run fastapi dev app/main.py --host 0.0.0.0

lint: ## Run linter
@echo "Running linter..."
$(DOCKER_RUN) ruff check --fix --exit-non-zero-on-fix
$(DOCKER_RUN) uv run ruff check --fix --exit-non-zero-on-fix

format: ## Run formatter
@echo "Running formatter..."
$(DOCKER_RUN) ruff format
$(DOCKER_RUN) uv run ruff format

shell: ## Access an interactive shell inside the app container
@echo "Running shell on app container..."
Expand All @@ -50,10 +50,10 @@ exec: ## Execute a given COMMAND inside the app container
test: ## Run tests, PYTEST_ARGS can be specified
ifdef PYTEST_ARGS
@echo "Running tests on $(PYTEST_ARGS)..."
$(DOCKER_RUN) python -m pytest $(PYTEST_ARGS)
$(DOCKER_RUN) uv run python -m pytest $(PYTEST_ARGS)
else
@echo "Running all tests with coverage..."
$(DOCKER_RUN) python -m pytest --cov app --cov-report html -n auto
$(DOCKER_RUN) uv run python -m pytest --cov app --cov-report html -n auto
endif

up: ## Build & run OverFastAPI application (production mode)
Expand All @@ -73,7 +73,7 @@ clean: down ## Clean up Docker environment
docker image prune -af
docker network prune -f

lock: ## Update poetry lock file
@poetry lock --no-update
lock: ## Update lock file
$(DOCKER_RUN) uv lock

.PHONY: help build start lint format shell exec test up down clean lock
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def get_app_version() -> str:
with Path(f"{Path.cwd()}/pyproject.toml").open(mode="rb") as project_file:
project_data = tomllib.load(project_file)
return project_data["tool"]["poetry"]["version"]
return project_data["project"]["version"]


class Settings(BaseSettings):
Expand Down
8 changes: 4 additions & 4 deletions build/overfast-crontab
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* * * * * cd /code && python -m app.commands.check_and_update_cache
0 2 * * * cd /code && python -m app.commands.check_new_hero
42 * * * * cd /code && python -m app.commands.update_search_data_cache
24 * * * * cd /code && python -m app.commands.check_and_delete_parser_cache
* * * * * cd /code && .venv/bin/python -m app.commands.check_and_update_cache
0 2 * * * cd /code && .venv/bin/python -m app.commands.check_new_hero
42 * * * * cd /code && .venv/bin/python -m app.commands.update_search_data_cache
24 * * * * cd /code && .venv/bin/python -m app.commands.check_and_delete_parser_cache
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
build:
context: .
target: ${BUILD_TARGET:-main}
command: fastapi run app/main.py --port 8080 --proxy-headers
command: /code/.venv/bin/fastapi run app/main.py --port 8080 --proxy-headers
env_file: ${APP_VOLUME_PATH:-.}/.env
volumes:
- ${APP_VOLUME_PATH:-/tmp}/app-logs:/code/logs
Expand Down
Loading

0 comments on commit 842a684

Please sign in to comment.