Skip to content

Commit

Permalink
! transition from poetry to uv
Browse files Browse the repository at this point in the history
! requirements bump
  • Loading branch information
ALERTua committed Nov 24, 2024
1 parent 50acce4 commit a991a02
Show file tree
Hide file tree
Showing 16 changed files with 1,961 additions and 3,261 deletions.
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-merge-conflict
- id: check-added-large-files
- id: check-builtin-literals
- id: check-case-conflict
- id: check-toml
- id: check-yaml
- id: check-docstring-first
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.5.1
hooks:
- id: uv-lock
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
97 changes: 36 additions & 61 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,81 +1,56 @@
FROM python:3.12-slim AS python-base
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS production

LABEL maintainer="ALERT <[email protected]>"

ENV \
BASE_DIR=/app \
SOURCE_DIR_NAME=youtube_automanager
ENV PORT=8080
ENV VERBOSE=0
ENV LOGFILES=False
ENV TELEGRAM_CHAT_ID=""
ENV TELEGRAM_BOT_TOKEN=""
ENV TELEGRAM_ANNOUNCE="False"
ENV DISCORD_WEBHOOK_URL=""
ENV SLACK_WEBHOOK_URL=""
ENV SLACK_CHANNEL=""
ENV SLACK_USER_MENTIONS=""
ENV TEAMS_WEBHOOK_URL=""
ENV TEAMS_USER_MENTIONS=""


WORKDIR $BASE_DIR
EXPOSE $PORT
VOLUME ["/app/config"]

ENV \
# uv
UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_FROZEN=1 \
UV_NO_PROGRESS=true \
UV_CACHE_DIR=.uv_cache \
# Python
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONIOENCODING=utf-8 \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
# pip
PIP_DISABLE_PIP_VERSION_CHECK=on \
# poetry
POETRY_HOME="$BASE_DIR/poetry" \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
# venv and requirements path
VIRTUAL_ENV="$BASE_DIR/venv" \
# cache path is HOME/.cache
CACHE_PATH="/root/.cache" \
SOURCE_PATH="$BASE_DIR/$SOURCE_DIR_NAME"

ENV PATH="$POETRY_HOME/bin:$VIRTUAL_ENV/bin:$PATH"

RUN python -m venv $VIRTUAL_ENV

ENV PYTHONPATH="$BASE_DIR:$PYTHONPATH"


FROM python-base AS builder-base

RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN --mount=type=cache,target=$CACHE_PATH \
curl -sSL https://install.python-poetry.org | python -

WORKDIR $BASE_DIR

COPY poetry.lock pyproject.toml ./

RUN --mount=type=cache,target=$CACHE_PATH \
poetry install --no-root --only main --compile


FROM builder-base AS development

WORKDIR $BASE_DIR

RUN --mount=type=cache,target=$CACHE_PATH \
poetry install --no-root --compile

CMD ["bash"]


FROM python-base AS production
# app
APP_DIR=/app \
SOURCE_DIR_NAME=youtube_automanager

COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $VIRTUAL_ENV $VIRTUAL_ENV
COPY entrypoint.sh /app/

RUN chmod +x /app/entrypoint.sh
WORKDIR $APP_DIR

WORKDIR $BASE_DIR
RUN --mount=type=cache,target=$UV_CACHE_DIR \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --no-install-project --no-dev

COPY poetry.lock pyproject.toml ./
COPY $SOURCE_DIR_NAME ./$SOURCE_DIR_NAME/
COPY $SOURCE_DIR_NAME $SOURCE_DIR_NAME

VOLUME /app/config
HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=5 \
CMD curl localhost:${PORT}/health || exit 1

EXPOSE 8080
ENTRYPOINT []

ENTRYPOINT ["/app/entrypoint.sh"]
CMD uv run python -m youtube_automanager.runners.automanage
5 changes: 0 additions & 5 deletions entrypoint.sh

This file was deleted.

2,992 changes: 0 additions & 2,992 deletions poetry.lock

This file was deleted.

49 changes: 23 additions & 26 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
[tool.poetry]
[project]
name = "youtube-automanager"
version = "0.1.0"
description = ""
authors = ["Alexey Rubaseff <[email protected]>"]
description = "Adds videos to your YouTube playlists automatically based on rules"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"fastapi>=0.115.5",
"global-logger>=0.4.2",
"google-api-python-client>=2.154.0",
"google-auth-oauthlib>=1.2.1",
"knockknock>=0.1.8.1",
"oauth2client>=4.1.3",
"pendulum>=3.0.0",
"python-worker>=2.2.4",
"python-youtube>=0.9.7",
"pyyaml>=6.0.2",
"sqlalchemy>=2.0.36",
"trustme>=1.2.0",
"uvicorn>=0.32.1",
]

[tool.poetry.dependencies]
python = "^3.12"
global-logger = "^0.4.2"
pyyaml = "^6.0.2"
sqlalchemy = "^2.0.34"
fastapi = "^0.114.0"
uvicorn = "^0.30.6"
trustme = "^1.1.0"
oauth2client = "^4.1.3"
google-auth-oauthlib = "^1.2.1"
google-api-python-client = "^2.144.0"
python-worker = "^2.2.4"
pendulum = "^3.0.0"
python-youtube = "^0.9.6"
knockknock = "^0.1.8.1"


[tool.poetry.group.dev.dependencies]
poetry = "^1.8.3"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[dependency-groups]
dev = [
"pre-commit>=4.0.1",
"ruff>=0.8.0",
]
75 changes: 75 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
line-length = 120
indent-width = 4

exclude = [
".git",
".ruff_cache",
".tox",
".venv*",
".vscode",
"__pypackages__",
"_build",
"build",
"dist",
"tryouts*.py",
]

[lint]
select = [
"ALL",
]
ignore = [
"UP031", # percent
"G002", # percent
"UP004", # class inherits from object
"TRY002", # own exception
"D212", # Multi-line docstring summary should start at the first line
"ANN201", # Missing return type annotation
"ANN202", # Missing return type annotation
"ANN001", # Missing type annotation
"ANN002", # Missing type annotation
"ANN003", # Missing type annotation
"ANN204", # Missing type annotation
"C408", # Unnecessary `dict` call (rewrite as a literal)
"D415", # First line should end with a period
"D400", # First line should end with a period
"D205", # 1 blank line required between summary line and description
"ANN101", # Missing type annotation for `self` in method
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed
"D203", # no-blank-line-before-class (incompatible with formatter)
"D212", # multi-line-summary-first-line (incompatible with formatter)
"FIX002", # Line contains TODO, consider resolving an issue
"TD003", # Missing issue link on the line following this TODO
"D103", # docstring
"D101", # docstring
"D102", # docstring
"D105", # docstring
"D100", # docstring
"D107", # docstring
"ANN205", # docstring
"ANN206", # docstring
"S101", # assert usage
"I001", # assert usage
"G004", # Logging statement uses f-string
"RET503", # Missing explicit `return` at the end of function able to return non-`None` value
"PLR0911", # Too many returns
"TD002", # Missing author in TODO
"FBT002", # Boolean default positional argument in function definition
"S104", # Possible binding to all interfaces
"PIE790", # Unnesessary `pass` statement
"RET504", # Unnessary assign before `return` statement
]
[lint.per-file-ignores]
"__init__.py" = ["F401", "D104"]
"tests/*.py" = ["S101", "D103", "ANN201", "D100"]

[lint.flake8-quotes]
docstring-quotes = "double"

[format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
docstring-code-format = false
docstring-code-line-length = "dynamic"
13 changes: 13 additions & 0 deletions script/build_run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

@if not defined DOCKER_HOST set DOCKER_HOST=tcp://docker:2375
@if not defined STAGE set STAGE=production
@if not defined CONTAINER_NAME set CONTAINER_NAME=test
@if not defined DOCKERFILENAME set DOCKERFILENAME=Dockerfile

docker kill %CONTAINER_NAME%
docker rm %CONTAINER_NAME%
docker build -f %DOCKERFILENAME% --target %STAGE% -t %CONTAINER_NAME%:latest . || exit /b 1
docker run --init --env-file="%~dp0..\.env" --name %CONTAINER_NAME% -t -d %CONTAINER_NAME% || exit /b 1
where nircmd >nul 2>nul && nircmd beep 500 500
docker logs %CONTAINER_NAME%
docker exec -it %CONTAINER_NAME% bash
12 changes: 9 additions & 3 deletions script/deploy.cmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
@echo off
setlocal enabledelayedexpansion enableextensions

poetry check || goto :end
if not exist poetry.lock poetry lock || goto :end
if exist .python-version (
uv sync --locked || goto :end
if not exist uv.lock uv lock || goto :end
) else (
poetry check || goto :end
if not exist poetry.lock poetry lock || goto :end
)

if not defined DOCKER_BUILDKIT set DOCKER_BUILDKIT=1
if not defined DOCKERFILE set DOCKERFILE=Dockerfile
if not defined DOCKER_REGISTRY set DOCKER_REGISTRY=registry.alertua.duckdns.org
echo DOCKER_REGISTRY: %DOCKER_REGISTRY%

Expand Down Expand Up @@ -69,7 +75,7 @@ if not defined DOCKER_REMOTE (
)

"%DOCKER_EXE%" --version
"%DOCKER_EXE%" build -t %BUILD_TAG% %BUILD_PATH% %* || goto :end
"%DOCKER_EXE%" build -t %BUILD_TAG% %BUILD_PATH% --target production %* || goto :end
"%DOCKER_EXE%" push %DOCKER_REGISTRY%/%IMAGE_NAME% || goto :end

@REM net stop %DOCKER_SERVICE% || goto :end
Expand Down
Loading

0 comments on commit a991a02

Please sign in to comment.