Skip to content

Commit

Permalink
build: always use uv for dependency management (#265)
Browse files Browse the repository at this point in the history
Modifies the build tooling to always use `uv pip` instead of `pip` and `uv build` instead of `python -m build`.

Additional checks are added to ensure that `uv` exists on the system. If
`uv` does not exists, then it is installed via `pip`. If `pip` is not
installed, it is installed via `python -m ensurepip`. The `pip` check is
required for a virtual environment created by `uv venv` since by
default, `uv venv` does not install `pip`.

The UV_SYSTEM_PYTHON environment variable is set in CI and Docker to
force `uv` to use the system Python environment instead of depending on
a virtual environment. See
https://docs.astral.sh/uv/configuration/environment/

This reduces the execution time of `make` from 22s to 5s on my machine
without a Pip cache. With a hydrated PIp cache, the execution time is
reduced from 8s to 4s.
  • Loading branch information
tdstein authored Sep 9, 2024
1 parent 700b118 commit 2a36391
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
UV_SYSTEM_PYTHON: true
jobs:
lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
UV_SYSTEM_PYTHON: true
jobs:
cov:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
tags:
- "v*.*.*"
env:
UV_SYSTEM_PYTHON: true
jobs:
default:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
UV_SYSTEM_PYTHON: true
jobs:
preview:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM python:3

ENV UV_SYSTEM_PYTHON=true

RUN apt-get update && apt-get install -y make

WORKDIR /sdk
Expand Down
30 changes: 19 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ include vars.mk

.DEFAULT_GOAL := all

.PHONY: build clean cov default deps dev docs fmt fix install it lint test uninstall version help
.PHONY: build clean cov default deps dev docs ensure-uv fmt fix install it lint test uninstall version help

all: deps dev test lint build

build:
$(PYTHON) -m build
$(UV) build

clean:
$(MAKE) -C ./docs $@
$(MAKE) -C ./integration $@
rm -rf .coverage .mypy_cache .pytest_cache .ruff_cache *.egg-info build coverage.xml dist htmlcov coverage.xml
find src -name "_version.py" -exec rm -rf {} +
find . -name "*.egg-info" -exec rm -rf {} +
find . -name "*.pyc" -exec rm -f {} +
find . -name "__pycache__" -exec rm -rf {} +
find . -name "_version.py" -exec rm -rf {} +
find . -type d -empty -delete

cov:
Expand All @@ -29,21 +29,29 @@ cov-html:
cov-xml:
$(PYTHON) -m coverage xml

deps:
$(PIP) install --upgrade pip setuptools wheel -r requirements.txt -r requirements-dev.txt
deps: ensure-uv
$(UV) pip install --upgrade pip setuptools wheel -r requirements.txt -r requirements-dev.txt

dev:
$(PIP) install -e .
dev: ensure-uv
$(UV) pip install -e .

docs:
$(MAKE) -C ./docs

ensure-uv:
@if ! command -v $(UV) >/dev/null 2>&1; then \
if ! command -v pip >/dev/null 2>&1; then \
$(PYTHON) -m ensurepip; \
fi; \
$(PYTHON) -m pip install uv; \
fi

fmt:
$(PYTHON) -m ruff check --fix
$(PYTHON) -m ruff format .

install:
$(PIP) install dist/*.whl
install: ensure-uv
$(UV) pip install dist/*.whl

it:
$(MAKE) -C ./integration
Expand All @@ -55,8 +63,8 @@ lint:
test:
$(PYTHON) -m coverage run --source=src -m pytest tests

uninstall:
$(PIP) uninstall $(NAME)
uninstall: ensure-uv
$(UV) pip uninstall $(NAME)

version:
@$(PYTHON) -m setuptools_scm
Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ clean:
find . -type d -empty -delete

deps:
$(PIP) install --upgrade pip -r requirements-site.txt
$(UV) pip install --upgrade pip -r requirements-site.txt
$(QUARTO) add --no-prompt posit-dev/[email protected]
$(QUARTO) add --no-prompt machow/quartodoc

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rsconnect-python
ruff
setuptools
setuptools-scm
uv
20 changes: 6 additions & 14 deletions vars.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ENV ?= dev

IMAGE_TAG ?= $(NAME):latest

NAME := posit-sdk-py
NAME := posit-sdk

ifeq ($(ENV), prod)
NETLIFY_ARGS := --prod
Expand All @@ -28,20 +28,12 @@ endif

NETLIFY_SITE_ID ?= 5cea1f56-7935-4387-975a-18a7905d15ee

ifneq ($(shell command -v uv 2>/dev/null),)
PYTHON := python
else
PYTHON := python3
endif

ifneq ($(shell command -v uv 2>/dev/null),)
PIP := uv pip
else
PIP := pip3
endif

SHELL := /bin/bash
PYTHON := $(shell command -v python || command -v python3)

QUARTO ?= quarto

QUARTODOC ?= quartodoc

SHELL := /bin/bash

UV := uv

0 comments on commit 2a36391

Please sign in to comment.