Skip to content

Commit 2bbe5dd

Browse files
committed
Migrate to uv Python package manager
1 parent 87c64f4 commit 2bbe5dd

File tree

10 files changed

+1077
-412
lines changed

10 files changed

+1077
-412
lines changed

.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

Dockerfile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
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
6-
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
4+
ENV UV_NO_DEV=1
105

116
WORKDIR /app
12-
ADD LICENSE requirements.txt ./
13-
ADD pyproject.toml ./
14-
RUN --mount=type=cache,target=/root/.cache/pip pip3 install -r requirements.txt .
157

16-
ADD static ./static/
17-
ADD resource_catalogue_fastapi ./resource_catalogue_fastapi/
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
13+
14+
COPY . /app
15+
16+
# Sync the project
17+
RUN --mount=type=cache,target=/root/.cache/uv \
18+
uv sync --frozen
1819

19-
CMD ["uvicorn", "resource_catalogue_fastapi:app", "--host", "0.0.0.0", "--port", "8000"]
20+
CMD ["uv", "run", "--no-sync", "uvicorn", "resource_catalogue_fastapi:app", "--host", "0.0.0.0", "--port", "8000"]

Makefile

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
VERSION ?= latest
33
IMAGENAME = resource-catalogue-fastapi
44
DOCKERREPO ?= public.ecr.aws/eodh
5+
uv-run ?= uv run --no-sync
56

67
dockerbuild:
78
DOCKER_BUILDKIT=1 docker build -t ${IMAGENAME}:${VERSION} .
@@ -11,48 +12,30 @@ dockerpush: dockerbuild
1112
docker push ${DOCKERREPO}/${IMAGENAME}:${VERSION}
1213

1314
test:
14-
./venv/bin/ptw resource_catalogue_fastapi
15+
${uv-run} ptw resource_catalogue_fastapi
1516

1617
testonce:
17-
./venv/bin/pytest
18+
${uv-run} pytest
1819

1920
ruff:
20-
./venv/bin/ruff check .
21+
${uv-run} ruff check .
2122

2223
black:
23-
./venv/bin/black .
24+
${uv-run} black .
2425

2526
isort:
26-
./venv/bin/isort . --profile black
27+
${uv-run} isort . --profile black
2728

2829
validate-pyproject:
29-
validate-pyproject pyproject.toml
30+
${uv-run} validate-pyproject pyproject.toml
3031

3132
lint: ruff black isort validate-pyproject
3233

33-
requirements.txt: venv pyproject.toml
34-
./venv/bin/pip-compile
35-
36-
requirements-dev.txt: venv pyproject.toml
37-
./venv/bin/pip-compile --extra dev -o requirements-dev.txt
38-
39-
requirements: requirements.txt requirements-dev.txt
40-
41-
requirements-update: venv
42-
./venv/bin/pip-compile -U
43-
./venv/bin/pip-compile --extra dev -o requirements-dev.txt -U
44-
45-
venv:
46-
virtualenv -p python3.11 venv
47-
./venv/bin/python -m ensurepip -U
48-
./venv/bin/pip3 install pip-tools
49-
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
34+
uv-sync:
35+
${uv-run} sync --frozen
5336

5437
.git/hooks/pre-commit:
55-
./venv/bin/pre-commit install
38+
${uv-run} pre-commit install
5639
curl -o .pre-commit-config.yaml https://raw.githubusercontent.com/EO-DataHub/github-actions/main/.pre-commit-config-python.yaml
5740

58-
setup: venv requirements .make-venv-installed .git/hooks/pre-commit
41+
setup: uv-sync .git/hooks/pre-commit

README.md

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,25 @@ PULSAR_URL
2525
```
2626
## Getting started
2727

28+
### Prerequisites
29+
30+
Install the `uv` package manager by following the [official documentation](https://docs.astral.sh/uv/getting-started/installation/).
31+
2832
### Install via makefile
2933

3034
```commandline
3135
make setup
3236
```
3337

34-
This will create a virtual environment called `venv`, build `requirements.txt` and
35-
`requirements-dev.txt` from `pyproject.toml` if they're out of date, install the Python
38+
This will create a Python virtual environment, install the Python
3639
and Node dependencies and install `pre-commit`.
3740

38-
It's safe and fast to run `make setup` repeatedly as it will only update these things if
39-
they have changed.
41+
It's safe and fast to run `make setup` repeatedly.
4042

4143
After `make setup` you can run `pre-commit` to run pre-commit checks on staged changes and
4244
`pre-commit run --all-files` to run them on all files. This replicates the linter checks that
4345
run from GitHub actions.
4446

45-
46-
### Alternative installation
47-
48-
You will need Python 3.11. On Debian you may need:
49-
* `sudo add-apt-repository -y 'deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal main'` (or `jammy` in place of `focal` for later Debian)
50-
* `sudo apt update`
51-
* `sudo apt install python3.11 python3.11-venv`
52-
53-
and on Ubuntu you may need
54-
* `sudo add-apt-repository -y 'ppa:deadsnakes/ppa'`
55-
* `sudo apt update`
56-
* `sudo apt install python3.11 python3.11-venv`
57-
58-
To prepare running it:
59-
60-
* `virtualenv venv -p python3.11`
61-
* `. venv/bin/activate`
62-
* `rehash`
63-
* `python -m ensurepip -U`
64-
* `pip3 install -r requirements.txt`
65-
* `pip3 install -r requirements-dev.txt`
66-
67-
You should also configure your IDE to use black so that code is automatically reformatted on save.
68-
6947
## Building and testing
7048

7149
This component uses `pytest` tests and the `ruff` and `black` linters. `black` will reformat your code in an opinionated way.
@@ -80,19 +58,6 @@ A number of `make` targets are defined:
8058
* `make dockerpush`: push a `latest` Docker image (again, you can add `VERSION=1.2.3`) - normally this should be done
8159
only via the build system and its GitHub actions.
8260

83-
## Managing requirements
84-
85-
Requirements are specified in `pyproject.toml`, with development requirements listed separately. Specify version
86-
constraints as necessary but not specific versions. After changing them:
87-
88-
* Run `pip-compile` (or `pip-compile -U` to upgrade requirements within constraints) to regenerate `requirements.txt`
89-
* Run `pip-compile --extra dev -o requirements-dev.txt` (again, add `-U` to upgrade) to regenerate
90-
`requirements-dev.txt`.
91-
* Run the `pip3 install -r requirements.txt` and `pip3 install -r requirements-dev.txt` commands again and test.
92-
* Commit these files.
93-
94-
If you see the error
95-
9661
```commandline
9762
Backend subprocess exited when trying to invoke get_requires_for_build_wheel
9863
Failed to parse /.../template-python/pyproject.toml

pyproject.toml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ dynamic = ["version"]
2828
description = "A FastApi deployment to handle requests to update the resource catalogue"
2929
readme = "README.md"
3030

31-
# Our target version is Python 3.11, but it may work with earlier versions.
32-
requires-python = ">=3.11"
31+
# Our target version is Python 3.13.
32+
requires-python = ">=3.13,<3.14"
3333

3434
license = {file = "LICENSE"}
3535

@@ -86,16 +86,20 @@ dependencies = [
8686
"kubernetes",
8787
]
8888

89-
# List additional groups of dependencies here (e.g. development
90-
# dependencies). Users will be able to install these using the "extras"
91-
# syntax, for example:
92-
#
93-
# $ pip install sampleproject[dev]
94-
#
95-
# Similar to `dependencies` above, these must be valid existing
96-
# projects.
97-
[project.optional-dependencies] # Optional
98-
dev = ["pip-tools", "pytest", "pytest-xdist", "pytest-mock", "pytest-watcher", "black", "ruff", "isort", "pre-commit", "httpx", "requests-mock"]
89+
[dependency-groups] # Optional
90+
dev = [
91+
"pytest",
92+
"pytest-xdist",
93+
"pytest-mock",
94+
"pytest-watcher",
95+
"black",
96+
"ruff",
97+
"isort",
98+
"pre-commit",
99+
"httpx",
100+
"requests-mock",
101+
"validate-pyproject>=0.24.1",
102+
]
99103

100104
# List URLs that are relevant to your project
101105
#

0 commit comments

Comments
 (0)