Skip to content

Commit f22a33b

Browse files
committed
ENH: Use entry_points to install the f2py scripts.
This adds entry_points for the f2py scripts. The installed scripts differ between Windows and other environments. * On Windows, the only script installed is 'f2py'. This works well in that environment because each Python version is installed in its own directory, making it easy to keep the differing script versions separate. * Otherwise, three scripts are installed, 'f2py', 'f2py' + 'minor', and 'f2py' + 'major.minor'. For instance, if Numpy is installed by Python 2.7, then the installed scripts will be named 'f2py', 'f2py2', and 'f2py2.7'. That naming scheme is used for back compatibility, and also so that more than one Python version can be dealt with in a way common to many Linux distros. Note that 'f2py' will always point to the latest install and 'f2py(2|3)' to the latest Python (2|3) install The script tests have been modified to check for the new environment and the code previously used to install the scripts has been removed.
1 parent d0a0e38 commit f22a33b

File tree

5 files changed

+65
-94
lines changed

5 files changed

+65
-94
lines changed

numpy/f2py/__main__.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
# See http://cens.ioc.ee/projects/f2py2e/
22
from __future__ import division, print_function
33

4-
import os
5-
import sys
6-
for mode in ["g3-numpy", "2e-numeric", "2e-numarray", "2e-numpy"]:
7-
try:
8-
i = sys.argv.index("--" + mode)
9-
del sys.argv[i]
10-
break
11-
except ValueError:
12-
pass
13-
os.environ["NO_SCIPY_IMPORT"] = "f2py"
14-
if mode == "g3-numpy":
15-
sys.stderr.write("G3 f2py support is not implemented, yet.\\n")
16-
sys.exit(1)
17-
elif mode == "2e-numeric":
18-
from f2py2e import main
19-
elif mode == "2e-numarray":
20-
sys.argv.append("-DNUMARRAY")
21-
from f2py2e import main
22-
elif mode == "2e-numpy":
23-
from numpy.f2py import main
24-
else:
25-
sys.stderr.write("Unknown mode: " + repr(mode) + "\\n")
26-
sys.exit(1)
4+
from f2py2e import main
5+
276
main()

numpy/f2py/f2py2e.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,13 +644,25 @@ def main():
644644
from numpy.distutils.system_info import show_all
645645
show_all()
646646
return
647+
648+
# Probably outdated options that were not working before 1.16
649+
if '--g3-numpy' in sys.argv[1:]:
650+
sys.stderr.write("G3 f2py support is not implemented, yet.\\n")
651+
sys.exit(1)
652+
elif '--2e-numeric' in sys.argv[1:]:
653+
sys.argv.remove('--2e-numeric')
654+
elif '--2e-numarray' in sys.argv[1:]:
655+
# Note that this errors becaust the -DNUMARRAY argument is
656+
# not recognized. Just here for back compatibility and the
657+
# error message.
658+
sys.argv.append("-DNUMARRAY")
659+
sys.argv.remove('--2e-numarray')
660+
elif '--2e-numpy' in sys.argv[1:]:
661+
sys.argv.remove('--2e-numpy')
662+
else:
663+
pass
664+
647665
if '-c' in sys.argv[1:]:
648666
run_compile()
649667
else:
650668
run_main(sys.argv[1:])
651-
652-
# if __name__ == "__main__":
653-
# main()
654-
655-
656-
# EOF

numpy/f2py/setup.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,29 @@
1818
"""
1919
from __future__ import division, print_function
2020

21-
__version__ = "$Id: setup.py,v 1.32 2005/01/30 17:22:14 pearu Exp $"
22-
2321
import os
2422
import sys
2523
from distutils.dep_util import newer
2624
from numpy.distutils import log
2725
from numpy.distutils.core import setup
2826
from numpy.distutils.misc_util import Configuration
2927

30-
from __version__ import version
31-
32-
33-
def _get_f2py_shebang():
34-
""" Return shebang line for f2py script
3528

36-
If we are building a binary distribution format, then the shebang line
37-
should be ``#!python`` rather than ``#!`` followed by the contents of
38-
``sys.executable``.
39-
"""
40-
if set(('bdist_wheel', 'bdist_egg', 'bdist_wininst',
41-
'bdist_rpm')).intersection(sys.argv):
42-
return '#!python'
43-
return '#!' + sys.executable
29+
from __version__ import version
4430

4531

4632
def configuration(parent_package='', top_path=None):
4733
config = Configuration('f2py', parent_package, top_path)
48-
4934
config.add_data_dir('tests')
50-
51-
config.add_data_files('src/fortranobject.c',
52-
'src/fortranobject.h',
53-
)
54-
55-
config.make_svn_version_py()
56-
57-
def generate_f2py_py(build_dir):
58-
f2py_exe = 'f2py' + os.path.basename(sys.executable)[6:]
59-
if f2py_exe[-4:] == '.exe':
60-
f2py_exe = f2py_exe[:-4] + '.py'
61-
if 'bdist_wininst' in sys.argv and f2py_exe[-3:] != '.py':
62-
f2py_exe = f2py_exe + '.py'
63-
target = os.path.join(build_dir, f2py_exe)
64-
if newer(__file__, target):
65-
log.info('Creating %s', target)
66-
f = open(target, 'w')
67-
f.write(_get_f2py_shebang() + '\n')
68-
mainloc = os.path.join(os.path.dirname(__file__), "__main__.py")
69-
with open(mainloc) as mf:
70-
f.write(mf.read())
71-
f.close()
72-
return target
73-
74-
config.add_scripts(generate_f2py_py)
75-
76-
log.info('F2PY Version %s', config.get_version())
77-
35+
config.add_data_files(
36+
'src/fortranobject.c',
37+
'src/fortranobject.h')
7838
return config
7939

40+
8041
if __name__ == "__main__":
8142

8243
config = configuration(top_path='')
83-
print('F2PY Version', version)
8444
config = config.todict()
8545

8646
config['download_url'] = "http://cens.ioc.ee/projects/f2py2e/2.x"\

numpy/tests/test_scripts.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,37 @@ def run_command(cmd, check_code=True):
6262
@pytest.mark.skipif(is_inplace, reason="Cannot test f2py command inplace")
6363
def test_f2py():
6464
# test that we can run f2py script
65+
66+
def try_f2py_commands(cmds):
67+
success = 0
68+
for f2py_cmd in cmds:
69+
try:
70+
code, stdout, stderr = run_command([f2py_cmd, '-v'])
71+
assert_equal(stdout.strip(), b'2')
72+
success += 1
73+
except Exception:
74+
pass
75+
return success
76+
6577
if sys.platform == 'win32':
78+
# Only the single 'f2py' script is installed in windows.
6679
exe_dir = dirname(sys.executable)
67-
6880
if exe_dir.endswith('Scripts'): # virtualenv
69-
f2py_cmd = r"%s\f2py.py" % exe_dir
81+
f2py_cmds = [os.path.join(exe_dir, 'f2py')]
7082
else:
71-
f2py_cmd = r"%s\Scripts\f2py.py" % exe_dir
72-
73-
code, stdout, stderr = run_command([sys.executable, f2py_cmd, '-v'])
74-
success = stdout.strip() == b'2'
75-
assert_(success, "Warning: f2py not found in path")
83+
f2py_cmds = [os.path.join(exe_dir, "Scripts", 'f2py')]
84+
success = try_f2py_commands(f2py_cmds)
85+
msg = "Warning: f2py not found in path"
86+
assert_(success == 1, msg)
7687
else:
88+
# Three scripts are installed in Unix-like systems:
89+
# 'f2py', 'f2py{major}', and 'f2py{major.minor}'. For example,
90+
# if installed with python3.7 the scripts would be named
91+
# 'f2py', 'f2py3', and 'f2py3.7'.
7792
version = sys.version_info
7893
major = str(version.major)
7994
minor = str(version.minor)
80-
8195
f2py_cmds = ('f2py', 'f2py' + major, 'f2py' + major + '.' + minor)
82-
success = False
83-
84-
for f2py_cmd in f2py_cmds:
85-
try:
86-
code, stdout, stderr = run_command([f2py_cmd, '-v'])
87-
assert_equal(stdout.strip(), b'2')
88-
success = True
89-
break
90-
except Exception:
91-
pass
92-
msg = "Warning: neither %s nor %s nor %s found in path" % f2py_cmds
93-
assert_(success, msg)
96+
success = try_f2py_commands(f2py_cmds)
97+
msg = "Warning: not all of %s, %s, and %s are found in path" % f2py_cmds
98+
assert_(success == 3, msg)

setup.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,18 @@ def setup_package():
352352
# Rewrite the version file everytime
353353
write_version_py()
354354

355+
# The f2py scripts that will be installed
356+
if sys.platform == 'win32':
357+
f2py_cmds = [
358+
'f2py = numpy.f2py.f2py2e:main',
359+
]
360+
else:
361+
f2py_cmds = [
362+
'f2py = numpy.f2py.f2py2e:main',
363+
'f2py%s = numpy.f2py.f2py2e:main' % sys.version_info[:1],
364+
'f2py%s.%s = numpy.f2py.f2py2e:main' % sys.version_info[:2],
365+
]
366+
355367
metadata = dict(
356368
name = 'numpy',
357369
maintainer = "NumPy Developers",
@@ -368,6 +380,9 @@ def setup_package():
368380
cmdclass={"sdist": sdist_checked},
369381
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
370382
zip_safe=False,
383+
entry_points={
384+
'console_scripts': f2py_cmds
385+
},
371386
)
372387

373388
if "--force" in sys.argv:

0 commit comments

Comments
 (0)