From 8d80de598a970381281ee5aa596336d1a9f0a24e Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Wed, 8 Mar 2023 16:29:45 +0800 Subject: [PATCH] Add python version search script --- tools/get-best-python-for-docs-build.sh | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 tools/get-best-python-for-docs-build.sh diff --git a/tools/get-best-python-for-docs-build.sh b/tools/get-best-python-for-docs-build.sh new file mode 100755 index 00000000..3bd15be5 --- /dev/null +++ b/tools/get-best-python-for-docs-build.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# This script prints the version suffix of the most suitable version of Python +# for building docs that's installed in this system. +# +# Caller can safely append this suffix to `python` or `pip` and expect the +# binary to exist. +# +# If no usable version of Python is available, this script will exit with a +# non-zero code. + +set -e + +# if `python3` is >= 3.8 and `pip3` is available, use that +if echo -e "import sys\nif sys.version_info < (3,8):\n exit(1)" | python3 && \ +which pip3 1>/dev/null 2>&1; then + echo "3" + 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 + # 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 "$SUFFIX" + exit 0 + fi +done + +# none found +exit 1