From 008ff709c820e50cb45c85ab7da52f665b819484 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Wed, 12 Jul 2023 18:44:04 +0800 Subject: [PATCH] Improve python binary selection logic - Check all available versions of python3 instead of just the shortlist - Allow manually overriding using make variables --- Makefile | 13 +++++++------ tools/get-python-bin.sh | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 77b5c5a3..548c23ee 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ MAKEFLAGS := --jobs=1 + VERSION := $(shell git describe --tag) COMMIT := $(shell git rev-parse --short HEAD) +PY_BIN := $(shell tools/get-python-bin.sh python) +PIP_BIN := $(shell tools/get-python-bin.sh pip) + .PHONY: help: @@ -96,16 +100,13 @@ build-deps-ubuntu: docs: docs-deps docs-build docs-build: .PHONY - PY=$$(tools/get-python-bin.sh python) && MKDOCS=$$(which mkdocs) && \ - $$PY $$MKDOCS build + PY=$$(which $(PY_BIN)) && MKDOCS=$$(which mkdocs) && $$PY $$MKDOCS build docs-deps: .PHONY - PIP=$$(tools/get-python-bin.sh pip) && \ - $$PIP install -r requirements.txt + PIP=$$(which $(PIP_BIN)) && $$PIP install -r requirements.txt docs-deps-update: .PHONY - PIP=$$(tools/get-python-bin.sh pip) && \ - $$PIP install -r requirements.txt --upgrade + PIP=$$(which $(PIP_BIN)) && $$PIP install -r requirements.txt --upgrade # Web app diff --git a/tools/get-python-bin.sh b/tools/get-python-bin.sh index 13cce1e0..8c0eb857 100755 --- a/tools/get-python-bin.sh +++ b/tools/get-python-bin.sh @@ -29,13 +29,14 @@ which pip3 1>/dev/null 2>&1; then exit 0 fi -# check `python3.N` from newest to oldest -CANDIDATE_SUFFIXES=("3.11" "3.10" "3.9" "3.8") -for SUFFIX in ${CANDIDATE_SUFFIXES[@]}; do +# list all available `python3.N`, then use the newest that passes checks +# compgen is bash-specific, but we asked for bash in shebang so it's fine +MINOR_VERSION_CANDIDATES=$(compgen -c | grep -P '^python3\.[0-9]+$' | sed 's/python3\.//' | awk 'int($NF) >= 8' | sort -nr) +for MINOR in ${MINOR_VERSION_CANDIDATES[@]}; do # if both `python3.N` and `pip3.N` are available, use that - if which "python$SUFFIX" 1>/dev/null 2>&1 && \ - which "pip$SUFFIX" 1>/dev/null 2>&1; then - echo "${BIN_PREFIX}${SUFFIX}" + if which "python3.${MINOR}" 1>/dev/null 2>&1 && \ + which "pip3.${MINOR}" 1>/dev/null 2>&1; then + echo "${BIN_PREFIX}3.${MINOR}" exit 0 fi done