diff --git a/Makefile b/Makefile index 10624ed5..097e5f16 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: @@ -111,25 +115,13 @@ build-deps-ubuntu: docs: docs-deps docs-build docs-build: .PHONY - @if ! /bin/echo -e "import sys\nif sys.version_info < (3,8):\n exit(1)" | python3; then \ - if which python3.8; then \ - echo "python3.8 $(shell which mkdocs) build"; \ - python3.8 $(shell which mkdocs) build; \ - else \ - echo "ERROR: Python version too low. mkdocs-material needs >= 3.8"; \ - exit 1; \ - fi; \ - else \ - echo "mkdocs build"; \ - mkdocs build; \ - fi + PY=$$(which $(PY_BIN)) && MKDOCS=$$(which mkdocs) && $$PY $$MKDOCS build docs-deps: .PHONY - pip3 install -r requirements.txt + PIP=$$(which $(PIP_BIN)) && $$PIP install -r requirements.txt docs-deps-update: .PHONY - pip3 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 new file mode 100755 index 00000000..8c0eb857 --- /dev/null +++ b/tools/get-python-bin.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +# Synopsis: +# - get-python-bin.sh python +# - get-python-bin.sh pip +# +# This script selects the most suitable `python` / `pip` binary available +# for building docs that's installed in this system. +# +# If no usable version of Python is available, this script will exit with a +# non-zero code. + +set -e + +case "$1" in + "python" | "pip") + BIN_PREFIX="$1" + ;; + *) + echo "Incorrect usage" >&2 + exit 1 + ;; +esac + +# 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 "${BIN_PREFIX}3" + exit 0 +fi + +# 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 "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 + +# none found +echo "No suitable version of Python found" >&2 +exit 2