Skip to content

Commit 7ca86da

Browse files
committed
Migrate to uv
1 parent e5fe5c8 commit 7ca86da

8 files changed

Lines changed: 173 additions & 265 deletions

File tree

.dockerignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Git and version control
2+
.git
3+
.gitignore
4+
5+
# Virtual environments
6+
.venv
7+
8+
# Python cache and bytecode
9+
__pycache__
10+
*.py[cod]
11+
*$py.class
12+
*.so
13+
*.egg-info
14+
*.egg
15+
16+
# Testing
17+
tests
18+
.pytest_cache
19+
.coverage
20+
.coverage.*
21+
htmlcov
22+
.tox
23+
.nox
24+
.hypothesis
25+
26+
# Development tools and docs
27+
Makefile
28+
.pre-commit-config.yaml
29+
CHANGELOG.md
30+
README.md
31+
32+
# IDE and editor files
33+
.vscode
34+
.idea
35+
*.swp
36+
*.swo
37+
*~
38+
39+
# OS files
40+
.DS_Store
41+
Thumbs.db
42+
43+
# Build artifacts
44+
build
45+
dist
46+
*.whl
47+
*.tar.gz
48+
49+
# Linting and type checking caches
50+
.ruff_cache
51+
.mypy_cache
52+
.dmypy.json
53+
54+
# Misc
55+
*.bak
56+
.cache
57+
58+
# Docker files
59+
Dockerfile
60+
.dockerignore

.github/workflows/actions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434

3535
unit-tests:
3636
name: Run unit tests
37-
uses: EO-DataHub/github-actions/.github/workflows/unit-tests-python.yaml@main
37+
uses: EO-DataHub/github-actions/.github/workflows/unit-tests-python-uv.yaml@main
3838

3939
get-tag-name:
4040
runs-on: ubuntu-latest

Dockerfile

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# syntax=docker/dockerfile:1
2-
FROM python:3.11-slim-bullseye
2+
FROM ghcr.io/astral-sh/uv:python3.13-trixie-slim
33

4-
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
5-
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
4+
ENV UV_NO_DEV=1
65

7-
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
8-
--mount=type=cache,target=/var/lib/apt,sharing=locked \
9-
apt-get update -y && apt-get upgrade -y
6+
WORKDIR /app
107

11-
WORKDIR /CHANGME-component-name
12-
ADD LICENSE.txt requirements.txt ./
13-
ADD CHANGEME-module-name ./CHANGEME-module-name/
14-
ADD pyproject.toml ./
15-
RUN --mount=type=cache,target=/root/.cache/pip pip3 install -r requirements.txt .
8+
# Install dependencies
9+
RUN --mount=type=cache,target=/root/.cache/uv \
10+
--mount=type=bind,source=uv.lock,target=uv.lock \
11+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
12+
uv sync --frozen --no-install-project
1613

17-
# Change as required, eg
18-
# CMD ["gunicorn", "-w", "2", "-b", "0.0.0.0", "-k", "uvicorn.workers.UvicornWorker", "--log-level", "debug", "mymodule.main:app"]
19-
CMD python -m my.module
14+
# Copy project files
15+
COPY . /app
2016

17+
# Sync the project
18+
RUN --mount=type=cache,target=/root/.cache/uv \
19+
uv sync --frozen
20+
21+
# Change as required
22+
CMD ["uv", "run", "--no-sync", "python", "-m", "my.module"]

Makefile

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,54 @@
1-
.PHONY: dockerbuild dockerpush test testonce ruff black lint isort pre-commit-check requirements-update requirements setup
21
VERSION ?= latest
32
IMAGENAME = CHANGEME
4-
DOCKERREPO ?= public.ecr.aws/n1b3o1k2/ukeodhp
3+
DOCKERREPO ?= public.ecr.aws/eodh
4+
uv-run ?= uv run --no-sync
55

6+
.PHONY: dockerbuild
67
dockerbuild:
78
DOCKER_BUILDKIT=1 docker build -t ${IMAGENAME}:${VERSION} .
89

9-
dockerpush: dockerbuild testdocker
10+
.PHONY: dockerpush
11+
dockerpush: dockerbuild
1012
docker tag ${IMAGENAME}:${VERSION} ${DOCKERREPO}/${IMAGENAME}:${VERSION}
1113
docker push ${DOCKERREPO}/${IMAGENAME}:${VERSION}
1214

15+
.PHONY: test
1316
test:
14-
./venv/bin/ptw CHANGEME-test-package-names
17+
${uv-run} ptw CHANGEME-test-package-names
1518

19+
.PHONY: testonce
1620
testonce:
17-
./venv/bin/pytest
21+
${uv-run} pytest
1822

19-
ruff:
20-
./venv/bin/ruff check .
21-
22-
black:
23-
./venv/bin/black .
24-
25-
isort:
26-
./venv/bin/isort . --profile black
27-
28-
validate-pyproject:
29-
validate-pyproject pyproject.toml
30-
31-
lint: ruff black isort validate-pyproject
32-
33-
requirements.txt: venv pyproject.toml
34-
./venv/bin/pip-compile
23+
.git/hooks/pre-commit:
24+
${uv-run} pre-commit install
25+
curl -o .pre-commit-config.yaml https://raw.githubusercontent.com/EO-DataHub/github-actions/main/.pre-commit-config-python.yaml
3526

36-
requirements-dev.txt: venv pyproject.toml
37-
./venv/bin/pip-compile --extra dev -o requirements-dev.txt
27+
.PHONY: setup
28+
setup: update .git/hooks/pre-commit
3829

39-
requirements: requirements.txt requirements-dev.txt
30+
.PHONY: pre-commit
31+
pre-commit: ${uv-run} pre-commit
4032

41-
requirements-update: venv
42-
./venv/bin/pip-compile -U
43-
./venv/bin/pip-compile --extra dev -o requirements-dev.txt -U
33+
.PHONY: pre-commit-all
34+
pre-commit-all: ${uv-run} pre-commit run --all-files
4435

45-
venv:
46-
virtualenv -p python3.11 venv
47-
./venv/bin/python -m ensurepip -U
48-
./venv/bin/pip3 install pip-tools
36+
.PHONY: check
37+
check:
38+
${uv-run} ruff check
39+
${uv-run} ruff format --check --diff
40+
${uv-run} pyright
41+
${uv-run} validate-pyproject pyproject.toml
4942

50-
.make-venv-installed: venv requirements.txt requirements-dev.txt
51-
./venv/bin/pip3 install -r requirements.txt -r requirements-dev.txt
52-
touch .make-venv-installed
43+
.PHONY: format
44+
format:
45+
${uv-run} ruff check --fix
46+
${uv-run} ruff format
5347

54-
.git/hooks/pre-commit:
55-
./venv/bin/pre-commit install
56-
curl -o .pre-commit-config.yaml https://raw.githubusercontent.com/EO-DataHub/github-actions/main/.pre-commit-config-python.yaml
48+
.PHONY: install
49+
install:
50+
uv sync --frozen
5751

58-
setup: venv requirements .make-venv-installed .git/hooks/pre-commit
52+
.PHONY: update
53+
update:
54+
uv sync

README.md

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,45 @@ When using this template remember to:
1515

1616
## Getting started
1717

18+
### Prerequisites
19+
20+
Install the `uv` package manager by following the [official documentation](https://docs.astral.sh/uv/getting-started/installation/).
21+
1822
### Install via makefile
1923

2024
```commandline
2125
make setup
2226
```
2327

24-
This will create a virtual environment called `venv`, build `requirements.txt` and
25-
`requirements-dev.txt` from `pyproject.toml` if they're out of date, install the Python
26-
and Node dependencies and install `pre-commit`.
27-
28-
It's safe and fast to run `make setup` repeatedly as it will only update these things if
29-
they have changed.
30-
31-
After `make setup` you can run `pre-commit` to run pre-commit checks on staged changes and
32-
`pre-commit run --all-files` to run them on all files. This replicates the linter checks that
33-
run from GitHub actions.
34-
35-
36-
### Alternative installation
37-
38-
You will need Python 3.11. On Debian you may need:
39-
* `sudo add-apt-repository -y 'deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal main'` (or `jammy` in place of `focal` for later Debian)
40-
* `sudo apt update`
41-
* `sudo apt install python3.11 python3.11-venv`
42-
43-
and on Ubuntu you may need
44-
* `sudo add-apt-repository -y 'ppa:deadsnakes/ppa'`
45-
* `sudo apt update`
46-
* `sudo apt install python3.11 python3.11-venv`
47-
48-
To prepare running it:
28+
This will create a Python virtual environment, install basic testing and linting
29+
dependencies and install `pre-commit`.
4930

50-
* `virtualenv venv -p python3.11`
51-
* `. venv/bin/activate`
52-
* `rehash`
53-
* `python -m ensurepip -U`
54-
* `pip3 install -r requirements.txt`
55-
* `pip3 install -r requirements-dev.txt`
56-
57-
You should also configure your IDE to use black so that code is automatically reformatted on save.
31+
After `make setup` you can run `make check` to run linting and type checking.
5832

5933
## Building and testing
6034

61-
This component uses `pytest` tests and the `ruff` and `black` linters. `black` will reformat your code in an
62-
opinionated way.
35+
This component uses `pytest` tests and the `ruff` linter and `pyright` type checker.
36+
37+
See Makefile for all available commands.
6338

6439
A number of `make` targets are defined:
6540
* `make test`: run tests continuously
6641
* `make testonce`: run tests once
67-
* `make lint`: lint and reformat
42+
* `make check`: run linting and type checking
43+
* `make format`: reformat code
44+
* `make install`: install dependencies
45+
* `make update`: update dependencies
6846
* `make dockerbuild`: build a `latest` Docker image (use `make dockerbuild `VERSION=1.2.3` for a release image)
6947
* `make dockerpush`: push a `latest` Docker image (again, you can add `VERSION=1.2.3`) - normally this should be done
7048
only via the build system and its GitHub actions.
7149

72-
## Managing requirements
73-
74-
Requirements are specified in `pyproject.toml`, with development requirements listed separately. Specify version
75-
constraints as necessary but not specific versions. After changing them:
76-
77-
* Run `pip-compile` (or `pip-compile -U` to upgrade requirements within constraints) to regenerate `requirements.txt`
78-
* Run `pip-compile --extra dev -o requirements-dev.txt` (again, add `-U` to upgrade) to regenerate
79-
`requirements-dev.txt`.
80-
* Run the `pip3 install -r requirements.txt` and `pip3 install -r requirements-dev.txt` commands again and test.
81-
* Commit these files.
82-
83-
If you see the error
84-
85-
```commandline
86-
Backend subprocess exited when trying to invoke get_requires_for_build_wheel
87-
Failed to parse /.../template-python/pyproject.toml
88-
```
89-
90-
then install and run `validate-pyproject pyproject.toml` and/or `pip3 install .` to check its syntax.
50+
## Managing dependencies
9151

92-
To check for vulnerable dependencies, run `pip-audit`.
52+
To add a new dependency, run `uv add <dependency>` or see `uv help add`.
9353

9454
## Releasing
9555

96-
Ensure that `make lint` and `make test` work correctly and produce no further changes to code formatting before
56+
Ensure that `make check` and `make test` work correctly and produce no further changes to code formatting before
9757
continuing.
9858

9959
Releases tagged `latest` and targeted at development environments can be created from the `main` branch. Releases for

0 commit comments

Comments
 (0)