Skip to content

Commit bb2c376

Browse files
hooks: pre-push: Add a python version check, with instructions
1 parent ace39c5 commit bb2c376

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

.hooks/pre-push

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
set -e
44

5+
# Check if the required Python version is available
6+
PYTHON_VERSION=$(grep -oP 'python\s*=\s*"[^0-9]*\K[0-9]+\.[0-9]+' pyproject.toml)
7+
command -v "python$PYTHON_VERSION" >/dev/null 2>&1 || {
8+
if [ "$(command -v pyenv >/dev/null 2>&1)" ]; then
9+
echo "Pyenv is available, switching local Python version to $PYTHON_VERSION"
10+
if [ ! "$(pyenv local "$PYTHON_VERSION")" ]; then
11+
echo "Python $PYTHON_VERSION is not available, installing Python $PYTHON_VERSION"
12+
pyenv install "$PYTHON_VERSION"
13+
14+
echo "Setting local Python version to $PYTHON_VERSION"
15+
pyenv local "$PYTHON_VERSION"
16+
fi
17+
else
18+
cat >&2 <<EOF
19+
Python $PYTHON_VERSION is not available. Aborting.
20+
If your Linux distribution doesn't provide it, consider using Pyenv:
21+
1. Install Pyenv using your Linux distribution package manager
22+
2. Run the following commands:
23+
pyenv install $PYTHON_VERSION
24+
pyenv local $PYTHON_VERSION
25+
For more information about pyenv, please visit https://github.com/pyenv/pyenv#readme
26+
EOF
27+
exit 1
28+
fi
29+
}
30+
531
# Check if poetry is installed
632
command -v poetry >/dev/null 2>&1 || {
733
echo >&2 "poetry is not installed. Aborting. Please install it and try again."

0 commit comments

Comments
 (0)