Merge branch 'fix-pr14560' into micro
[platform/upstream/automake.git] / t / python-virtualenv.sh
1 #! /bin/sh
2 # Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # Check that python support can work well with virtualenvs.
18 # This test also works as a mild stress-test on the python support.
19
20 required='cc python virtualenv'
21 . test-init.sh
22
23 # In case the user's config.site defines pythondir or pyexecdir.
24 CONFIG_SITE=/dev/null; export CONFIG_SITE
25
26 py_version_pre=$($PYTHON -V)
27
28 # Skip the test if a proper virtualenv cannot be created.
29 virtualenv -p"$PYTHON" --verbose virtenv && py_installed virtenv/bin/activate \
30   || skip_ "couldn't create python virtual environment"
31
32 # Activate the virtualenv.
33 . ./virtenv/bin/activate
34 # Sanity check.
35 if test -z "$VIRTUAL_ENV"; then
36   framework_failure_ "can't activate python virtual environment"
37 fi
38
39 py_version_post=$(python -V)
40
41 # Sanity check.
42 test "$py_version_pre" = "$py_version_post"
43
44 cwd=$(pwd) || fatal_ "getting current working directory"
45 py_version=$(python -c 'import sys; print("%u.%u" % tuple(sys.version_info[:2]))')
46 py_site=$VIRTUAL_ENV/lib/python$py_version/site-packages
47
48 # We need to do do this early, just to set some cache variables properly,
49 # since because we're going to unset $PYTHON next.
50 if python_has_pep3147; then
51   : PEP 3147 will be used in installation of ".pyc" files
52 fi
53 # We don't want our original python to be picked up by configure
54 # invocations.
55 unset PYTHON
56
57 # We need control over the package name.
58 cat > configure.ac << END
59 AC_INIT([am_virtenv], [1.0])
60 AM_INIT_AUTOMAKE
61 AC_CONFIG_FILES([Makefile])
62 AC_PROG_CC
63 AM_PROG_AR
64 AC_PROG_RANLIB
65 AM_PATH_PYTHON
66 AC_OUTPUT
67 END
68
69 cat > Makefile.am << 'END'
70 python_PYTHON = am_foo.py
71 pkgpython_PYTHON = __init__.py
72 pyexec_LIBRARIES = libquux.a
73 libquux_a_SOURCES = foo.c
74 pkgpyexec_LIBRARIES = libzardoz.a
75 libzardoz_a_SOURCES = foo.c
76
77 .PYTHON: debug test-run
78 debug:
79         @echo PYTHON: $(PYTHON)
80         @echo PYTHON_VERSION: $(PYTHON_VERSION)
81         @echo prefix: $(prefix)
82         @echo pythondir: $(pythondir)
83         @echo pkgpythondir: $(pkgpythondir)
84         @echo pyexecdir: $(pyexecdir)
85         @echo pkgpyexecdir: $(pkgpyexecdir)
86 test-run:
87         ## In a virtualenv, the default python must be the custom
88         ## virtualenv python.
89         @: \
90           && py1=`python -c 'import sys; print(sys.executable)'` \
91           && py2=`$(PYTHON) -c 'import sys; print(sys.executable)'` \
92           && echo "py1: $$py1" \
93           && echo "py2: $$py2" \
94           && test -n "$$py1" \
95           && test -n "$$py2" \
96           && test x"$$py1" = x"$$py2"
97         ## Check that modules installed in the virtualenv are readily
98         ## available.
99         python -c 'from am_foo import foo_func; assert (foo_func () == 12345)'
100         python -c 'from am_virtenv import old_am; assert (old_am () == "AutoMake")'
101 all-local: debug
102 END
103
104 cat > am_foo.py << 'END'
105 def foo_func ():
106     return 12345
107 END
108
109 cat > __init__.py << 'END'
110 def old_am ():
111     return 'AutoMake'
112 END
113
114 cat > foo.c << 'END'
115 int foo (void)
116 {
117   return 0;
118 }
119 END
120
121 check_install ()
122 {
123   $MAKE install ${1+"$@"}
124
125   test -f      "$py_site"/am_foo.py
126   py_installed "$py_site"/am_foo.pyc
127   py_installed "$py_site"/am_foo.pyo
128   py_installed "$py_site"/am_virtenv/__init__.py
129   py_installed "$py_site"/am_virtenv/__init__.pyc
130   py_installed "$py_site"/am_virtenv/__init__.pyo
131   test -f      "$py_site"/libquux.a
132   test -f      "$py_site"/am_virtenv/libzardoz.a
133 }
134
135 check_uninstall ()
136 {
137   $MAKE uninstall ${1+"$@"}
138
139   test ! -e          "$py_site"/am_foo.py
140   py_installed --not "$py_site"/am_foo.pyc
141   py_installed --not "$py_site"/am_foo.pyo
142   test ! -e          "$py_site"/am_virtenv/__init__.py
143   py_installed --not "$py_site"/am_virtenv/__init__.pyc
144   py_installed --not "$py_site"/am_virtenv/__init__.pyo
145   test ! -e          "$py_site"/libquux.a
146   test ! -e          "$py_site"/am_virtenv/libzardoz.a
147 }
148
149 $ACLOCAL
150 $AUTOCONF
151 $AUTOMAKE --add-missing
152
153 # Try a VPATH build.
154 mkdir build
155 cd build
156 ../configure --prefix="$VIRTUAL_ENV"
157 check_install
158 $MAKE test-run
159 check_uninstall
160 cd ..
161
162 # Try an in-tree build.
163 ./configure --prefix="$VIRTUAL_ENV"
164 check_install
165 $MAKE test-run
166 check_uninstall
167
168 $MAKE distclean
169
170 # Overriding pythondir and pyexecdir with cache variables should work.
171 ./configure am_cv_python_pythondir="$py_site" \
172             am_cv_python_pyexecdir="$py_site"
173 check_install
174 $MAKE test-run
175 check_uninstall
176
177 $MAKE distclean
178
179 # Overriding pythondir and pyexecdir at make time should be enough.
180 ./configure --prefix="$cwd/bad-prefix"
181 pythondir=$py_site pyexecdir=$py_site
182 export pythondir pyexecdir
183 check_install -e
184 test ! -e bad-prefix
185 $MAKE test-run
186 check_uninstall -e
187 unset pythondir pyexecdir
188
189 # Also check that the distribution is self-contained, for completeness.
190 $MAKE distcheck
191
192 # Finally, check that if we disable the virtualenv, we shouldn't be
193 # able to access to the installed modules anymore.
194 cd build
195 $MAKE install
196 python -c 'import am_foo; print(am_foo.__file__)'
197 python -c 'import am_virtenv; print(am_virtenv.__file__)'
198 deactivate "nondestructive"
199 python -c 'import am_foo' && exit 1
200 python -c 'import am_virtenv' && exit 1
201
202 :