From 5047404e8b2bbd7d826861073736662671b0271f Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Fri, 13 Feb 2015 01:12:52 +0100 Subject: ENH: make f2py an executable module allows pep 338 execution via python -mnumpy.f2py --- numpy/f2py/__main__.py | 23 +++++++++++++++++++++++ numpy/f2py/setup.py | 30 ++++-------------------------- 2 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 numpy/f2py/__main__.py diff --git a/numpy/f2py/__main__.py b/numpy/f2py/__main__.py new file mode 100644 index 000000000..11dbf5f52 --- /dev/null +++ b/numpy/f2py/__main__.py @@ -0,0 +1,23 @@ +# See http://cens.ioc.ee/projects/f2py2e/ +import os, sys +for mode in ["g3-numpy", "2e-numeric", "2e-numarray", "2e-numpy"]: + try: + i=sys.argv.index("--"+mode) + del sys.argv[i] + break + except ValueError: pass +os.environ["NO_SCIPY_IMPORT"]="f2py" +if mode=="g3-numpy": + sys.stderr.write("G3 f2py support is not implemented, yet.\\n") + sys.exit(1) +elif mode=="2e-numeric": + from f2py2e import main +elif mode=="2e-numarray": + sys.argv.append("-DNUMARRAY") + from f2py2e import main +elif mode=="2e-numpy": + from numpy.f2py import main +else: + sys.stderr.write("Unknown mode: " + repr(mode) + "\\n") + sys.exit(1) +main() diff --git a/numpy/f2py/setup.py b/numpy/f2py/setup.py index 2f1fd6a01..d1c5ad5d8 100644 --- a/numpy/f2py/setup.py +++ b/numpy/f2py/setup.py @@ -52,32 +52,10 @@ def configuration(parent_package='',top_path=None): if newer(__file__, target): log.info('Creating %s', target) f = open(target, 'w') - f.write('''\ -#!%s -# See http://cens.ioc.ee/projects/f2py2e/ -import os, sys -for mode in ["g3-numpy", "2e-numeric", "2e-numarray", "2e-numpy"]: - try: - i=sys.argv.index("--"+mode) - del sys.argv[i] - break - except ValueError: pass -os.environ["NO_SCIPY_IMPORT"]="f2py" -if mode=="g3-numpy": - sys.stderr.write("G3 f2py support is not implemented, yet.\\n") - sys.exit(1) -elif mode=="2e-numeric": - from f2py2e import main -elif mode=="2e-numarray": - sys.argv.append("-DNUMARRAY") - from f2py2e import main -elif mode=="2e-numpy": - from numpy.f2py import main -else: - sys.stderr.write("Unknown mode: " + repr(mode) + "\\n") - sys.exit(1) -main() -'''%(sys.executable)) + f.write('#!%s\n' % (sys.executable)) + mainloc = os.path.join(os.path.dirname(__file__), "__main__.py") + with open(mainloc) as mf: + f.write(mf.read()) f.close() return target -- cgit v1.2.1 From 7c20ee5aa225308e4dc5b126a566ece126c73515 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Wed, 29 Apr 2015 13:00:15 -0700 Subject: BUG: fix f2py shebang line for bdist wheel, egg Command `bdist_wheel` was generating a shebang line for f2py that uses the Python path for the building Python. If we are building a wheel or an egg, use the generic `#!python` shebang line for the f2py script instead, which setuptools will modify at install time. Closes gh-5812. --- numpy/f2py/setup.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/numpy/f2py/setup.py b/numpy/f2py/setup.py index d1c5ad5d8..d165f95fa 100644 --- a/numpy/f2py/setup.py +++ b/numpy/f2py/setup.py @@ -29,6 +29,19 @@ from numpy.distutils.misc_util import Configuration from __version__ import version + +def _get_f2py_shebang(): + """ Return shebang line for f2py script + + If we are building an egg or a wheel binary package, then the shebang line + should be ``#!python`` rather than ``#!`` followed by the contents of + ``sys.executable``. + """ + if set(('bdist_wheel', 'bdist_egg')).intersection(sys.argv): + return '#!python' + return '#!' + sys.executable + + def configuration(parent_package='',top_path=None): config = Configuration('f2py', parent_package, top_path) @@ -52,7 +65,7 @@ def configuration(parent_package='',top_path=None): if newer(__file__, target): log.info('Creating %s', target) f = open(target, 'w') - f.write('#!%s\n' % (sys.executable)) + f.write(_get_f2py_shebang() + '\n') mainloc = os.path.join(os.path.dirname(__file__), "__main__.py") with open(mainloc) as mf: f.write(mf.read()) -- cgit v1.2.1 From fc4904c3fd8ee540225843cc177259a204ad1db7 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Wed, 29 Apr 2015 14:45:17 -0700 Subject: ENH: add bdist_mpkg, bdist_wininst to binary dists Add other binary distribution formats to list of build commands that should generate !python shebang lines. --- numpy/f2py/setup.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/numpy/f2py/setup.py b/numpy/f2py/setup.py index d165f95fa..d8334e687 100644 --- a/numpy/f2py/setup.py +++ b/numpy/f2py/setup.py @@ -33,11 +33,12 @@ from __version__ import version def _get_f2py_shebang(): """ Return shebang line for f2py script - If we are building an egg or a wheel binary package, then the shebang line + If we are building a binary distribution format, then the shebang line should be ``#!python`` rather than ``#!`` followed by the contents of ``sys.executable``. """ - if set(('bdist_wheel', 'bdist_egg')).intersection(sys.argv): + if set(('bdist_wheel', 'bdist_egg', 'bdist_mpkg', 'bdist_wininst', + 'bdist_rpm')).intersection(sys.argv): return '#!python' return '#!' + sys.executable -- cgit v1.2.1 From 96ed3d2b20c5fdab135c42e7bd45d8bd3df26f3b Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Tue, 5 May 2015 12:06:47 -0700 Subject: BUG: revert use of !python for bdist_mpkg scripts bdist_mpkg is a very crude install method that will assume the path to Python, so we should not use the `#!python` form when installing scripts in bdist_mpkg. --- numpy/f2py/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/f2py/setup.py b/numpy/f2py/setup.py index d8334e687..c63ab059a 100644 --- a/numpy/f2py/setup.py +++ b/numpy/f2py/setup.py @@ -37,7 +37,7 @@ def _get_f2py_shebang(): should be ``#!python`` rather than ``#!`` followed by the contents of ``sys.executable``. """ - if set(('bdist_wheel', 'bdist_egg', 'bdist_mpkg', 'bdist_wininst', + if set(('bdist_wheel', 'bdist_egg', 'bdist_wininst', 'bdist_rpm')).intersection(sys.argv): return '#!python' return '#!' + sys.executable -- cgit v1.2.1 From b064e4b4cb3f8d571605ee8a7f53c9ce2d5df879 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Wed, 29 Apr 2015 14:52:27 -0700 Subject: TEST: add module to test installed scripts Module tests whether we can run f2py and return correct version. Skip this test when running in-place (we don't install f2py in that case). Use our own virtualenvs in travis-ci to avoid picking up travis' numpy. --- .travis.yml | 4 +++ numpy/tests/test_scripts.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ tools/travis-test.sh | 13 ++++++--- 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 numpy/tests/test_scripts.py diff --git a/.travis.yml b/.travis.yml index 12a443d41..eba6890df 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,10 @@ before_install: - ulimit -a - mkdir builds - pushd builds + # Build into own virtualenv + # We therefore control our own environment, avoid travis' numpy + - virtualenv --python=python venv + - source venv/bin/activate - pip install nose # pip install coverage - python -V diff --git a/numpy/tests/test_scripts.py b/numpy/tests/test_scripts.py new file mode 100644 index 000000000..b48e3f3f7 --- /dev/null +++ b/numpy/tests/test_scripts.py @@ -0,0 +1,65 @@ +""" Test scripts + +Test that we can run executable scripts that have been installed with numpy. +""" +from __future__ import division, print_function, absolute_import + +import os +from os.path import join as pathjoin, isfile, dirname, basename +import sys +from subprocess import Popen, PIPE +import numpy as np +from numpy.compat.py3k import basestring, asbytes +from nose.tools import assert_equal +from numpy.testing.decorators import skipif + +skipif_inplace = skipif(isfile(pathjoin(dirname(np.__file__), '..', 'setup.py'))) + +def run_command(cmd, check_code=True): + """ Run command sequence `cmd` returning exit code, stdout, stderr + + Parameters + ---------- + cmd : str or sequence + string with command name or sequence of strings defining command + check_code : {True, False}, optional + If True, raise error for non-zero return code + + Returns + ------- + returncode : int + return code from execution of `cmd` + stdout : bytes (python 3) or str (python 2) + stdout from `cmd` + stderr : bytes (python 3) or str (python 2) + stderr from `cmd` + + Raises + ------ + RuntimeError + If `check_code` is True, and return code !=0 + """ + cmd = [cmd] if isinstance(cmd, basestring) else list(cmd) + if os.name == 'nt': + # Quote any arguments with spaces. The quotes delimit the arguments + # on Windows, and the arguments might be file paths with spaces. + # On Unix the list elements are each separate arguments. + cmd = ['"{0}"'.format(c) if ' ' in c else c for c in cmd] + proc = Popen(cmd, stdout=PIPE, stderr=PIPE) + stdout, stderr = proc.communicate() + if proc.poll() == None: + proc.terminate() + if check_code and proc.returncode != 0: + raise RuntimeError('\n'.join( + ['Command "{0}" failed with', + 'stdout', '------', '{1}', '', + 'stderr', '------', '{2}']).format(cmd, stdout, stderr)) + return proc.returncode, stdout, stderr + + +@skipif_inplace +def test_f2py(): + # test that we can run f2py script + f2py_cmd = 'f2py' + basename(sys.executable)[6:] + code, stdout, stderr = run_command([f2py_cmd, '-v']) + assert_equal(stdout.strip(), asbytes('2')) diff --git a/tools/travis-test.sh b/tools/travis-test.sh index de078edf7..9a215314f 100755 --- a/tools/travis-test.sh +++ b/tools/travis-test.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -ex # setup env @@ -117,10 +117,17 @@ fi export PYTHON export PIP if [ -n "$USE_WHEEL" ] && [ $# -eq 0 ]; then - $PIP install --upgrade pip + # Build wheel $PIP install wheel $PYTHON setup.py bdist_wheel - $PIP install --pre --upgrade --find-links dist numpy + # Make another virtualenv to install into + virtualenv --python=python venv-for-wheel + . venv-for-wheel/bin/activate + # Move out of source directory to avoid finding local numpy + pushd dist + $PIP install --pre --upgrade --find-links . numpy + $PIP install nose + popd run_test elif [ "$USE_CHROOT" != "1" ] && [ "$USE_BENTO" != "1" ]; then setup_base -- cgit v1.2.1