Skip to content

Commit 53c724b

Browse files
authored
Merge pull request #37 from mrkn/anaconda
Set PYTHONHOME when using anaconda or miniconda
2 parents 8eb6783 + c98808d commit 53c724b

File tree

7 files changed

+163
-30
lines changed

7 files changed

+163
-30
lines changed

.travis.yml

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
1-
sudo: false
21
language: ruby
32

3+
os: linux
4+
5+
dist: trusty
6+
sudo: required
7+
48
rvm:
5-
- ruby-head
6-
- 2.4.2
7-
- 2.3.5
8-
- 2.2.8
9-
- 2.1.10
9+
- ruby-head
10+
- 2.4.2
11+
- 2.3.5
12+
- 2.2.8
13+
- 2.1.10
1014

1115
env:
1216
global:
1317
- PYCALL_DEBUG_FIND_LIBPYTHON=1
1418
matrix:
15-
- PYTHON=python PYENV_VERSION=system
16-
- PYTHON=python3 PYENV_VERSION=3.5.3 LIBPYTHON=wrong_value
17-
- PYENV_VERSION=3.5.3 LIBPYTHON=/opt/python/3.5.3/lib/libpython3.5m.so
18-
19-
addons:
20-
apt:
21-
packages:
22-
- python3
23-
- python3-dev
24-
- python3-all
25-
- python3-all-dev
19+
- PYENV_VERSION=2.7.13
20+
- PYENV_VERSION=3.6.2
21+
- PYENV_VERSION=system LIBPYTHON=versions/3.6.2/lib/libpython3.6m.so
22+
- PYENV_VERSION=miniconda2-4.1.11
23+
- PYENV_VERSION=miniconda3-4.3.11
24+
25+
matrix:
26+
include:
27+
- os: osx
28+
osx_image: xcode9
29+
compiler: clang
30+
rvm: 2.4.1
31+
env: PYENV_VERSION=3.6.2
32+
- os: osx
33+
osx_image: xcode9
34+
compiler: clang
35+
rvm: 2.4.1
36+
env: PYENV_VERSION=system LIBPYTHON=versions/3.6.2/lib/libpython3.6m.so
37+
- os: osx
38+
osx_image: xcode9
39+
compiler: clang
40+
rvm: 2.4.1
41+
env: PYENV_VERSION=miniconda3-4.3.11
42+
allow_failure:
43+
- os: osx
2644

2745
before_install:
28-
- gem update --system
29-
- gem update bundler
46+
- gem update --system
47+
- gem update bundler
48+
- export PATH="$(pyenv root)/bin:$PATH"
49+
- eval "$(pyenv init -)"
3050

31-
before_script:
32-
- bundle exec rake clobber compile
33-
- echo === python investigator.py ===
34-
- python lib/pycall/python/investigator.py
35-
- python3 lib/pycall/python/investigator.py
36-
- travis_retry pip install --user numpy
37-
- PYENV_VERSION=3.5.3 travis_retry pip3 install --user numpy
51+
install:
52+
- ci/travis_install.sh
3853

39-
matrix:
40-
allow_failures:
41-
- env: PYTHON=python # Ignore failed on python 2.7
54+
before_script:
55+
- . ci/travis_before_script.sh
56+
- bundle exec rake clobber compile
57+
- python lib/pycall/python/investigator.py

ci/travis_before_script.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__DIR__=$(cd $(dirname $BASH_SOURCE); pwd)
2+
. $__DIR__/travis_retry.sh
3+
4+
set -ex
5+
6+
if test -z "$PYENV_VERSION"; then
7+
echo "ERROR: PYENV_VERSION is not provided" >2
8+
exit 1
9+
fi
10+
11+
if test -n "$LIBPYTHON"; then
12+
export LIBPYTHON=$(pyenv root)/$LIBPYTHON
13+
fi
14+
15+
case "$PYENV_VERSION" in
16+
*conda*)
17+
source $(pyenv prefix)/bin/activate test-environment
18+
;;
19+
esac
20+
21+
set +ex

ci/travis_install.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /bin/bash
2+
3+
__DIR__=$(cd $(dirname $BASH_SOURCE); pwd)
4+
. $__DIR__/travis_retry.sh
5+
6+
set -ex
7+
8+
if test -z "$PYENV_VERSION"; then
9+
echo "ERROR: PYENV_VERSION is not provided" >2
10+
exit 1
11+
fi
12+
13+
if test -n "$LIBPYTHON"; then
14+
export LIBPYTHON=$(pyenv root)/$LIBPYTHON
15+
fi
16+
17+
if test "$PYENV_VERSION" = "system"; then
18+
if test -z "$LIBPYTHON"; then
19+
echo "ERROR: LIBPYTHON is not provided for PYENV_VERSION=system" >2
20+
exit 1
21+
fi
22+
# NOTE: PYENV_VERSION should be the version of LIBPYTHON during install script
23+
PYENV_VERSION=$(basename $(dirname $(dirname $LIBPYTHON)))
24+
fi
25+
PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install -f $PYENV_VERSION
26+
27+
case "$PYENV_VERSION" in
28+
*conda*)
29+
case "$PYENV_VERSION" in
30+
*conda2*)
31+
python_version=2.7
32+
;;
33+
*)
34+
python_version=3.6
35+
;;
36+
esac
37+
conda config --set always_yes yes --set changeps1 no
38+
travis_retry conda update -q conda
39+
conda info -a
40+
travis_retry conda create -q -n test-environment python=$python_version numpy
41+
source $(pyenv prefix)/bin/activate test-environment
42+
;;
43+
*)
44+
travis_retry pip install --user numpy
45+
;;
46+
esac
47+
48+
bundle install

ci/travis_retry.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
travis_retry ()
2+
{
3+
local result=0
4+
local count=1
5+
while [ $count -le 3 ]; do
6+
[ $result -ne 0 ] && {
7+
echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" 1>&2
8+
}
9+
"$@"
10+
result=$?
11+
[ $result -eq 0 ] && break
12+
count=$(($count + 1))
13+
sleep 1
14+
done
15+
[ $count -gt 3 ] && {
16+
echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" 1>&2
17+
}
18+
return $result
19+
}

lib/pycall/libpython/finder.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def find_libpython(python = nil)
3939
end
4040
end
4141

42+
set_PYTHONHOME(python_config)
4243
libs = make_libs(python_config)
4344
libpaths = make_libpaths(python_config)
4445

@@ -101,6 +102,10 @@ def investigate_python_config(python)
101102
{}.tap do |config|
102103
io.each_line do |line|
103104
key, value = line.chomp.split(': ', 2)
105+
case value
106+
when 'True', 'true', 'False', 'false'
107+
value = (value == 'True' || value == 'true')
108+
end
104109
config[key.to_sym] = value if value != 'None'
105110
end
106111
end
@@ -113,6 +118,17 @@ def python_investigator_py
113118
File.expand_path('../../python/investigator.py', __FILE__)
114119
end
115120

121+
def set_PYTHONHOME(python_config)
122+
if !ENV.has_key?('PYTHONHOME') && python_config[:conda]
123+
case RUBY_PLATFORM
124+
when /mingw32/, /cygwin/, /mswin/
125+
ENV['PYTHONHOME'] = python_config[:exec_prefix]
126+
else
127+
ENV['PYTHONHOME'] = python_config.values_at(:prefix, :exec_prefix).join(':')
128+
end
129+
end
130+
end
131+
116132
def make_libs(python_config)
117133
libs = []
118134
%i(INSTSONAME LDLIBRARY).each do |key|

lib/pycall/python/investigator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from distutils.sysconfig import get_config_var
22
import sys
3+
4+
def conda():
5+
return 'conda' in sys.version or 'Continuum' in sys.version
6+
37
for var in ('executable', 'exec_prefix', 'prefix'):
48
print(var + ': ' + str(getattr(sys, var)))
9+
print('conda: ' + ('true' if conda() else 'false'))
510
print('multiarch: ' + str(getattr(getattr(sys, 'implementation', sys), '_multiarch', None)))
611
for var in ('VERSION', 'INSTSONAME', 'LIBRARY', 'LDLIBRARY', 'LIBDIR', 'PYTHONFRAMEWORKPREFIX', 'MULTIARCH'):
712
print(var + ': ' + str(get_config_var(var)))

spec/spec_helper.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
puts
44
puts "Environment variables:"
5-
%w[LIBPYTHON PYTHON PYTHONPATH PYCALL_DEBUG_FIND_LIBPYTHON].each do |key|
5+
%w[
6+
ANACONDA
7+
LIBPYTHON
8+
PYENV_VERSION
9+
PYTHON
10+
PYTHON_VERSION
11+
PYTHONPATH
12+
PYCALL_DEBUG_FIND_LIBPYTHON
13+
].each do |key|
614
puts "- #{key}=#{ENV[key]}"
715
end
816

0 commit comments

Comments
 (0)