Files
Spellcast3d/tools/environment.sh

109 lines
5.3 KiB
Bash

# Spellcast3D's environment on Linux. Sourced by the other scripts in this folder (bash), it sets for them:
# ROOT the Spellcast3D folder, absolute
# PY the base Python 3.11 that creates the venvs: tools/python/bin/python3
# PATH with tools/python/bin first, plus a clean Python and pip setup
# On a problem it prints what to do and returns 1.
#
# The Python is a standalone build (github.com/astral-sh/python-build-standalone), downloaded once into tools/python:
# a system's own Python is another version on most distributions. Developer override: SPELLCAST_BASE_PYTHON=/path/to
# a 64-bit CPython 3.11.
PYTHON_RELEASE="20261124"
PYTHON_FILE="cpython-3.11.18+${PYTHON_RELEASE}-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"
PYTHON_SHA256="d873f435e58d396a18858d4f52a335f1abe8cea385cb9d411ec511f958249d54"
PYTHON_URL="https://github.com/astral-sh/python-build-standalone/releases/download/${PYTHON_RELEASE}/${PYTHON_FILE//+/%2B}"
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Keep the user's own Python, Conda and pip settings away from Spellcast3D.
export PYTHONNOUSERSITE=1 PYTHONUTF8=1 PIP_DISABLE_PIP_VERSION_CHECK=1
unset PYTHONPATH PYTHONHOME PYTHONSTARTUP PYTHONUSERBASE VIRTUAL_ENV CONDAPATH PIP_REQUIRE_VIRTUALENV
for name in $(compgen -v CONDA_); do unset "$name"; done
# The tools check their own web servers at startup; a system proxy must not intercept that.
export NO_PROXY=127.0.0.1,localhost no_proxy=127.0.0.1,localhost
export GIT_TERMINAL_PROMPT=0
# git refuses to work in a folder that another user owns (unpacked with sudo, a mounted volume), and then no
# tool's source can be set up or updated. Whoever owns this folder owns every program in it that is started here,
# so the git commands of Spellcast3D and of its tools do not ask who owns a folder (not by this folder's name: a
# folder reached through a link would not match, and an older git knows no "and what is in it"). Nothing is
# written to the user's own git settings.
if [ -z "${GIT_CONFIG_COUNT:-}" ]; then
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0='*'
fi
if [ "$(uname -s)" != "Linux" ] || [ "$(uname -m)" != "x86_64" ]; then
echo
echo "ERROR: These scripts are for Linux on a 64-bit Intel / AMD processor (x86_64);"
echo "this is $(uname -s) on $(uname -m). On Windows use the .bat files."
return 1
fi
if ! command -v git > /dev/null; then
echo
echo "ERROR: git is not installed. Spellcast3D downloads its tools with it."
echo "Install it (Ubuntu / Debian: sudo apt install git) and start again."
return 1
fi
_spellcast_fetch_python() {
local tmp="$ROOT/code/tmp" archive="$ROOT/code/tmp/$PYTHON_FILE"
mkdir -p "$tmp" "$ROOT/tools" || return 1
# A download that is whole already (its unpacking failed: a full disk, Ctrl+C) is not asked for again - a
# server answers the resume of a complete file with an error. One that fails is thrown away, so that the next
# start begins anew instead of resuming a file that cannot be resumed.
if ! echo "$PYTHON_SHA256 $archive.part" | sha256sum --check --status 2> /dev/null; then
echo "Downloading Spellcast3D's own Python (31 MB) ..."
if command -v curl > /dev/null; then
curl --fail --location --retry 3 --continue-at - --output "$archive.part" "$PYTHON_URL" ||
{ rm -f "$archive.part"; return 1; }
elif command -v wget > /dev/null; then
wget --tries=3 --continue --output-document="$archive.part" "$PYTHON_URL" ||
{ rm -f "$archive.part"; return 1; }
else
echo "ERROR: neither curl nor wget is installed (Ubuntu / Debian: sudo apt install curl)."
return 1
fi
fi
if ! echo "$PYTHON_SHA256 $archive.part" | sha256sum --check --status; then
rm -f "$archive.part"
echo "ERROR: the downloaded Python is not the expected file (its SHA-256 differs). Start again to retry."
return 1
fi
# Unpacked in code/tmp first: a download or an unpacking cut off never leaves half a Python in its place.
rm -rf "$tmp/python.unpacking"
mkdir -p "$tmp/python.unpacking" || return 1
tar -xzf "$archive.part" -C "$tmp/python.unpacking" || return 1
rm -rf "$ROOT/tools/python"
mv "$tmp/python.unpacking/python" "$ROOT/tools/python" || return 1
rm -rf "$tmp/python.unpacking" "$archive.part"
}
if [ -n "${SPELLCAST_BASE_PYTHON:-}" ]; then
PY="$SPELLCAST_BASE_PYTHON"
if [ ! -x "$PY" ]; then
echo
echo "ERROR: SPELLCAST_BASE_PYTHON points to a file that cannot be run:"
echo " $PY"
echo "Set it to a 64-bit Python 3.11, or remove it to use tools/python."
return 1
fi
else
PY="$ROOT/tools/python/bin/python3"
if [ ! -x "$PY" ] && ! _spellcast_fetch_python; then
echo
echo "ERROR: Spellcast3D's own Python could not be put into"
echo " $ROOT/tools/python"
echo "Check your internet connection and that the folder can be written, then start again."
return 1
fi
export PATH="$ROOT/tools/python/bin:$PATH"
fi
if ! "$PY" -c "import ssl, venv" > /dev/null 2>&1; then
echo
echo "ERROR: Spellcast3D's Python does not start correctly:"
echo " $PY"
echo "Delete the folder $ROOT/tools/python and start again: it is downloaded anew."
return 1
fi
return 0