2
0

python-virtualenv.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #! /bin/sh
  2. # Copyright (C) 2011-2017 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. # Check that python support can work well with virtualenvs.
  17. # This test also works as a mild stress-test on the python support.
  18. required='cc python virtualenv'
  19. . test-init.sh
  20. # In case the user's config.site defines pythondir or pyexecdir.
  21. CONFIG_SITE=/dev/null; export CONFIG_SITE
  22. py_version_pre=$($PYTHON -V)
  23. # Skip the test if a proper virtualenv cannot be created.
  24. virtualenv -p"$PYTHON" --verbose virtenv && py_installed virtenv/bin/activate \
  25. || skip_ "couldn't create python virtual environment"
  26. # Activate the virtualenv.
  27. . ./virtenv/bin/activate
  28. # Sanity check.
  29. if test -z "$VIRTUAL_ENV"; then
  30. framework_failure_ "can't activate python virtual environment"
  31. fi
  32. py_version_post=$(python -V)
  33. # Sanity check.
  34. test "$py_version_pre" = "$py_version_post"
  35. cwd=$(pwd) || fatal_ "getting current working directory"
  36. py_version=$(python -c 'import sys; print("%u.%u" % tuple(sys.version_info[:2]))')
  37. py_site=$VIRTUAL_ENV/lib/python$py_version/site-packages
  38. # We need to do do this early, just to set some cache variables properly,
  39. # since because we're going to unset $PYTHON next.
  40. if python_has_pep3147; then
  41. : PEP 3147 will be used in installation of ".pyc" files
  42. fi
  43. # We don't want our original python to be picked up by configure
  44. # invocations.
  45. unset PYTHON
  46. # We need control over the package name.
  47. cat > configure.ac << END
  48. AC_INIT([am_virtenv], [1.0])
  49. AM_INIT_AUTOMAKE
  50. AC_CONFIG_FILES([Makefile])
  51. AC_PROG_CC
  52. AM_PROG_AR
  53. AC_PROG_RANLIB
  54. AM_PATH_PYTHON
  55. AC_OUTPUT
  56. END
  57. cat > Makefile.am << 'END'
  58. python_PYTHON = am_foo.py
  59. pkgpython_PYTHON = __init__.py
  60. pyexec_LIBRARIES = libquux.a
  61. libquux_a_SOURCES = foo.c
  62. pkgpyexec_LIBRARIES = libzardoz.a
  63. libzardoz_a_SOURCES = foo.c
  64. .PYTHON: debug test-run
  65. debug:
  66. @echo PYTHON: $(PYTHON)
  67. @echo PYTHON_VERSION: $(PYTHON_VERSION)
  68. @echo prefix: $(prefix)
  69. @echo pythondir: $(pythondir)
  70. @echo pkgpythondir: $(pkgpythondir)
  71. @echo pyexecdir: $(pyexecdir)
  72. @echo pkgpyexecdir: $(pkgpyexecdir)
  73. test-run:
  74. ## In a virtualenv, the default python must be the custom
  75. ## virtualenv python.
  76. @: \
  77. && py1=`python -c 'import sys; print(sys.executable)'` \
  78. && py2=`$(PYTHON) -c 'import sys; print(sys.executable)'` \
  79. && echo "py1: $$py1" \
  80. && echo "py2: $$py2" \
  81. && test -n "$$py1" \
  82. && test -n "$$py2" \
  83. && test x"$$py1" = x"$$py2"
  84. ## Check that modules installed in the virtualenv are readily
  85. ## available.
  86. python -c 'from am_foo import foo_func; assert (foo_func () == 12345)'
  87. python -c 'from am_virtenv import old_am; assert (old_am () == "AutoMake")'
  88. all-local: debug
  89. END
  90. cat > am_foo.py << 'END'
  91. def foo_func ():
  92. return 12345
  93. END
  94. cat > __init__.py << 'END'
  95. def old_am ():
  96. return 'AutoMake'
  97. END
  98. cat > foo.c << 'END'
  99. int foo (void)
  100. {
  101. return 0;
  102. }
  103. END
  104. check_install ()
  105. {
  106. $MAKE install ${1+"$@"}
  107. test -f "$py_site"/am_foo.py
  108. py_installed "$py_site"/am_foo.pyc
  109. py_installed "$py_site"/am_foo.pyo
  110. py_installed "$py_site"/am_virtenv/__init__.py
  111. py_installed "$py_site"/am_virtenv/__init__.pyc
  112. py_installed "$py_site"/am_virtenv/__init__.pyo
  113. test -f "$py_site"/libquux.a
  114. test -f "$py_site"/am_virtenv/libzardoz.a
  115. }
  116. check_uninstall ()
  117. {
  118. $MAKE uninstall ${1+"$@"}
  119. test ! -e "$py_site"/am_foo.py
  120. py_installed --not "$py_site"/am_foo.pyc
  121. py_installed --not "$py_site"/am_foo.pyo
  122. test ! -e "$py_site"/am_virtenv/__init__.py
  123. py_installed --not "$py_site"/am_virtenv/__init__.pyc
  124. py_installed --not "$py_site"/am_virtenv/__init__.pyo
  125. test ! -e "$py_site"/libquux.a
  126. test ! -e "$py_site"/am_virtenv/libzardoz.a
  127. }
  128. $ACLOCAL
  129. $AUTOCONF
  130. $AUTOMAKE --add-missing
  131. # Try a VPATH build.
  132. mkdir build
  133. cd build
  134. ../configure --prefix="$VIRTUAL_ENV"
  135. check_install
  136. $MAKE test-run
  137. check_uninstall
  138. cd ..
  139. # Try an in-tree build.
  140. ./configure --prefix="$VIRTUAL_ENV"
  141. check_install
  142. $MAKE test-run
  143. check_uninstall
  144. $MAKE distclean
  145. # Overriding pythondir and pyexecdir with cache variables should work.
  146. ./configure am_cv_python_pythondir="$py_site" \
  147. am_cv_python_pyexecdir="$py_site"
  148. check_install
  149. $MAKE test-run
  150. check_uninstall
  151. $MAKE distclean
  152. # Overriding pythondir and pyexecdir at make time should be enough.
  153. ./configure --prefix="$cwd/bad-prefix"
  154. check_install pythondir="$py_site" pyexecdir="$py_site" \
  155. AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
  156. test ! -e bad-prefix
  157. $MAKE test-run
  158. check_uninstall pythondir="$py_site" pyexecdir="$py_site" \
  159. AM_MAKEFLAGS="pythondir='$py_site' pyexecdir='$py_site'"
  160. # Also check that the distribution is self-contained, for completeness.
  161. $MAKE distcheck
  162. # Finally, check that if we disable the virtualenv, we shouldn't be
  163. # able to access to the installed modules anymore.
  164. cd build
  165. $MAKE install
  166. python -c 'import am_foo; print(am_foo.__file__)'
  167. python -c 'import am_virtenv; print(am_virtenv.__file__)'
  168. deactivate "nondestructive"
  169. python -c 'import am_foo' && exit 1
  170. python -c 'import am_virtenv' && exit 1
  171. :