#!/bin/bash
# Centos7.9.2009 install python3.8.16 auto install script
# datatime: 2023.8.24 13:00:00  小日本将核废水排放到大海的日子。
# useradd python
# echo "123456"|passwd --stdin python
# visudo ----> sudo su - python 切换到python用户;

# install some python env depend on packages:
mkdir -p /home/python/offline
cd /home/python/offline
sudo yum install -y wget yum-utils net-tools vim lrzsz ansible --downloadonly --downloaddir=/home/python/offline
sudo yum -y groupinstall "Development tools" --downloadonly --downloaddir=/home/python/offline
sudo yum install -y zlib zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel --downloadonly --downloaddir=/home/python/offline
sudo yum -y localinstall *.rpm
cd

# update openssl
# centos7.9 默认openssl版本:OpenSSL 1.0.2k-fips  26 Jan 2017
# centos解决 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 
# 下载不下来可以提前下载下来然后上传到服务器上
# wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
sudo su - root
tar -zxf openssl-OpenSSL_1_1_1d.tar.gz
cd openssl-OpenSSL_1_1_1d
./config --prefix=/usr/local/openssl && echo $?
make && make install && echo $?
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/lib64/openssl /usr/lib64/openssl.old
mv /usr/lib64/libssl.so /usr/lib64/libssl.so.old
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so /usr/lib64/libssl.so
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v
openssl version
#OpenSSL 1.1.1d  10 Sep 2019

# install_python2.7.18
sudo mv /usr/bin/python /usr/bin/python.bak
sudo mv /usr/bin/python2 /usr/bin/python2.bak
sudo mkdir -p /home/python/backupfiles
sudo cp -fr /usr/bin/yum /home/python/backupfiles
sudo sed -i 's/#!\/usr\/bin\/python/#!\/usr\/bin\/python2.7/g' /usr/bin/yum
sudo head -n1 /usr/bin/yum
sudo cp -fr /usr/libexec/urlgrabber-ext-down /home/python/backupfiles
sudo sed -i 's/#! \/usr\/bin\/python/#! \/usr\/bin\/python2.7/g' /usr/libexec/urlgrabber-ext-down
sudo head -n1 /usr/libexec/urlgrabber-ext-down
sudo cp -fr /usr/bin/yum-config-manager /home/python/backupfiles
sudo sed -i 's/#!\/usr\/bin\/python/#!\/usr\/bin\/python2.7/g' /usr/bin/yum-config-manager
sudo head -n1 /usr/bin/yum-config-manager
sudo cp -fr /usr/bin/ansible /home/python/backupfiles
sudo sed -i 's/#!\/usr\/bin\/python2/#!\/usr\/bin\/python2.7/g' /usr/bin/ansible
sudo head -n1 /usr/bin/ansible

# rz 提前上传python-packages.tar.gz 到 /home/python,并解压;
# tar -zxf python-packages.tar.gz
cd /home/python/python-packages
# tar -zxf Python-2.7.18.tgz
cd Python-2.7.18
#解决 ModuleNotFoundError: No module named '_ssl' 问题
vim ./Modules/Setup
#修改此处,去掉注释:
#SSL=/usr/local/openssl
#_ssl _ssl.c \
#   -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#   -L$(SSL)/lib -lssl -lcrypto
#参考:https://blog.csdn.net/tyhj_sf/article/details/122876807
./configure --prefix=/home/python/python2 --with-openssl=/usr/local/openssl && make && make install && echo $?
sudo ln -s /home/python/python2/bin/python /usr/bin/
sudo ln -s /home/python/python2/bin/python2 /usr/local/sbin/python2

cd setuptools-36.0.1
/home/python/python2/bin/python setup.py install
cd ../pip-9.0.1
/home/python/python2/bin/python setup.py install
sudo ln -s /home/python/python2/bin/pip /usr/bin/
sudo ln -s /home/python/python2/bin/pip2 /usr/local/sbin/pip2
pip -V
pip2 -V
python -V
python2 -V
#pip2 install --upgrade pip
#python仅下载不安装
# python -m pip download -d /home/packs  setuptools==33.1.1 #下载setuptools==33.1.1模块,在/home/packs目录下
#mkdir -p /home/python/pip-packages
#python -m pip download -d /home/python/pip-packages setuptools
#python -m pip download -d /home/python/pip-packages setuptools -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

# install python2 vm env
sudo pip install --upgrade pip -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com 
sudo pip install --upgrade setuptools -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
sudo pip install virtualenv -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 
sudo pip install pbr -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com 
sudo pip install virtualenvwrapper -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 
#[python@python ~]$ find /home/python -name "virtualenv"
#/home/python/python2/lib/python2.7/site-packages/virtualenv
#/home/python/python2/bin/virtualenv
#[python@python ~]$ find /home/python -name "virtualenvwrapper.sh"
#/home/python/python2/bin/virtualenvwrapper.sh
#pip list
#virtualenv          20.15.1
#virtualenv-clone    0.5.7
#virtualenvwrapper   4.8.4

# install_python3.8.16
tar -zxf Python-3.8.16.tgz
cd Python-3.8.16
./configure --prefix=/home/python/python3 --with-openssl=/usr/local/openssl && echo $?
# 指定编译文件的存放目录(安装目录)
# --prefix=/home/programs/python/Python-3.8.16
# 待openssl包编译,否则pip install组件的时候,会无法下载https的组件
# --with-openssl=/usr/local/openssl
make && make install && echo $?
sudo ln -s /home/python/python3/bin/python3 /usr/bin/python3 
sudo ln -s /home/python/python3/bin/pip3 /usr/bin/pip3
python3 -V
pip3 -V
#/home/python/python3/bin/python3 -m pip install --upgrade pip
echo "python3环境安装完成!"

# install_python3_vm_env
pip3 install --upgrade pip -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com 
pip3 install --upgrade setuptools -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com 
pip3 install virtualenv -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 
pip3 install pbr -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com 
#pip3 install virtualenvwrapper -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
pip3 install virtualenvwrapper==4.8.2 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
find /home/python -name "virtualenv"
find /home/python -name "virtualenvwrapper.sh"
sudo ln -s /home/python/python3/bin/virtualenv /usr/local/bin/

#pip3 list
#virtualenv        20.24.3
#virtualenv-clone  0.5.7
#virtualenvwrapper 4.4.0

#set_bashrc_env
#cat>>~/.bashrc <<EOF
#if [ \$(id -u) != '' ]; then
#    export VIRTUALENV_USE_DISTRIBUTE=1                 # 总是使用 pip/distribute  
#    export WORKON_HOME=\$HOME/.virtualenvs             # 所有虚拟环境存储的目录
#    if [ -e /home/python/python3/bin/virtualenvwrapper.sh ];then
#        source /home/python/python3/bin/virtualenvwrapper.sh
#    fi
#    export PIP_VIRTUALENV_BASE=\$WORKON_HOME
#    export PIP_RESPECT_VIRTUALENV=true
#fi
#EOF
#Or
#export VIRTUALENV_USE_DISTRIBUTE=1
#export PIP_VIRTUALENV_BASE=~/.virtualenvs
#export PIP_RESPECT_VIRTUALENV=true
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/home/python/python2/bin/virtualenv
source /home/python/python2/bin/virtualenvwrapper.sh

source .bashrc

#使用mkvirtualenv创建虚拟环境
#创建python2的虚拟环境
#mkvirtualenv -p python2 py2_env
#创建python3的虚拟环境
#mkvirtualenv -p python3 py3_env

#创建python2的虚拟环境
cd $HOME/.virtualenvs
/home/python/python2/bin/virtualenv python2_env

#创建python3的虚拟环境
cd $HOME/.virtualenvs
/home/python/python3/bin/virtualenv python3_env

#通过workon命令去查看
workon
#通过workon命令切换两个虚拟环境
workon py3_env
# 退出python虚拟环境
deactivate 
workon py2_env
# 删除python虚拟环境
mkvirtualenv -p python3 test
workon|grep test
deactivate
rmvirtualenv test
workon|grep test