Skip to content

Commit

Permalink
move from rye to uv
Browse files Browse the repository at this point in the history
  • Loading branch information
inean committed Sep 13, 2024
1 parent 0cfe576 commit 9f9e1f0
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 207 deletions.
46 changes: 21 additions & 25 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
# Builder image
FROM debian:bookworm-slim AS builder

LABEL maintainer="inean <inean.es+contact@gmail.com>"

# hadolint ignore=DL3008
RUN <<EOF
apt-get update
apt-get install -y --no-install-recommends ca-certificates curl
EOF

# Install and initialize rye
ENV RYE_HOME="/opt/rye"
ENV PATH="$RYE_HOME/shims:$PATH"

# hadolint ignore=DL4006
RUN <<EOF
curl -sSf https://rye.astral.sh/get | RYE_INSTALL_OPTION="--yes" bash
rye config --set-bool behavior.global-python=true
rye config --set-bool behavior.use-uv=true
EOF

# Copy project
WORKDIR /opt
# Download the latest installer
ADD https://astral.sh/uv/install.sh uv-installer.sh

# Find pinned python version on current project and download it
COPY ./.python-version ./
RUN rye pin "$(cat .python-version)"
# Run the installer then remove it
RUN sh uv-installer.sh

# Devcontainer image
FROM mcr.microsoft.com/vscode/devcontainers/base:bookworm
COPY --from=builder /opt/rye /opt/rye

ENV RYE_HOME="/opt/rye"
ENV PATH="$RYE_HOME/shims:$PATH"
# Set uv
ENV UV_HOME="/opt/.cargo/bin"
ENV PATH="$UV_HOME/:$PATH"
COPY --from=builder /root/.cargo/bin/uv $UV_HOME/uv

# Configure uv behavior
ENV PYTHONUNBUFFERED=True
ENV UV_LINK_MODE=copy

# Use rye as the default python and uv as default package manager
RUN <<EOF
rye config --set-bool behavior.global-python=true
rye config --set-bool behavior.use-uv=true
EOF
# Set workdir
WORKDIR /opt
# Define version of python interpreter to use
COPY ./.python-version ./
# Pin and install python interpreter
RUN uv python pin "$(cat .python-version)"

RUN chown -R vscode $RYE_HOME
# let vscode user to use uv
RUN chown -R vscode $UV_HOME
31 changes: 23 additions & 8 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,40 @@
"exiasr.hadolint",
"kevinrose.vsc-python-indent",
"ms-azuretools.vscode-docker",
"ms-python.mypy-type-checker",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-vscode-remote.remote-containers",
"shardulm94.trailing-spaces",
"usernamehw.errorlens",
"yzhang.markdown-all-in-one"
// Project related extensions
],
"settings": {
// Editor settings
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,

// Python setttings
"python.testing.pytestArgs": [
"tests"
],
"python.testing.pytestEnabled": true,
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true,
"python.analysis.extraPaths": [
"${containerWorkspaceFolder}/types"
],
"python.defaultInterpreterPath": "${containerWorkspaceFolder}/.venv/bin/python",
"python.venvPath": "${containerWorkspaceFolder}/.venv",
//Extensions
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
},
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.rulers": [
88
]
"editor.formatOnSave": true
},
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
Expand All @@ -66,10 +81,10 @@
},

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "rye sync -f",
"postCreateCommand": "uv sync --dev --all-extras",

// use 'postStartCommand' to run commands after the container is started.
"postStartCommand": "which git && rye run pre-commit install",
"postStartCommand": "which git && uv run pre-commit install",

// Run vscode-server as non-root user
"containerUser": "vscode"
Expand Down
61 changes: 0 additions & 61 deletions .github/actions/setup-python-with-rye/action.yml

This file was deleted.

52 changes: 52 additions & 0 deletions .github/actions/setup-python-with-uv/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: Setup uv
description: Install Python with uv

inputs:
python-version:
description: Python version
required: false
uv-sync-options:
description: Options to pass to uv sync
required: false
default: "--locked --dev --all-extras"

outputs:
python-version:
description: The Python version used
value: ${{ steps.extract-python-version.outputs.python-version }}

runs:
using: composite
steps:
- name: Extract Python Version
id: extract-python-version
uses: ./.github/actions/extract-python-version
with:
python-version: ${{ inputs.python-version }}

- name: Install uv
uses: astral-sh/setup-uv@v2

- name: Pin Python Version
run: |
export PYTHONUNBUFFERED=True
uv python pin ${{ steps.extract-python-version.outputs.python-version }}
shell: bash

- name: Install dependencies
run: uv sync ${{ inputs.uv-sync-options}}
shell: bash

- name: Normalize UV Sync Options
id: normalize-uv-sync-options
run: |
NORMALIZED_UV_SYNC_OPTIONS=$(echo "${{ inputs.uv-sync-options }}" | tr '[:upper:]' '[:lower:]' | tr -c '[:alnum:]' '-')
echo "normalized-uv-sync-options=$NORMALIZED_UV_SYNC_OPTIONS" >> $GITHUB_ENV
shell: bash

- uses: actions/cache@v4
id: cache-uv
with:
path: ~/.cache/uv
key: ${{ runner.os }}-python-${{ steps.extract-python-version.outputs.python-version }}-uv-sync-${{ env.normalized-uv-sync-options }}
3 changes: 1 addition & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ updates:
- package-ecosystem: github-actions
directory: /
schedule:
nterval: daily
interval: daily
time: "08:00"
timezone: "Europe/Madrid"
interval: weekly
28 changes: 11 additions & 17 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
---
name: Code CI

on:

push:
branches: [ main ]

pull_request:
branches: [ main ]
on: [push, pull_request]

jobs:
lint:
Expand All @@ -16,40 +10,40 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Python with Rye
uses: ./.github/actions/setup-python-with-rye
- name: Setup Python with uv
uses: ./.github/actions/setup-python-with-uv

- name: Lint
run: rye run ruff check --output-format=github .
run: uv run ruff check --output-format=github .

format:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Python with Rye
uses: ./.github/actions/setup-python-with-rye
- name: Setup Python with uv
uses: ./.github/actions/setup-python-with-uv

- name: Format
run: rye run ruff format . --check --diff
run: uv run ruff format . --check --diff

tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
python-version: ['3.11', '3.12']
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Python ${{ matrix.python-version }} with Rye
uses: ./.github/actions/setup-python-with-rye
- name: Setup Python ${{ matrix.python-version }} with uv
uses: ./.github/actions/setup-python-with-uv
with:
python-version: ${{ matrix.python-version }}

- name: Run Pytest if directory exists
run: rye run pytest --cov=src --cov-report=xml --junitxml=junit.xml
run: uv run pytest --cov=src --cov-report=xml --junitxml=junit.xml

- name: Upload test results to Codecov
if: ${{ !env.ACT }}
Expand Down
38 changes: 28 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
---
default_stages: [commit]
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
rev: v0.6.4
hooks:
- id: ruff
name: Ruff check
description: "Run 'ruff check' for extremely fast Python linting"
args: [ --fix ]

- id: ruff-format
name: Ruff format
description: "Run 'ruff format' for extremely fast Python formatting"
- id: ruff
name: Ruff linter
args: [--fix]
types_or: [python, pyi]
- id: ruff-format
name: Ruff format
types_or: [python, pyi]
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.13
hooks:
- id: mdformat
additional_dependencies:
- mdformat-ruff

- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
rev: v2.13.0-beta
hooks:
- id: hadolint
name: Lint Dockerfiles
Expand All @@ -32,3 +38,15 @@ repos:
stages: [commit]
files: ^pyproject\.toml$
additional_dependencies: [toml]

- id: pyupgrade
name: Pyupgrade
entry: pyupgrade --py311-plus
types: [python]
language: system

- id: deptry
name: Deptry
entry: deptry src tests
language: system
pass_filenames: false
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10
3.11
Loading

0 comments on commit 9f9e1f0

Please sign in to comment.