Skip to content

Commit 3b06a80

Browse files
Update Makefile for python client with auto setup (#1995)
Automate python client setup and use a virtual env instead to avoid change an end-users' OS python
1 parent 19f44d8 commit 3b06a80

File tree

7 files changed

+212
-124
lines changed

7 files changed

+212
-124
lines changed

.github/workflows/python-client.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ jobs:
5858
with:
5959
python-version: ${{ matrix.python-version }}
6060

61-
- name: Install Poetry
62-
run: |
63-
pip install --user --upgrade -r regtests/requirements.txt
64-
65-
# TODO: add cache for poetry dependencies once we have poetry.lock in the repo
66-
- name: Install dependencies
67-
working-directory: client/python
68-
run: poetry install --all-extras
69-
7061
- name: Lint
7162
working-directory: client/python
7263
run: |
@@ -75,15 +66,14 @@ jobs:
7566
- name: Generated Client Tests
7667
working-directory: client/python
7768
run: |
78-
export SCRIPT_DIR="non-existing-mock-directory"
79-
poetry run pytest test/
69+
make test-client
8070
8171
- name: Image build
8272
run: |
8373
./gradlew \
84-
:polaris-server:assemble \
85-
:polaris-server:quarkusAppPartsBuild --rerun \
86-
-Dquarkus.container-image.build=true
74+
:polaris-server:assemble \
75+
:polaris-server:quarkusAppPartsBuild --rerun \
76+
-Dquarkus.container-image.build=true
8777
8878
- name: Integration Tests
8979
working-directory: client/python

client/python/.pre-commit-config.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ repos:
2323
- id: end-of-file-fixer
2424
- id: debug-statements
2525
- repo: https://github.com/astral-sh/ruff-pre-commit
26-
rev: v0.11.13
26+
rev: v0.12.1
2727
hooks:
28-
- id: ruff
28+
# Run the linter.
29+
- id: ruff-check
2930
args: [ --fix, --exit-non-zero-on-fix ]
31+
# Run the formatter.
3032
- id: ruff-format
3133
- repo: https://github.com/pre-commit/mirrors-mypy
3234
rev: v1.16.0
3335
hooks:
3436
- id: mypy
3537
args:
3638
[--disallow-untyped-defs, --ignore-missing-imports, --install-types, --non-interactive]
39+
files: 'integration_tests/.*\.py'

client/python/Makefile

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,65 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
regenerate-client:
18+
# .SILENT:
19+
20+
# Configures the shell for recipes to use bash, enabling bash commands and ensuring
21+
# that recipes exit on any command failure (including within pipes).
22+
SHELL = /usr/bin/env bash -o pipefail
23+
.SHELLFLAGS = -ec
24+
25+
# Version information
26+
VERSION ?= $(shell cat pyproject.toml | grep version | sed 's/version *= *"\(.*\)"/\1/')
27+
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%S%:z")
28+
GIT_COMMIT := $(shell git rev-parse HEAD)
29+
POETRY_VERSION := $(shell cat pyproject.toml | grep requires-poetry | sed 's/requires-poetry *= *"\(.*\)"/\1/')
30+
31+
# Variables
32+
VENV_DIR := .venv
33+
34+
.PHONY: help
35+
help: ## Display this help.
36+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-30s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
37+
38+
.PHONY: version
39+
version: ## Print version information.
40+
@echo "Apache Polaris version: ${VERSION}"
41+
@echo "Build date: ${BUILD_DATE}"
42+
@echo "Git commit: ${GIT_COMMIT}"
43+
@echo "Poetry version: ${POETRY_VERSION}"
44+
45+
# Target to create the virtual environment directory
46+
$(VENV_DIR):
47+
@echo "Setting up Python virtual environment at $(VENV_DIR)..."
48+
python3 -m venv $(VENV_DIR)
49+
@echo "Virtual environment created."
50+
51+
.PHONY: setup-env
52+
setup-env: $(VENV_DIR) install-poetry-deps
53+
54+
.PHONY: install-poetry-deps
55+
install-poetry-deps:
56+
@echo "Installing Poetry and project dependencies into $(VENV_DIR)..."
57+
# Ensure pip is up-to-date within the venv
58+
$(VENV_DIR)/bin/pip install --upgrade pip
59+
# Install poetry if not already present
60+
@if [ ! -f "$(VENV_DIR)/bin/poetry" ]; then \
61+
$(VENV_DIR)/bin/pip install --upgrade "poetry${POETRY_VERSION}"; \
62+
fi
63+
# Install needed dependencies using poetry
64+
$(VENV_DIR)/bin/poetry install --all-extras
65+
@echo "Poetry and dependencies installed."
66+
67+
.PHONY: regenerate-client
68+
regenerate-client: ## Regenerate the client code
1969
../templates/regenerate.sh
2070

21-
test-integration:
71+
.PHONY: test-client
72+
test-client: setup-env ## Run client tests
73+
SCRIPT_DIR="non-existing-mock-directory" $(VENV_DIR)/bin/poetry run pytest test/
74+
75+
.PHONY: test-integration
76+
test-integration: setup-env ## Run integration tests
2277
docker compose -f docker-compose.yml kill
2378
docker compose -f docker-compose.yml rm -f
2479
docker compose -f docker-compose.yml up -d
@@ -28,8 +83,25 @@ test-integration:
2883
echo "Still waiting for HTTP 200 from /q/health..."; \
2984
done
3085
@echo "Polaris is healthy. Starting integration tests..."
31-
poetry run pytest integration_tests/ ${PYTEST_ARGS}
86+
$(VENV_DIR)/bin/poetry run pytest integration_tests/ ${PYTEST_ARGS}
87+
88+
.PHONY: lint
89+
lint: setup-env ## Run linting checks
90+
$(VENV_DIR)/bin/poetry run pre-commit run --files integration_tests/* cli/*
3291

92+
.PHONY: clean-venv
93+
clean-venv:
94+
@echo "Attempting to remove virtual environment directory: $(VENV_DIR)..."
95+
# SAFETY CHECK: Ensure VENV_DIR is not empty and exists before attempting to remove
96+
@if [ -n "$(VENV_DIR)" ] && [ -d "$(VENV_DIR)" ]; then \
97+
rm -rf "$(VENV_DIR)"; \
98+
echo "Virtual environment removed."; \
99+
else \
100+
echo "Virtual environment directory '$(VENV_DIR)' not found or VENV_DIR is empty. No action taken."; \
101+
fi
33102

34-
lint:
35-
poetry run pre-commit run --files integration_tests/*
103+
.PHONY: clean
104+
clean: clean-venv ## Cleanup
105+
@echo "Cleaning up Python cache files..."
106+
find . -type f -name "*.pyc" -delete
107+
find . -type d -name "__pycache__" -delete

client/python/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
to you under the Apache License, Version 2.0 (the
77
"License"); you may not use this file except in compliance
88
with the License. You may obtain a copy of the License at
9-
9+
1010
http://www.apache.org/licenses/LICENSE-2.0
11-
11+
1212
Unless required by applicable law or agreed to in writing,
1313
software distributed under the License is distributed on an
1414
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,17 +24,13 @@ The Apache Polaris Python package provides a client for interacting with the Apa
2424

2525
### Prerequisites
2626
- Python 3.9 or later
27-
- poetry >= 2.0
27+
- poetry >= 2.1
2828

2929
### Installation
30-
First we need to generate the OpenAPI client code from the OpenAPI specification.
30+
First we need to generate the OpenAPI client code from the OpenAPI specification.
3131
```
3232
make regenerate-client
3333
```
34-
Install the project with test dependencies:
35-
```
36-
poetry install --all-extras
37-
```
3834

3935
### Auto-formatting and Linting
4036
```
@@ -44,4 +40,4 @@ make lint
4440
### Running Integration Tests
4541
```
4642
make test-integration
47-
```
43+
```

0 commit comments

Comments
 (0)