Skip to content

Commit a4927fe

Browse files
authored
Improve python tooling, CI and testing (evcc-io#26)
1 parent 6800f48 commit a4927fe

15 files changed

Lines changed: 786 additions & 73 deletions

File tree

.dockerignore

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# flyctl launch added from .gitignore
2-
**/.claude
3-
**/.vscode
4-
**/.git
1+
# Ignore all files
2+
*
53

6-
# flyctl launch added from .venv/.gitignore
7-
# Created by venv; see https://docs.python.org/3/library/venv.html
8-
.venv/**/*
9-
fly.toml
4+
# Except what we really need
5+
!uv.lock
6+
!pyproject.toml
7+
!README.md
8+
!src/
9+
10+
# Exclude files in the included directories that we don't want
11+
__pycache__

.flake8

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Test
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v5
16+
- name: Install uv
17+
uses: astral-sh/setup-uv@v6
18+
with:
19+
enable-cache: true
20+
- name: Format
21+
run: uv run autopep8 --exit-code --diff .
22+
- name: Lint
23+
run: uv run ruff check
24+
- name: Test
25+
run: uv run pytest
26+
27+
docker:
28+
name: Docker
29+
runs-on: ubuntu-latest
30+
needs: test
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v5
34+
- name: Build
35+
uses: docker/build-push-action@v6
36+
with:
37+
push: false
38+
# To be changed
39+
tags: andig/evopt:latest

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.claude/
2-
.vscode/
2+
.vscode/
3+
.venv/
4+
__pycache__/

.pylintrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

Dockerfile

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
FROM python:3.13-slim
1+
# Install uv
2+
FROM python:3.13-slim AS builder
3+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
24

5+
# Change the working directory to the `app` directory
36
WORKDIR /app
47

5-
# Install system dependencies including CBC solver for PuLP
6-
RUN apt-get update && apt-get install -y \
7-
gcc \
8-
g++ \
9-
coinor-cbc \
10-
htop \
11-
&& rm -rf /var/lib/apt/lists/*
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 --locked --no-install-project --no-editable --no-group dev
13+
14+
# Copy the project into the intermediate image
15+
ADD . /app
1216

13-
# Copy requirements and install Python dependencies
14-
COPY requirements.txt .
15-
RUN pip install --upgrade --no-cache-dir -r requirements.txt
17+
# Sync the project
18+
RUN --mount=type=cache,target=/root/.cache/uv \
19+
uv sync --locked --no-editable --no-group dev
1620

17-
# Copy application code
18-
COPY *.py .
21+
FROM python:3.13-slim
1922

20-
# Expose the port
21-
EXPOSE 7050
23+
# Copy the environment, but not the source code
24+
COPY --from=builder --chown=app:app /app/.venv /app/.venv
2225

2326
# Run the application
24-
CMD ["gunicorn", "--bind", "0.0.0.0:7050", "--workers", "4", "--max-requests", "32", "app:app"]
27+
CMD ["/app/.venv/bin/gunicorn", "--bind", "0.0.0.0:7050", "--workers", "4", "--max-requests", "32", "evopt.app:app"]

Makefile

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@ default: build docker-build
55
build::
66
go generate ./...
77

8-
test::
9-
go run example/client.go
10-
118
install::
12-
python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade -r requirements.txt
9+
uv sync
1310

1411
upgrade::
15-
python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade $$(pip list --outdated | awk 'NR>2 {print $$1}')
12+
uv lock --upgrade
13+
14+
lint::
15+
uv run autopep8 . --in-place
16+
uv run ruff check --fix
17+
18+
test::
19+
uv run pytest
1620

1721
run::
18-
source .venv/bin/activate && python3 app.py
22+
uv run python -m evopt.app
1923

20-
docker: docker-build docker-run
24+
docker: docker-build docker-push docker-run
2125

2226
docker-build::
23-
docker buildx build . --tag $(DOCKER_IMAGE) --push
27+
docker buildx build . --tag $(DOCKER_IMAGE)
2428

2529
docker-run::
2630
docker run -p 7050:7050 -it $(DOCKER_IMAGE)
2731

32+
docker-push::
33+
docker push $(DOCKER_IMAGE)
34+
2835
fly::
2936
fly deploy --local-only

pyproject.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[project]
2+
name = "evopt"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
authors = [{ name = "andig", email = "andi@evcc.io" }, { name = "ekke" }]
7+
requires-python = "~=3.13.0"
8+
dependencies = [
9+
"flask>=3.0.3",
10+
"flask-restx>=1.3.0",
11+
"gunicorn>=22.0.0",
12+
"numpy>=2.3.1",
13+
"pulp>=3.2.2",
14+
"pyjwt>=2.8.0",
15+
"werkzeug>=3.0.3",
16+
]
17+
18+
[project.scripts]
19+
evopt = "evopt:main"
20+
21+
[build-system]
22+
requires = ["hatchling"]
23+
build-backend = "hatchling.build"
24+
25+
[dependency-groups]
26+
dev = [
27+
"autopep8>=2.3.2",
28+
"pytest>=8.4.2",
29+
"ruff>=0.12.12",
30+
]
31+
32+
[tool.autopep8]
33+
max_line_length = 160
34+
recursive = true
35+
36+
[tool.ruff]
37+
line-length = 160
38+
39+
[tool.ruff.lint]
40+
select = ["F", "I", "E", "W", "PL"]
41+
ignore = ["PLR0912", "PLR0913", "PLR2004", "PLR0917", "PLR6301"]

requirements.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)