Skip to content

Commit 24105fb

Browse files
VIKTORVAV99Copilot
andauthored
set up package for dev with uv (#8589)
* set up package for dev with uv * update CI to use uv * use uv to download python in CI * cache python * do not cache python * update earthfile * update some other stuff * update poetry run to uv run * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * update dev container * update dev container --------- Co-authored-by: Copilot <[email protected]>
1 parent 4ec5adf commit 24105fb

19 files changed

+1612
-2413
lines changed

.devcontainer/devcontainer.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
{
22
"name": "Electricity Maps Dev",
3-
"image": "mcr.microsoft.com/devcontainers/universal:2",
3+
"image": "mcr.microsoft.com/devcontainers/python:1-3.10-bookworm",
44
"features": {
5-
"ghcr.io/devcontainers/features/python:1": {
6-
"version": "3.10"
7-
},
8-
"ghcr.io/devcontainers/features/node:1": {
9-
"version": "lts"
10-
},
11-
"ghcr.io/devcontainers-contrib/features/poetry:2": {},
12-
"ghcr.io/devcontainers-contrib/features/pnpm:2": {}
5+
"ghcr.io/devcontainer-community/devcontainer-features/astral.sh-uv:1": {}
136
},
7+
"postCreateCommand": "uv sync --group parser --group dev --group scripts",
148
"customizations": {
159
"vscode": {
1610
"extensions": [
1711
"ms-python.vscode-pylance",
1812
"ms-python.python",
1913
"esbenp.prettier-vscode",
20-
"dbaeumer.vscode-eslint",
2114
"redhat.vscode-yaml",
2215
"charliermarsh.ruff"
2316
]

.github/pull_request_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
### Double check
1414

15-
- [ ] I have tested my parser changes locally with `poetry run test_parser "zone_key"`
16-
- [ ] I have run `pnpx prettier@2 --write .` and `poetry run format` in the top level directory to format my changes.
15+
- [ ] I have tested my parser changes locally with `uv run test_parser "zone_key"`
16+
- [ ] I have run `pnpx prettier@2 --write .` and `uv run format` in the top level directory to format my changes.

.github/workflows/CI.yml

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -495,34 +495,21 @@ jobs:
495495
steps:
496496
- name: Checkout
497497
uses: actions/checkout@v6
498-
- name: Install poetry
499-
run: |
500-
pipx install "poetry==2.*"
501-
- name: Get poetry version
502-
id: get-poetry-version
503-
run: |
504-
echo "$(poetry --version | awk '{print "poetry-version="$3}' | tr -d '()')" >> $GITHUB_OUTPUT
505-
- name: Setup Python
506-
uses: actions/setup-python@v6
498+
- name: Install uv
499+
uses: astral-sh/setup-uv@v7
507500
with:
508-
python-version-file: '.python-version'
509-
- name: Restore cache
510-
id: cache
511-
uses: actions/cache@v5
512-
with:
513-
path: .venv
514-
key: ${{ runner.os }}-venv-poetry_${{steps.get-poetry-version.outputs.poetry-version}}-${{ hashFiles('poetry.lock') }}
515-
restore-keys: ${{ runner.os }}-venv-poetry_${{steps.get-poetry-version.outputs.poetry-version}}
501+
enable-cache: true
502+
cache-suffix: 'formatting'
503+
python-version: '3.10'
516504
- name: Install Dependencies
517-
if: steps.cache.outputs.cache-hit != 'true'
518505
run: |
519-
poetry install -E parsers --sync
506+
uv sync --group parsers
520507
- name: Check formatting
521508
run: |
522-
poetry run ruff format . --check
509+
uv run ruff format . --check
523510
- name: Check linting
524511
run: |
525-
poetry run ruff check . --output-format=github
512+
uv run ruff check . --output-format=github
526513
527514
python_tests:
528515
name: Python / Tests
@@ -539,35 +526,22 @@ jobs:
539526
steps:
540527
- name: Checkout
541528
uses: actions/checkout@v6
542-
- name: Install poetry
543-
run: |
544-
pipx install "poetry==2.*"
545-
- name: Get poetry version
546-
id: get-poetry-version
547-
run: |
548-
echo "$(poetry --version | awk '{print "poetry-version="$3}' | tr -d '()')" >> $GITHUB_OUTPUT
549-
- name: Setup Python
550-
uses: actions/setup-python@v6
529+
- name: Install uv
530+
uses: astral-sh/setup-uv@v7
551531
with:
532+
enable-cache: true
533+
cache-suffix: 'tests'
552534
python-version: ${{ matrix.python-version }}
553-
- name: Restore cache
554-
id: cache
555-
uses: actions/cache@v5
556-
with:
557-
path: .venv
558-
key: ${{ runner.os }}-venv-python_${{ matrix.python-version }}-poetry_${{steps.get-poetry-version.outputs.poetry-version}}-${{ hashFiles('poetry.lock') }}
559-
restore-keys: ${{ runner.os }}-venv-python_${{ matrix.python-version }}-poetry_${{steps.get-poetry-version.outputs.poetry-version}}
560535
- name: Install libxml2-dev and tesseract-ocr
561536
run: |
562537
sudo apt-get update
563538
sudo apt-get install libxml2-dev tesseract-ocr tesseract-ocr-eng
564-
- name: Install Poetry dependencies
565-
if: steps.cache.outputs.cache-hit != 'true'
539+
- name: Install Dependencies
566540
run: |
567-
poetry install -E parsers --sync
541+
uv sync --group parsers --group dev
568542
- name: Run tests
569543
run: |
570-
poetry run test
544+
uv run test
571545
572546
# Config checks
573547
validate_config_filenames:

.github/workflows/create-release.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
- patch
1212
- minor
1313
- major
14+
- dev
1415
jobs:
1516
create-release:
1617
runs-on: ubuntu-latest
@@ -23,7 +24,6 @@ jobs:
2324
- name: Checkout
2425
uses: actions/checkout@v6
2526
with:
26-
ref: master
2727
token: ${{ secrets.BOT_GITHUB_TOKEN }}
2828
- name: Configure git
2929
run: |
@@ -42,13 +42,17 @@ jobs:
4242
- uses: actions/setup-python@v6
4343
with:
4444
python-version: '3.10.13'
45-
- name: Install Poetry
46-
run: |
47-
python -m pip install --upgrade pip
48-
pip install poetry==2.1.1
45+
- name: Install uv
46+
uses: astral-sh/setup-uv@v7
4947
- name: Bump version
5048
run: |
51-
poetry version ${{ inputs.type }}
49+
if [[ "${{ inputs.type }}" == "dev" ]]; then
50+
# Bump patch first to ensure we are moving forward, then add dev suffix
51+
# This handles 1.2.1 -> 1.2.2.dev0
52+
uv version --bump patch --bump dev
53+
else
54+
uv version --bump ${{ inputs.type }}
55+
fi
5256
- name: Commit changes
5357
run: |
5458
git add pyproject.toml
@@ -57,13 +61,11 @@ jobs:
5761
uses: ad-m/[email protected]
5862
with:
5963
github_token: ${{ secrets.BOT_GITHUB_TOKEN }}
60-
branch: master
64+
branch: ${{ github.ref_name }}
6165
- name: Build package
6266
run: |
63-
poetry build
67+
uv build
6468
- name: Publish package
6569
run: |
6670
export ACCESS_TOKEN=$(gcloud auth print-access-token)
67-
poetry config keyring.enabled false
68-
poetry config repositories.internal https://europe-west1-python.pkg.dev/tmrow-152415/libs/
69-
poetry publish --repository=internal --skip-existing --username=oauth2accesstoken --password "$ACCESS_TOKEN"
71+
uv publish --publish-url https://europe-west1-python.pkg.dev/tmrow-152415/libs/ --username oauth2accesstoken --password "$ACCESS_TOKEN"

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ The zone configurations hold information such as the installed capacity, which p
4646

4747
To get started with editing the parsers use the following steps:
4848

49-
1. Run `poetry install -E parsers` to install all needed dependencies.
50-
2. Use `poetry run test_parser ZONE_KEY` to test any parser changes.
49+
1. Run `uv sync --group parsers` to install all needed dependencies.
50+
2. Use `uv run test_parser ZONE_KEY` to test any parser changes.
5151

52-
Note: This requires you to have [Python 3.10][python homepage] and [Poetry][poetry homepage] installed, you can see their respective installation guides here:
52+
Note: This requires you to have [Python 3.10][python homepage] and [uv][uv homepage] installed, you can see their respective installation guides here:
5353

5454
- [Downloading Python][python install guide]
55-
- [poetry installation][poetry install guide]
55+
- [uv installation][uv install guide]
5656

5757
### Parser information
5858

@@ -66,7 +66,7 @@ For an example of how a parser can look we have an example here: </br> [electric
6666

6767
We use [black][black homepage] and [isort][isort homepage] as code formatters for python which is automatically checked in the CI job `Python / Formatting`.
6868

69-
If this jobs fails and you need to manually format the code you can run `poetry run format` in the top level of the repository.
69+
If this jobs fails and you need to manually format the code you can run `uv run format` in the top level of the repository.
7070

7171
Check the [wiki page][wiki python code formatting] for more details and tips.
7272

@@ -181,10 +181,10 @@ If it is frontend changes it will be deployed to our staging environment at http
181181

182182
<!-- Link definitions to keep the text clean -->
183183

184-
[poetry homepage]: https://python-poetry.org/
184+
[uv homepage]: https://github.com/astral-sh/uv
185185
[python homepage]: https://www.python.org/
186186
[python install guide]: https://wiki.python.org/moin/BeginnersGuide/Download
187-
[poetry install guide]: https://python-poetry.org/docs/#installation
187+
[uv install guide]: https://docs.astral.sh/uv/getting-started/installation/
188188
[example parser]: https://github.com/electricitymaps/electricitymaps-contrib/blob/master/electricitymap/contrib/parsers/examples/example_parser.py
189189
[black homepage]: https://github.com/psf/black
190190
[isort homepage]: https://pycqa.github.io/isort/

Earthfile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VERSION 0.8
2-
FROM python:3.10
2+
FROM astral/uv:python3.10-bookworm-slim
33
WORKDIR /contrib
44

55
linting-files:
@@ -33,17 +33,15 @@ api-files:
3333
COPY config/zone_names.json ./config/zone_names.json
3434
SAVE ARTIFACT .
3535

36-
poetry-lock:
37-
COPY poetry.lock .
36+
uv-lock:
37+
COPY uv.lock .
3838
SAVE ARTIFACT .
3939

4040
prepare:
4141
FROM +src-files-with-parsers
42-
RUN pip install "poetry==2.*"
4342
RUN apt-get update && apt-get install -y python3-opencv tesseract-ocr tesseract-ocr-jpn tesseract-ocr-eng libgl1
44-
RUN poetry config virtualenvs.create false
45-
COPY poetry.lock .
46-
RUN poetry install --compile -E parsers
43+
COPY uv.lock .
44+
RUN uv sync --frozen --group parsers --group dev --compile-bytecode
4745

4846
build:
4947
FROM +prepare
@@ -53,7 +51,7 @@ test:
5351
COPY tests ./tests
5452
COPY web/src/utils/constants.ts ./web/src/utils/constants.ts # TODO: python tests should not depend on this js file
5553
COPY web/geo/world.geojson ./web/geo/world.geojson
56-
RUN poetry run check
54+
RUN uv run check
5755

5856
# includes both test target and build target here to make sure both can work
5957
# we can split into two later if required

capacity_update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Usage: poetry run update_capacity --zone FR --target_datetime "2022-01-01"
2+
Usage: uv run update_capacity --zone FR --target_datetime "2022-01-01"
33
"""
44

55
import logging
@@ -33,8 +33,8 @@ def capacity_update(
3333
\n
3434
Examples
3535
-------
36-
>>> poetry run capacity_update --zone FR --target_datetime "2022-01-01"
37-
>>> poetry run capacity_update --source ENTSOE --target_datetime "2022-01-01"
36+
>>> uv run capacity_update --zone FR --target_datetime "2022-01-01"
37+
>>> uv run capacity_update --source ENTSOE --target_datetime "2022-01-01"
3838
"""
3939
logging.basicConfig(
4040
level=logging.DEBUG,

electricitymap/contrib/capacity_parsers/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Before opening a PR to update capacity data, you should check the following:
8484

8585
For some zones, we have developed capacity parsers which collect the data automatically.
8686

87-
The update of capacity configurations can be done in the `contrib` repo using `poetry run capacity_update`.
87+
The update of capacity configurations can be done in the `contrib` repo using `uv run capacity_update`.
8888

8989
The `capacity_update` function has the following arguments:
9090
| Argument | Description |
@@ -97,11 +97,11 @@ The `capacity_update` function has the following arguments:
9797
Here is a list of examples:
9898

9999
```{python}
100-
poetry run capacity_update --zone DK-DK1 --target_datetime "2023-01-01 --update_aggregate True"
100+
uv run capacity_update --zone DK-DK1 --target_datetime "2023-01-01 --update_aggregate True"
101101
```
102102

103103
```{python}
104-
poetry run capacity_update --source EIA --target_datetime "2023-06-01"
104+
uv run capacity_update --source EIA --target_datetime "2023-06-01"
105105
```
106106

107107
The following zones can be updated with a parser are listed on our wiki page [Capacity update process](https://github.com/electricitymaps/electricitymaps-contrib/wiki/Capacity-update-process)

parsers.dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
# Run with "$ docker run parsers US-CAL-CISO production"
44
# If needed, pass any token env var using "docker run -e TOKEN=<token>"
55

6-
FROM python:3.10
6+
FROM astral/uv:python3.10-bookworm-slim
77
WORKDIR /workspace
88
RUN apt-get update && apt-get install -y python3-opencv tesseract-ocr tesseract-ocr-jpn tesseract-ocr-eng libgl1
9-
RUN pip install "poetry==2.*"
109
COPY . .
11-
RUN poetry install -E parsers
12-
ENTRYPOINT ["poetry", "run", "test_parser"]
10+
RUN uv sync --frozen --group parsers --group dev --compile-bytecode
11+
ENTRYPOINT ["uv", "run", "test_parser"]

0 commit comments

Comments
 (0)