forked from MacPython/terryfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravis_tools.sh
370 lines (313 loc) · 10.2 KB
/
travis_tools.sh
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
# Use with ``source travis_tools.sh``
MACPYTHON_URL=https://www.python.org/ftp/python
MACPYTHON_PY_PREFIX=/Library/Frameworks/Python.framework/Versions
GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py
MACPORTS_URL=https://distfiles.macports.org/MacPorts
MACPORTS_VERSION="MacPorts-2.2.1"
MACPORTS_PREFIX=/opt/local
MACPORTS_PY_PREFIX=$MACPORTS_PREFIX$MACPYTHON_PY_PREFIX
NIPY_WHEELHOUSE=https://nipy.bic.berkeley.edu/scipy_installers
DOWNLOADS_SDIR=downloads
WORKING_SDIR=working
function require_success {
STATUS=$?
MESSAGE=$1
if [ "$STATUS" != "0" ]; then
echo $MESSAGE
exit $STATUS
fi
}
function check_python {
if [ -z "$PYTHON_EXE" ]; then
echo "PYTHON_EXE variable not defined"
exit 1
fi
}
function check_pip {
if [ -z "$PIP_CMD" ]; then
echo "PIP_CMD variable not defined"
exit 1
fi
}
function check_var {
if [ -z "$1" ]; then
echo "required variable not defined"
exit 1
fi
}
function abspath {
python -c "import os; print(os.path.abspath('$1'))"
}
function realpath {
python -c "import os; print(os.path.realpath('$1'))"
}
function get_py_digit {
check_python
$PYTHON_EXE -c "import sys; print(sys.version_info[0])"
}
function get_py_mm {
check_python
$PYTHON_EXE -c "import sys; print('{0}.{1}'.format(*sys.version_info[0:2]))"
}
function get_py_mm_nodot {
check_python
$PYTHON_EXE -c "import sys; print('{0}{1}'.format(*sys.version_info[0:2]))"
}
function get_py_prefix {
check_python
$PYTHON_EXE -c "import sys; print(sys.prefix)"
}
function get_py_site_packages {
# Return site-packages directory using PYTHON_EXE
check_python
$PYTHON_EXE -c "from distutils import sysconfig; print(sysconfig.get_python_lib())"
}
function toggle_py_sys_site_packages {
# When in virtualenv (not checked) toggle use of system site packages
local no_sp_fname="`get_py_site_packages`/../no-global-site-packages.txt"
if [ -f "$no_sp_fname" ]; then
rm $no_sp_fname
else
touch $no_sp_fname
fi
}
function get_pip_sudo {
# Echo "sudo" if PIP_CMD starts with sudo
# Useful for checking if Python installations need sudo
check_pip
if [ "${PIP_CMD:0:4}" == "sudo" ]; then
echo "sudo"
fi
}
function install_macpython {
# Installs Python.org Python
# Parameter $version
# Version given in major.minor.micro e.g "3.4.1"
# sets $PYTHON_EXE variable to python executable
local py_version=$1
local py_dmg=python-$py_version-macosx10.6.dmg
local dmg_path=$DOWNLOADS_SDIR/$py_dmg
mkdir -p $DOWNLOADS_SDIR
curl $MACPYTHON_URL/$py_version/${py_dmg} > $dmg_path
require_success "Failed to download mac python $py_version"
hdiutil attach $dmg_path -mountpoint /Volumes/Python
sudo installer -pkg /Volumes/Python/Python.mpkg -target /
require_success "Failed to install Python.org Python $py_version"
local py_mm=${py_version:0:3}
PYTHON_EXE=$MACPYTHON_PY_PREFIX/$py_mm/bin/python$py_mm
}
function install_pip {
# Generic install pip
# Gets needed version from version implied by $PYTHON_EXE
# Installs pip into python given by $PYTHON_EXE
# Assumes pip will be installed into same directory as $PYTHON_EXE
check_python
mkdir -p $DOWNLOADS_SDIR
curl $GET_PIP_URL > $DOWNLOADS_SDIR/get-pip.py
require_success "failed to download get-pip"
sudo $PYTHON_EXE $DOWNLOADS_SDIR/get-pip.py
require_success "Failed to install pip"
local py_mm=`get_py_mm`
PIP_CMD="sudo `dirname $PYTHON_EXE`/pip$py_mm"
}
function install_virtualenv {
# Generic install of virtualenv
# Installs virtualenv into python given by $PYTHON_EXE
# Assumes virtualenv will be installed into same directory as $PYTHON_EXE
check_pip
$PIP_CMD install virtualenv
require_success "Failed to install virtualenv"
check_python
VIRTUALENV_CMD="`dirname $PYTHON_EXE`/virtualenv"
}
function make_workon_venv {
# Make a virtualenv in given directory ('venv' default)
# Set $PYTHON_EXE, $PIP_CMD to virtualenv versions
# Parameter $venv_dir
# directory for virtualenv
local venv_dir=$1
if [ -z "$venv_dir" ]; then
venv_dir="venv"
fi
venv_dir=`abspath $venv_dir`
check_python
$VIRTUALENV_CMD --python=$PYTHON_EXE $venv_dir
PYTHON_EXE=$venv_dir/bin/python
PIP_CMD=$venv_dir/bin/pip
}
function install_macports {
# Initialize macports, put macports on PATH
local macports_path=$DOWNLOADS_SDIR/$MACPORTS_VERSION.tar.gz
mkdir -p $DOWNLOADS_SDIR
curl $MACPORTS_URL/$MACPORTS_VERSION.tar.gz > $macports_path --insecure
require_success "failed to download macports"
mkdir -p $WORKING_SDIR
cd $WORKING_SDIR
tar -xzf ../$macports_path
cd $MACPORTS_VERSION
./configure --prefix=$MACPORTS_PREFIX
make
sudo make install
cd ../..
PATH=$MACPORTS_PREFIX/bin:$PATH
sudo port -v selfupdate
}
function macports_install_python {
# Installs macports python
# Parameter $version
# Version given in format major.minor e.g. "3.4"
# sets $PYTHON_EXE variable to python executable
local py_version=$1
local force=$2
if [ "$force" == "1" ]; then
force="-f"
fi
local py_mm=${py_version:0:3}
local py_mm_nodot=`echo $py_mm | tr -d '.'`
sudo port install $force python$py_mm_nodot
require_success "Failed to install macports python"
PYTHON_EXE=`realpath $MACPORTS_PREFIX/bin/python$py_mm`
}
function macports_install_pip {
# macports install of pip
# Gets needed version from version implied by $PYTHON_EXE
local py_mm=`get_py_mm`
local py_mm_nodot=`get_py_mm_nodot`
sudo port install py$py_mm_nodot-pip
PIP_CMD="sudo $MACPORTS_PREFIX/bin/pip-$py_mm"
}
function macports_install_virtualenv {
# macports install of virtualenv
# Gets needed version from version implied by $PYTHON_EXE
local py_mm=`get_py_mm`
local py_mm_nodot=`get_py_mm_nodot`
sudo port install py$py_mm_nodot-virtualenv
VIRTUALENV_CMD="$MACPORTS_PREFIX/bin/virtualenv-$py_mm"
}
function brew_install_python {
# Installs macports python
# Parameter $version
# Version can only be "2" or "3"
# sets $PYTHON_EXE variable to python executable
local py_version=$1
local py_digit=${py_version:0:1}
if [[ "$py_digit" == "3" ]] ; then
brew install python3
else
brew install python
fi
require_success "Failed to install python"
PYTHON_EXE=/usr/local/bin/python$py_digit
}
function brew_set_pip_cmd {
# homebrew set of $PIP_CMD variable
# pip already installed by Python formula
# Gets version from version implied by $PYTHON_EXE
# https://github.com/Homebrew/homebrew/wiki/Homebrew-and-Python
local py_digit=`get_py_digit`
PIP_CMD=/usr/local/bin/pip${py_digit}
}
function patch_sys_python {
# Fixes error discussed here:
# http://stackoverflow.com/questions/22313407/clang-error-unknown-argument-mno-fused-madd-python-package-installation-fa
# Present for OSX 10.9.2 fixed in 10.9.3
# This should be benign for 10.9.3 though
local py_sys_dir="/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7"
pushd $py_sys_dir
if [ -n "`grep fused-madd _sysconfigdata.py`" ]; then
sudo sed -i '.old' 's/ -m\(no-\)\{0,1\}fused-madd//g' _sysconfigdata.py
sudo rm _sysconfigdata.pyo _sysconfigdata.pyc
fi
popd
}
function system_install_pip {
# Install pip into system python
sudo easy_install pip
PIP_CMD="sudo /usr/local/bin/pip"
}
function system_install_virtualenv {
# Install virtualenv into system python
# Needs $PIP_CMD
check_pip
$PIP_CMD install virtualenv
require_success "Failed to install virtualenv"
VIRTUALENV_CMD="/usr/local/bin/virtualenv"
}
function rename_wheels {
local wheelhouse=$1
check_var $wheelhouse
# From https://github.com/minrk/wheelhouse/blob/master/wheelhouse.sh
# tell pip that all of the 10.6 intel wheels will also work on 10.9
# (System Python) and for x86_64
for file in $(find "$wheelhouse" -name '*-macosx_10_6_intel.whl'); do
mv $file $(echo $file | sed s/macosx_10_6_intel.whl/macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.whl/)
done
}
function get_python_environment {
# Set up python environment
# Parameters:
# $install_type : {macpython|macports|homebrew|system}
# Type of Python to install
# $version :
# macpython : major.minor.micro e.g. "3.4.1"
# macpports : major.minor e.g. "3.4"
# homebrew : major e.g "3"
# system : ignored (but required to be not empty)
# $venv_dir : {directory_name|not defined}
# If defined - make virtualenv in this directory, set python / pip
# commands accordingly
#
# Installs Python
# Sets $PYTHON_EXE to path to Python executable
# Sets $PIP_CMD to full command for pip (including sudo if necessary)
# If $venv_dir defined, Sets $VIRTUALENV_CMD to virtualenv executable
# Puts directory of $PYTHON_EXE on $PATH
local install_type=$1
local version=$2
local venv_dir=$3
case $install_type in
macpython)
install_macpython $version
install_pip
if [ -n "$venv_dir" ]; then
install_virtualenv
make_workon_venv $venv_dir
fi
;;
macports)
install_macports
macports_install_python $version
macports_install_pip
if [ -n "$venv_dir" ]; then
macports_install_virtualenv
make_workon_venv $venv_dir
fi
;;
homebrew)
# Homebrew already installed on travis worker
brew update
brew_install_python $version
brew_set_pip_cmd
if [ -n "$venv_dir" ]; then
install_virtualenv
make_workon_venv $venv_dir
fi
;;
system)
PYTHON_EXE="/usr/bin/python"
system_install_pip
if [ -n "$venv_dir" ]; then
system_install_virtualenv
make_workon_venv $venv_dir
fi
;;
*)
echo "Strange install type $install_type"
exit 1
;;
esac
# Put python binary on path and export
export PATH="`dirname $PYTHON_EXE`:$PATH"
export PYTHON_EXE PIP_CMD
}