-
Notifications
You must be signed in to change notification settings - Fork 14
/
travis_tools.sh
330 lines (289 loc) · 10.3 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
#!/bin/bash
# Use with ``source travis_tools.sh``
# Get our own location on this filesystem
TERRYFY_DIR=$(dirname "${BASH_SOURCE[0]}")
# Work round bug in travis xcode image described at
# https://github.com/direnv/direnv/issues/210
function is_function {
# Echo "true" if input argument string is a function
# Allow errors during "set -e" blocks.
(set +e; echo $($(declare -Ff "$1") > /dev/null && echo true))
}
if [ -z "$(is_function "shell_session_update")" ]; then
shell_session_update() { :; }
fi
# Get multibuild utilities
(cd $TERRYFY_DIR && git submodule update --init)
source $TERRYFY_DIR/multibuild/osx_utils.sh
MACPORTS_VERSION=${MACPORTS_VERSION:-2.6.2}
MACPORTS_URL=https://github.com/macports/macports-base/releases/download/v$MACPORTS_VERSION
MACPORTS_PREFIX=/opt/local
MACPORTS_PY_PREFIX=$MACPORTS_PREFIX$MACPYTHON_PY_PREFIX
# -q to avoid this:
# https://lists.macosforge.org/pipermail/macports-users/2014-June/035672.html
PORT_INSTALL="sudo port -q install"
NIPY_WHEELHOUSE=https://nipy.bic.berkeley.edu/scipy_installers
GET_PIP=get-pip.py
GET_PIP_URL=https://bootstrap.pypa.io/${GET_PIP}
function get_osx_version {
# Echo OSX version
sw_vers | grep ProductVersion | awk '{print $2}'
}
function osx_version2version_name {
# Report OSX version + name as used by MacPorts
local version=$1
local lexed=$(lex_ver $version)
local pruned="${lexed:0:6}"
if [ $pruned == "010006" ]; then echo "10.6-SnowLeopard";
elif [ $pruned == "010007" ]; then echo "10.7-Lion";
elif [ $pruned == "010008" ]; then echo "10.8-MountainLion";
elif [ $pruned == "010009" ]; then echo "10.9-Mavericks";
elif [ $pruned == "010010" ]; then echo "10.10-Yosemite";
elif [ $pruned == "010011" ]; then echo "10.11-ElCapitan";
elif [ $pruned == "010012" ]; then echo "10.12-Sierra";
elif [ $pruned == "010013" ]; then echo "10.13-HighSierra";
elif [ $pruned == "010014" ]; then echo "10.14-Mojave";
elif [ $pruned == "010015" ]; then echo "10.15-Catalina";
else
echo "Did not recognize OSX version $version"
exit 1
fi
}
function require_success {
local status=$?
local message=$1
if [ "$status" != "0" ]; then
echo $message
exit $status
fi
}
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 pyver_ge {
# Echo 1 if first python version is greater or equal to second
# Parameters
# $first (python version in major.minor.extra format)
# $second (python version in major.minor.extra format)
if [ $(lex_ver $1) -ge $(lex_ver $2) ]; then echo 1; 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_macports {
# Initialize macports, put macports on PATH
local osx_version=$(get_osx_version)
local vers_name=$(osx_version2version_name $osx_version)
local macports_pkg=MacPorts-$MACPORTS_VERSION-${vers_name}.pkg
local macports_path=$DOWNLOADS_SDIR/$macports_pkg
mkdir -p $DOWNLOADS_SDIR
curl -L $MACPORTS_URL/$macports_pkg > $macports_path
require_success "failed to download macports"
sudo installer -pkg $macports_path -target /
require_success "failed to install macports"
PATH=$MACPORTS_PREFIX/bin:$PATH
sudo port -d 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 '.'`
$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`
$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`
$PORT_INSTALL py$py_mm_nodot-virtualenv
VIRTUALENV_CMD="$MACPORTS_PREFIX/bin/virtualenv-$py_mm"
}
function brew_install_python {
# Installs homebrew 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}
local other_digit=2
[[ $py_digit = 2 ]] && other_digit=3
brew unlink python@${other_digit}
brew uninstall --force --ignore-dependencies python@${py_digit}
brew install --force python@${py_digit}
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 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.10
# (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.macosx_10_10_intel.macosx_10_10_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
# Install python, pip, maybe virtualenv for different systems
case $install_type in
macpython)
get_macpython_environment $version $venv_dir
return
;;
macports)
remove_travis_ve_pip
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
remove_travis_ve_pip
brew update
brew_install_python $version
brew_set_pip_cmd
if [ -n "$venv_dir" ]; then
install_virtualenv
make_workon_venv $venv_dir
else # Need sudo for pip installs
PIP_CMD="sudo $PIP_CMD"
fi
;;
system)
PYTHON_EXE="/usr/bin/python"
PIP_CMD="${PYTHON_EXE} -m pip"
remove_travis_ve_pip
curl -LO $GET_PIP_URL
$PYTHON_EXE $GET_PIP --user
if [ -n "$venv_dir" ]; then
$PIP_CMD install --user virtualenv
# Resets PYTHON_EXE, PIP_CMD to virtualenv versions.
make_workon_venv $venv_dir
fi
;;
*)
echo "Strange install type $install_type"
exit 1
;;
esac
set_py_vars
}
TRAVIS_TOOLS_DIR=`realpath $( dirname "$BASH_SOURCE[0]}" )`
function checkout_closest_tag {
# Check out tag closest to origin/master
# Parameters
# submodule (name of submodule)
# branch (name of branch to start from; optional, default "master")
local submodule=$1
check_var $submodule
local branch=$2
if [ -z "$branch" ]; then branch=master; fi
cd $submodule
git fetch
git fetch --tags
git checkout `$TRAVIS_TOOLS_DIR/git-closest-tag origin/$branch`
cd ..
}
function checkout_commit {
# Check out named commit or latest
# Parameters
# submodule (name of submodule)
# commitish ('latest-tag' or a git ref; optional, default
# 'master')
# branch (name of branch to start 'latest-tag' from; optional,
# default "master")
local submodule=$1
check_var $submodule
local commitish=$2
if [ -z "$commitish" ]; then commitish=master; fi
local branch=$3
if [ -z "$branch" ]; then branch=master; fi
cd $submodule
git fetch
git fetch --tags
if [ "$commitish" == 'latest-tag' ]; then
commitish=`$TRAVIS_TOOLS_DIR/git-closest-tag $branch`
fi
git checkout $commitish
cd ..
}