-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython_installer.sh
More file actions
executable file
·214 lines (182 loc) · 6.17 KB
/
python_installer.sh
File metadata and controls
executable file
·214 lines (182 loc) · 6.17 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#
# Python Installer
#
# Copyright (C) 2013 Harrison Feng <feng.harrison@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
#
#
# Python Installer is a BASH script used to install Python from source in Ubuntu
# box. Since this script will go to download official Python source package,
# please make sure your network is alive. It requires wget utility is installed
# in your Ubuntu box. Actually, wget should be default installed in Ubuntu box.
#
#
# @author Harrison Feng <feng.harrison@gmail.com>
# @file python_installer.sh
PYTHON_INSTALLER_VERSION=0.1.4
# Color Definition
TXTRESET='\e[0m'
TXTRED='\e[0;31m'
TXTGREEN='\e[0;32m'
TXTBLUE='\e[0;34m'
TXTPURPLE='\e[0;35m'
BTXTRED='\e[1;31m'
BTXTGREEN='\e[1;32m'
BTXTBLUE='\e[1;34m'
# Args
PYTHON_VERSION=$1
INSTALL_DIR_PREFIX=$2
# Error code
NO_ARGS=5
ERRORS=444
FILE_NOT_EXISTS=505
ERROR_DOWNLOAD=404
LATEST_PYTHON_VERSIONS_STABLE='3.13.5'
PYTHON_SRC_URL_PREFIX='http://www.python.org/ftp/python'
# If the user doesn't give it, we will use it.
DEFAULT_INSTALL_DIR_PREFIX=/opt/python
PYTHON_VERSION_MAJOR=`echo ${PYTHON_VERSION} | cut -d. -f1`
function usage() {
echo -e "${BTXTRED}
**********************************************************************
Usage:
./`basename $0` <PYTHON_VERSION> [INSTALL_DIR_PREFIX]
For example:
./`basename $0` 3.6.0 /opt/python
**********************************************************************
${TXTRESET}\n"
}
function check_parameters() {
if [ -z ${PYTHON_VERSION} ]; then
usage
exit ${NO_ARGS}
fi
if [ -z ${INSTALL_DIR_PREFIX} ]; then
INSTALL_DIR_PREFIX=${DEFAULT_INSTALL_DIR_PREFIX}
fi
}
function prepare_environment() {
WORKSPACE=${PWD}
PYTHON_VERSION_SHORT=`echo ${PYTHON_VERSION} | cut -d "." -f -2`
PYTHON_SRC_DIR=Python-${PYTHON_VERSION}
PYTHON_SRC_PKG_NAME=Python-${PYTHON_VERSION}.tar.xz
TARGET_INSTALL_DIR=${INSTALL_DIR_PREFIX}/python${PYTHON_VERSION}
PYTHON_BIN_NAME=`echo "python${PYTHON_VERSION}" | cut -d\. -f1-2`
PYTHON_BIN_PATH=${TARGET_INSTALL_DIR}/bin/${PYTHON_BIN_NAME}
PYTHON_SRC_PKG_URL=${PYTHON_SRC_URL_PREFIX}/${PYTHON_VERSION}/${PYTHON_SRC_PKG_NAME}
echo -e "${BTXTGREEN}
******************* Python Install Environment ***********************
Python Source Package: ${PYTHON_SRC_PKG_URL}
Python Install Directory: ${TARGET_INSTALL_DIR}
Python Bin Directory: ${TARGET_INSTALL_DIR}/bin
Python Bin Path: ${PYTHON_BIN_PATH}
Python Startup Path: /usr/local/bin/python${PYTHON_VERSION}
******************* Python Install Environment ***********************
${TXTRESET}\n"
}
function install_build_deps() {
echo -e "${BTXTGREEN}
**********************************************************************
Start to install dependencies, make sure you have root permission.
**********************************************************************
${TXTRESET}\n"
if [ -f /usr/bin/yum ]; then
yum -y groupinstall "Development tools"
INSTALL_CMDLINE="yum -y install \
openssl*-devel bzip2-devel \
expat-devel gdbm-devel \
readline-devel sqlite-devel wget"
elif [ -f /usr/bin/apt-get ]; then
INSTALL_CMDLINE="apt-get -y install \
build-essential libncursesw5-dev \
libreadline6-dev libssl-dev \
libgdbm-dev libc6-dev \
libsqlite3-dev tk-dev bzip2 libbz2-dev wget"
fi
sudo ${INSTALL_CMDLINE}
if [ "$?" -ne "0" ]; then
echo -e "${BTXTRED}
Errors happened or failed install dependencies,
please re-run to install it again.
${TXTRESET}\n"
exit ${ERRORS}
fi
}
function get_python_source_pkg() {
echo -e "${BTXTGREEN}Start to download python source code now...${TXTRES}\n"
wget ${PYTHON_SRC_PKG_URL}
if [ "$?" -ne "0" ]; then
exit ${ERROR_DOWNLOAD}
fi
}
function build_install_python() {
cd ${WORKSPACE}
if [ -f ${PYTHON_SRC_PKG_NAME} ]; then
tar xvf ${PYTHON_SRC_PKG_NAME}
else
echo -e "${PYTHON_SRC_PKG_NAME} is not found!"
exit ${FILE_NOT_EXISTS}
fi
if [ "$?" -ne "0" ]; then
echo -e "${BTXTRED}Errors on extracting source package ${PYTHON_SRC_PKG_NAME}${TXTRESET}\n"
exit 5
else
cd ${PYTHON_SRC_DIR}
echo -e "${BTXTGREEN}
**********************************************************************
Start to build and install Python ${PYTHON_VERSION} now...
**********************************************************************
${TXTRESET}\n"
./configure --prefix=${TARGET_INSTALL_DIR}
make
sudo make install
fi
}
function post_install() {
if [ -f ${PYTHON_BIN_PATH} ]; then
echo -e "${BTXTGREEN}
**********************************************************************
Creating symblinks below
/usr/local/bin/python${PYTHON_VERSION}
/usr/local/bin/python${PYTHON_VERSION_SHORT}
**********************************************************************
${TXTRESET}\n"
sudo ln -s -f ${PYTHON_BIN_PATH} /usr/local/bin/python${PYTHON_VERSION}
sudo ln -s -f ${PYTHON_BIN_PATH} /usr/local/bin/python${PYTHON_VERSION_SHORT}
else
echo -e "${BTXTRED}${PYTHON_BIN_PATH} doesn't exist.${TXTRESET}\n"
fi
}
function clean_build_artifacts() {
cd ${WORKSPACE}
sudo rm -rf ${PYTHON_SRC_DIR}
sudo rm -rf ${PYTHON_SRC_PKG_NAME}
}
function install_pip() {
if [ "${PYTHON_VERSION_MAJOR}" -ne "3" ]; then
./pip_installer.sh ${PYTHON_VERSION} ${INSTALL_DIR_PREFIX}
fi
}
function main() {
check_parameters
install_build_deps
prepare_environment
get_python_source_pkg
build_install_python
post_install
clean_build_artifacts
}
main