mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-24 17:41:03 +01:00
- Refactored the Python installation script to support multiple commands: install, uninstall, switch, and list. - Added functions for usage printing, dependency installation, pyenv setup, Python version verification, symlink creation, and cleanup. - Improved error handling for version checks and command execution, enhancing usability and flexibility in Docker environments. - Default Python version is set to 3.12, with the option to specify a different version during installation.
134 lines
2.7 KiB
Bash
134 lines
2.7 KiB
Bash
#!/bin/bash
|
|
|
|
# Fail on error
|
|
set -e
|
|
|
|
# Function to print usage
|
|
print_usage() {
|
|
echo "Usage: $0 <command> [version]"
|
|
echo "Commands:"
|
|
echo " install <version> - Install Python version (default: 3.12)"
|
|
echo " uninstall <version> - Uninstall Python version"
|
|
echo " switch <version> - Switch to a different Python version"
|
|
echo " list - List installed Python versions"
|
|
}
|
|
|
|
# Function to install Python dependencies
|
|
install_dependencies() {
|
|
apt-get install -y \
|
|
make \
|
|
build-essential \
|
|
libssl-dev \
|
|
zlib1g-dev \
|
|
libbz2-dev \
|
|
libreadline-dev \
|
|
libsqlite3-dev \
|
|
wget \
|
|
curl \
|
|
llvm \
|
|
libncursesw5-dev \
|
|
xz-utils \
|
|
tk-dev \
|
|
libxml2-dev \
|
|
libxmlsec1-dev \
|
|
libffi-dev \
|
|
liblzma-dev
|
|
}
|
|
|
|
# Function to setup pyenv
|
|
setup_pyenv() {
|
|
# Install pyenv if not already installed
|
|
if [ ! -d "$HOME/.pyenv" ]; then
|
|
curl https://pyenv.run | bash
|
|
|
|
# Create a file in $HOME/.pyenv-env.sh
|
|
cat > $HOME/.pyenv-env.sh << 'EOF'
|
|
export PYENV_ROOT="$HOME/.pyenv"
|
|
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
|
|
eval "$(pyenv init -)"
|
|
eval "$(pyenv virtualenv-init -)"
|
|
EOF
|
|
chmod +x $HOME/.pyenv-env.sh
|
|
fi
|
|
|
|
source $HOME/.pyenv-env.sh
|
|
}
|
|
|
|
# Function to verify Python installation
|
|
verify_python() {
|
|
local version=$1
|
|
python_version=$(python -V)
|
|
if [[ ! $python_version =~ "Python ${version}" ]]; then
|
|
echo "ERROR: python version does not match. expect \"Python ${version}\", but actual is \"${python_version}\""
|
|
return 1
|
|
fi
|
|
|
|
pip_version=$(pip -V)
|
|
if [[ ! $pip_version =~ "python ${version}" ]]; then
|
|
echo "ERROR: pip version does not match. expected: \"python ${version}\", but actual is \"${pip_version}\""
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# Function to create symlinks
|
|
create_symlinks() {
|
|
ln -sf $(pyenv which python) /usr/local/bin/python
|
|
ln -sf $(pyenv which python3) /usr/local/bin/python3
|
|
ln -sf $(pyenv which pip) /usr/local/bin/pip
|
|
}
|
|
|
|
# Function to cleanup
|
|
cleanup() {
|
|
pip cache purge
|
|
rm -rf ~/.cache/pip/*
|
|
apt-get remove -y make build-essential
|
|
apt-get autoremove -y
|
|
}
|
|
|
|
# Main logic
|
|
command="${1:-install}"
|
|
version="${2:-3.12}"
|
|
|
|
case $command in
|
|
"install")
|
|
install_dependencies
|
|
setup_pyenv
|
|
pyenv install $version
|
|
pyenv global $version
|
|
verify_python $version
|
|
pip install -r /app/install/python/requirements.txt
|
|
create_symlinks
|
|
cleanup
|
|
;;
|
|
|
|
"uninstall")
|
|
if [ -z "$version" ]; then
|
|
echo "Please specify a version to uninstall"
|
|
exit 1
|
|
fi
|
|
setup_pyenv
|
|
pyenv uninstall -f $version
|
|
;;
|
|
|
|
"switch")
|
|
if [ -z "$version" ]; then
|
|
echo "Please specify a version to switch to"
|
|
exit 1
|
|
fi
|
|
setup_pyenv
|
|
pyenv global $version
|
|
verify_python $version
|
|
create_symlinks
|
|
;;
|
|
|
|
"list")
|
|
setup_pyenv
|
|
pyenv versions
|
|
;;
|
|
|
|
*)
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
esac |