Skip to content

Instantly share code, notes, and snippets.

@xiaojueguan
Last active January 2, 2024 02:49
Show Gist options
  • Save xiaojueguan/14fa256c31fcaee8012eafac2cbcb9d2 to your computer and use it in GitHub Desktop.
Save xiaojueguan/14fa256c31fcaee8012eafac2cbcb9d2 to your computer and use it in GitHub Desktop.
#!/bin/bash
pmt='apt'
pkgs=('python3-pip')
# Check the distribution
if [[ -f /etc/os-release ]]; then
source /etc/os-release
if [[ -n "$PRETTY_NAME" ]]; then
echo "Distribution: $PRETTY_NAME"
# Check specific distributions
if [[ "$ID" == "ubuntu" ]]; then
echo "Perform actions for Ubuntu"
pmt='apt'
# Actions specific to Ubuntu
elif [[ "$ID" == "centos" ]]; then
echo "Perform actions for CentOS"
pmt='yum'
# Actions specific to CentOS
elif [[ "$ID" == "fedora" ]]; then
echo "Perform actions for Fedora"
pmt='dnf'
# Actions specific to Fedora
else
echo "Perform default actions for other Linux distributions"
# Default actions for other Linux distributions
fi
fi
else
exit 1
fi
for pkg in "${pkgs[@]}"; do
$pmt install -y $pkg
done
# List of servers
servers=("mirrors.aliyun.com" "pypi.org")
# Initialize minimum latency as a very large number
min_latency=1000000
min_server=""
# Loop through all servers
for server in ${servers[@]}; do
# Ping the server and get the average latency
latency=$(ping -c 4 $server | tail -1| awk '{print $4}' | cut -d '/' -f 2)
# Check if the latency is less than the minimum latency
if (( $(echo "$latency < $min_latency" | bc -l) )); then
min_latency=$latency
min_server=$server
fi
done
# Print the server with the minimum latency
echo "The server with the minimum latency is $min_server with a latency of $min_latency ms"
if [ min_server == "pypi.org" ]; then
index_url="https://${min_server}/simple/"
else
index_url="https://${min_server}/pypi/simple/"
fi
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
trusted-host=${min_server}
index-url=${index_url}
EOF
pip_pkgs=('virtualenv', 'virtualenvwrapper')
for pkg in "${pip_kgs[@]}"; do
pip install $pkg
done
cat <<EOF>> ~/.bashrc
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source virtualenvwrapper.sh
EOF
source ~/.bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment