|
1 | 1 | POETRY_HOME := $(CURDIR)/.poetry
|
2 | 2 | POETRY := $(POETRY_HOME)/bin/poetry
|
3 | 3 |
|
| 4 | +MIN_PYTHON_VERSION := 3.10 |
| 5 | + |
| 6 | +PYTHON_VERSIONS := 3.11 3.10 |
| 7 | + |
| 8 | +PYTHON := $(shell \ |
| 9 | + for ver in $(PYTHON_VERSIONS); do \ |
| 10 | + if command -v python$$ver >/dev/null 2>&1; then echo python$$ver; exit 0; fi; \ |
| 11 | + done \ |
| 12 | +) |
| 13 | + |
| 14 | +ifndef PYTHON |
| 15 | +$(error "Python version $(MIN_PYTHON_VERSION) or higher is required but not found.") |
| 16 | +endif |
| 17 | + |
4 | 18 | .PHONY: pre-install
|
5 | 19 | pre-install:
|
| 20 | + @echo "🐍 Using Python interpreter: $(PYTHON)" |
6 | 21 | @echo "🐍 Checking if Python is installed"
|
7 |
| - @command -v python3 >/dev/null 2>&1 || { echo >&2 "Python is not installed. Aborting."; exit 1; } |
| 22 | + @command -v $(PYTHON) >/dev/null 2>&1 || { echo >&2 "$(PYTHON) is not installed. Aborting."; exit 1; } |
8 | 23 | @echo "🐍 Checking Python version"
|
9 |
| - @python3 --version | grep -E "Python 3\.(10|[1-9][1-9])" >/dev/null 2>&1 || { echo >&2 "Python version 3.10 or higher is required. Aborting."; exit 1; } |
| 24 | + @$(PYTHON) --version | grep -E "Python 3\.(1[0-9]|[2-9][0-9])" >/dev/null 2>&1 || { echo >&2 "Python $(MIN_PYTHON_VERSION) or higher is required. Aborting."; exit 1; } |
10 | 25 | @echo "📦 Checking if Poetry is installed"
|
11 | 26 | @if ! command -v poetry >/dev/null 2>&1 || [ ! -d "$(POETRY_HOME)" ]; then \
|
12 |
| - echo "Poetry is not installed or POETRY_HOME does not exist. Installing Poetry."; \ |
13 |
| - curl -sSL https://install.python-poetry.org | POETRY_HOME=$(POETRY_HOME) python3 -; \ |
14 |
| - fi |
| 27 | + echo "Poetry is not installed or POETRY_HOME does not exist. Installing Poetry."; \ |
| 28 | + curl -sSL https://install.python-poetry.org | POETRY_HOME=$(POETRY_HOME) $(PYTHON) -; \ |
| 29 | + fi |
| 30 | + @echo "📦 Configuring Poetry" |
| 31 | + @if [ -z "$$CONDA_PREFIX" ] && [ -z "$$VIRTUAL_ENV" ]; then \ |
| 32 | + echo "Configuring Poetry to create a virtual environment."; \ |
| 33 | + $(POETRY) config virtualenvs.in-project true; \ |
| 34 | + else \ |
| 35 | + echo "Configuring Poetry to use the existing environment."; \ |
| 36 | + $(POETRY) config virtualenvs.create false; \ |
| 37 | + fi |
| 38 | + @echo "📦 Setting Poetry to use $(PYTHON)" |
| 39 | + @$(POETRY) env use $(PYTHON) || { echo "Failed to set Python version for Poetry. Aborting."; exit 1; } |
15 | 40 |
|
16 | 41 | .PHONY: install
|
17 | 42 | install: pre-install ## Install the poetry environment and install the pre-commit hooks
|
18 | 43 | @echo "📦 Installing dependencies with Poetry"
|
19 | 44 | @PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring $(POETRY) install --with core
|
20 | 45 | @echo "🔧 Installing pre-commit hooks"
|
21 | 46 | @$(POETRY) run pre-commit install
|
22 |
| - @echo "🐚 Activating virtual environment" |
23 |
| - @$(POETRY) shell |
| 47 | + @$(MAKE) shell |
24 | 48 |
|
25 | 49 | .PHONY: full-install
|
26 | 50 | full-install: pre-install ## Install the poetry environment and install the pre-commit hooks
|
27 | 51 | @echo "📦 Installing dependencies with Poetry"
|
28 | 52 | @PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring $(POETRY) install --with core,docs,dev
|
29 | 53 | @echo "🔧 Installing pre-commit hooks"
|
30 | 54 | @$(POETRY) run pre-commit install
|
31 |
| - @echo "🐚 Activating virtual environment" |
32 |
| - @$(POETRY) shell |
| 55 | + @$(MAKE) shell |
33 | 56 |
|
34 | 57 | .PHONY: shell
|
35 | 58 | shell: ## Start a shell in the poetry environment
|
36 |
| - @echo "🐚 Activating virtual environment" |
37 |
| - @$(POETRY) shell |
| 59 | + @if [ -z "$$CONDA_PREFIX" ] && [ -z "$$VIRTUAL_ENV" ]; then \ |
| 60 | + echo "🐚 Activating virtual environment"; \ |
| 61 | + $(POETRY) shell; \ |
| 62 | + else \ |
| 63 | + echo "🐚 Conda or virtual environment detected, skipping Poetry shell activation"; \ |
| 64 | + fi |
38 | 65 |
|
39 | 66 | .PHONY: sync
|
40 | 67 | sync: ## Sync the lock file
|
|
0 commit comments