-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-python-src.sh
More file actions
executable file
·228 lines (207 loc) · 5.35 KB
/
get-python-src.sh
File metadata and controls
executable file
·228 lines (207 loc) · 5.35 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
215
216
217
218
219
220
221
222
223
224
225
226
227
#! /bin/bash
# File: get-python-src.sh
# Github: https://github.com/scottsideleau/get-python-src
# License: Copyright (C) 2021, Scott R. Sideleau (@scottsideleau)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# A copy of the License is distributed with this source. Else,
# you may obtain a copy of the License at:
#
# https://www.apache.org/licenses/LICENSE-2.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
WELCOME="
FETCH (and/or BUILD) source for Custom Python Environment
------------------------------------------------------------------------"
echo "${WELCOME}"
USAGE="
./get-python-src.sh <version> <params>
The <version> is required:
- Major/Minor e.g. 3.6 Fetches latest release
- Major/Minor/Patch e.g. 3.6.14 Fetches user-specified release
At least one <param> is required:
"fetch" - Only fetch the source (.tgz)
"build" - Fetch, unpack, & build the source (.so version)
"static" - Build the source as static library (.a version)
"install" - Install the Python build to /opt/python<version>
"clean" - Clean the src/ directory
NOTE: The <params> can be stacked (any order) for increased effect.
"
# Initialize
FETCH=false
BUILD=false
INSTALL=false
STATIC=false
# Facilitate running this script from any location.
if [ `uname` = "Linux" ]; then
SCRIPT_PATH=`readlink -f $0`
else
# Mac/BSD version of 'readlink' doesn't support -f option
SCRIPT_PATH="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
fi
SCRIPT_DIR=`dirname ${SCRIPT_PATH}`
# Store the Python version information
if [ -z "${1}" ];
then
echo " ERROR: Missing version"
echo -n "${USAGE}"
exit 1
else
if [[ ${1} =~ ^[0-9].[0-9][0-9]?$ ]];
then
SPECIFIC=false
elif [[ ${1} =~ ^[0-9].[0-9][0-9]?.[0-9][0-9]?$ ]];
then
SPECIFIC=true
else
echo " ERROR: Invalid version '${1}'"
echo " Use form N.N or N.N.N"
echo "${USAGE}"
exit 1
fi
VERSION=${1}
fi
# Check for at least one user-provided parameter
if [ -z "${2}" ];
then
echo " ERROR: Missing parameter"
echo -n "${USAGE}"
exit 1
fi
# Handle the user-provided parameters
PARAMS=""
while (( "${#}" ));
do
case "${1}" in
--fetch|-fetch|fetch)
FETCH=true
shift
;;
--build|-build|build)
BUILD=true
shift
;;
--static|-static|static)
BUILD=true
STATIC=true
shift
;;
--install|-install|install)
BUILD=true
INSTALL=true
shift
;;
--clean|-clean|clean)
pushd ${SCRIPT_DIR} >& /dev/null
echo " Cleaning the src/ directory ...
"
rm -rf src/*
popd >& /dev/null
shift
;;
--*|-*)
echo " ERROR: unsupported flag '${1}' " >&2
echo "${USAGE}"
exit 1
;;
*)
PARAMS="${PARAMS} ${1}"
shift
;;
esac
done
eval set -- ${PARAMS}
# Show version
echo " Version: ${VERSION}"
# Show fetch status
echo -n " Fetch: "
if [ ${FETCH} = true ];
then
echo "Yes"
else
echo "No"
fi
# Show build status
echo -n " Build: "
if [ ${BUILD} = true ];
then
echo "Yes"
else
echo "No"
fi
# Evaluate status & print usage if no action requested
if [ ${FETCH} = false -a ${BUILD} = false ];
then
echo "${USAGE}"
exit 1
fi
# Get list of versions
VERSIONS=$( curl --silent https://www.python.org/ftp/python/ \
| grep ">[0-9].*\/<" \
| awk -v FS="(>|/<)" '{print $2}' \
| sort --version-sort )
# If desired, fetch the source
if [ ${FETCH} = true ];
then
if [ ${SPECIFIC} = true ];
then
VER=${VERSION}
else
VER=$( echo "$VERSIONS" \
| grep "${VERSION}\." \
| sort --version-sort \
| tail -1 )
fi
URL="https://www.python.org/ftp/python/${VER}/Python-${VER}.tgz"
echo "
Fetching source for Python ${VER} from:"
echo " ${URL}
"
$( mkdir ${SCRIPT_DIR}/downloads >& /dev/null )
pushd ${SCRIPT_DIR}/downloads >& /dev/null
$( curl --remote-name --location --continue-at - ${URL} )
popd >& /dev/null
fi
# If desired, build the source
if [ ${BUILD} = true ];
then
$( mkdir ${SCRIPT_DIR}/src >& /dev/null )
pushd ${SCRIPT_DIR}/src >& /dev/null
echo "
Extracting Python-${VER}.tgz ..."
$( tar --extract --gzip --file=../downloads/Python-${VER}.tgz )
$( mkdir Python-${VER}/debug >& /dev/null )
pushd Python-${VER}/debug >& /dev/null
echo " Configuring ..."
$( mkdir -p /opt/python${VER}/lib )
if [ ! -d "/opt/python${VER}/lib" ];
then
echo " Error: Unable to write to /opt"
echo " Check your permissions"
exit 1
fi
if [ ${STATIC} = true ];
then
DYNOPT=""
DYNLIB=""
else
DYNOPT="--enable-shared"
DYNLIB="-Wl,-rpath /opt/python${VER}/lib"
fi
../configure ${DYNOPT} --with-lto --with-ensurepip=install \
--enable-optimizations --prefix=/opt/python${VER} \
LDFLAGS="${DYNLIB}"
make -j $(getconf _NPROCESSORS_ONLN)
# If desired, install
if [ ${INSTALL} = true ];
then
make altinstall
fi
popd >& /dev/null
popd >& /dev/null
fi