File size: 4,185 Bytes
fadfe74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash

# 参数:
python_version="3.6.5";
system_version="centos";


# parse options
while true; do
  [ -z "${1:-}" ] && break;  # break if there are no arguments
  case "$1" in
    --*) name=$(echo "$1" | sed s/^--// | sed s/-/_/g);
      eval '[ -z "${'"$name"'+xxx}" ]' && echo "$0: invalid option $1" 1>&2 && exit 1;
      old_value="(eval echo \\$$name)";
      if [ "${old_value}" == "true" ] || [ "${old_value}" == "false" ]; then
        was_bool=true;
      else
        was_bool=false;
      fi

      # Set the variable to the right value-- the escaped quotes make it work if
      # the option had spaces, like --cmd "queue.pl -sync y"
      eval "${name}=\"$2\"";

      # Check that Boolean-valued arguments are really Boolean.
      if $was_bool && [[ "$2" != "true" && "$2" != "false" ]]; then
        echo "$0: expected \"true\" or \"false\": $1 $2" 1>&2
        exit 1;
      fi
      shift 2;
      ;;

    *) break;
  esac
done

echo "python_version: ${python_version}";
echo "system_version: ${system_version}";


if [ ${system_version} = "centos" ]; then
  # 安装 python 开发编译环境
  yum -y groupinstall "Development tools"
  yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  yum install libffi-devel -y
  yum install -y wget
  yum install -y make

  mkdir -p /data/dep
  cd /data/dep || exit 1;
  if [ ! -e Python-${python_version}.tgz ]; then
    wget -P /data/dep https://www.python.org/ftp/python/${python_version}/Python-${python_version}.tgz
  fi

  cd /data/dep || exit 1;
  if [ ! -d Python-${python_version} ]; then
    tar -zxvf Python-${python_version}.tgz
    cd /data/dep/Python-${python_version} || exit 1;
  fi

  mkdir /usr/local/python-${python_version}
  ./configure --prefix=/usr/local/python-${python_version}
  make && make install

  /usr/local/python-${python_version}/bin/python3 -V
  /usr/local/python-${python_version}/bin/pip3 -V

  rm -rf /usr/local/bin/python3
  rm -rf /usr/local/bin/pip3
  ln -s /usr/local/python-${python_version}/bin/python3 /usr/local/bin/python3
  ln -s /usr/local/python-${python_version}/bin/pip3 /usr/local/bin/pip3

  python3 -V
  pip3 -V

elif [ ${system_version} = "ubuntu" ]; then
  # 安装 python 开发编译环境
  # https://zhuanlan.zhihu.com/p/506491209

  # 刷新软件包目录
  sudo apt update
  # 列出当前可用的更新
  sudo apt list --upgradable
  # 如上一步提示有可以更新的项目,则执行更新
  sudo apt -y upgrade
  # 安装 GCC 编译器
  sudo apt install gcc
  # 检查安装是否成功
  gcc -v

  # 安装依赖
  sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libbz2-dev liblzma-dev sqlite3 libsqlite3-dev tk-dev uuid-dev libgdbm-compat-dev

  mkdir -p /data/dep
  cd /data/dep || exit 1;
  if [ ! -e Python-${python_version}.tgz ]; then
    # sudo wget -P /data/dep https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
    sudo wget -P /data/dep https://www.python.org/ftp/python/${python_version}/Python-${python_version}.tgz
  fi

  cd /data/dep || exit 1;
  if [ ! -d Python-${python_version} ]; then
    # tar -zxvf Python-3.6.5.tgz
    tar -zxvf Python-${python_version}.tgz
    # cd /data/dep/Python-3.6.5
    cd /data/dep/Python-${python_version} || exit 1;
  fi

  # mkdir /usr/local/python-3.6.5
  mkdir /usr/local/python-${python_version}

  # 检查依赖与配置编译
  # sudo ./configure --prefix=/usr/local/python-3.6.5 --enable-optimizations --with-lto --enable-shared
  sudo ./configure --prefix=/usr/local/python-${python_version} --enable-optimizations --with-lto --enable-shared
  cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
  # sudo make -j 4
  sudo make -j "${cpu_count}"

  /usr/local/python-${python_version}/bin/python3 -V
  /usr/local/python-${python_version}/bin/pip3 -V

  rm -rf /usr/local/bin/python3
  rm -rf /usr/local/bin/pip3
  ln -s /usr/local/python-${python_version}/bin/python3 /usr/local/bin/python3
  ln -s /usr/local/python-${python_version}/bin/pip3 /usr/local/bin/pip3

  python3 -V
  pip3 -V
fi