summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-03-02 21:53:13 +0000
committerEric Wieser <wieser.eric@gmail.com>2017-03-02 21:53:13 +0000
commit9c09f0105b6a62c0dfe9167fa78c0fb59878e222 (patch)
tree3798208493120f0d883140a50baaf7a0fed2ea1a /numpy
parentfca60ca62c1da49b416cd8525b18e22e79019f30 (diff)
downloadnumpy-9c09f0105b6a62c0dfe9167fa78c0fb59878e222.tar.gz
MAINT: Improve handing of files and subprocesses
Since we can use 2.7+ features now, we can have the with statement and subprocess.check_call
Diffstat (limited to 'numpy')
-rwxr-xr-xnumpy/linalg/lapack_lite/make_lite.py106
1 files changed, 59 insertions, 47 deletions
diff --git a/numpy/linalg/lapack_lite/make_lite.py b/numpy/linalg/lapack_lite/make_lite.py
index 041e34692..2126f267f 100755
--- a/numpy/linalg/lapack_lite/make_lite.py
+++ b/numpy/linalg/lapack_lite/make_lite.py
@@ -1,18 +1,28 @@
#!/usr/bin/env python
+"""
+Usage: make_lite.py <wrapped_routines_file> <lapack_dir> <output_dir>
+
+Typical invocation:
+
+ make_lite.py wrapped_routines /tmp/lapack-3.x.x .
+
+Requires the following to be on the path:
+ * f2c
+
+"""
from __future__ import division, absolute_import, print_function
-import sys, os
+import sys
+import os
+import subprocess
+
import fortran
import clapack_scrub
-try: set
-except NameError:
- from sets import Set as set
-
# Arguments to pass to f2c. You'll always want -A for ANSI C prototypes
# Others of interest: -a to not make variables static by default
# -C to check array subscripts
-F2C_ARGS = '-A'
+F2C_ARGS = ['-A']
# The header to add to the top of the *_lite.c file. Note that dlamch_() calls
# will be replaced by the macros below by clapack_scrub.scrub_source()
@@ -64,6 +74,9 @@ class FortranRoutine(object):
self._dependencies = [d.lower() for d in deps]
return self._dependencies
+ def __repr__(self):
+ return "FortranRoutine({!r}, filename={!r})".format(self.name, self.filename)
+
class UnknownFortranRoutine(FortranRoutine):
"""Wrapper for a Fortran routine for which the corresponding file
is not known.
@@ -188,64 +201,61 @@ def getLapackRoutines(wrapped_routines, ignores, lapack_dir):
return library
def getWrappedRoutineNames(wrapped_routines_file):
- fo = open(wrapped_routines_file)
routines = []
ignores = []
- for line in fo:
- line = line.strip()
- if not line or line.startswith('#'):
- continue
- if line.startswith('IGNORE:'):
- line = line[7:].strip()
- ig = line.split()
- ignores.extend(ig)
- else:
- routines.append(line)
+ with open(wrapped_routines_file) as fo:
+ for line in fo:
+ line = line.strip()
+ if not line or line.startswith('#'):
+ continue
+ if line.startswith('IGNORE:'):
+ line = line[7:].strip()
+ ig = line.split()
+ ignores.extend(ig)
+ else:
+ routines.append(line)
return routines, ignores
+types = {'blas', 'zlapack', 'dlapack'}
+
def dumpRoutineNames(library, output_dir):
- for typename in ['unknown', 'blas', 'dlapack', 'zlapack']:
+ for typename in {'unknown'} | types:
routines = library.allRoutinesByType(typename)
filename = os.path.join(output_dir, typename + '_routines.lst')
- fo = open(filename, 'w')
- for r in routines:
- deps = r.dependencies()
- fo.write('%s: %s\n' % (r.name, ' '.join(deps)))
- fo.close()
+ with open(filename, 'w') as fo:
+ for r in routines:
+ deps = r.dependencies()
+ fo.write('%s: %s\n' % (r.name, ' '.join(deps)))
def concatenateRoutines(routines, output_file):
- output_fo = open(output_file, 'w')
- for r in routines:
- fo = open(r.filename, 'r')
- source = fo.read()
- fo.close()
- output_fo.write(source)
- output_fo.close()
+ with open(output_file, 'w') as output_fo:
+ for r in routines:
+ with open(r.filename, 'r') as fo:
+ source = fo.read()
+ output_fo.write(source)
class F2CError(Exception):
pass
def runF2C(fortran_filename, output_dir):
- # we're assuming no funny business that needs to be quoted for the shell
- cmd = "f2c %s -d %s %s" % (F2C_ARGS, output_dir, fortran_filename)
- rc = os.system(cmd)
- if rc != 0:
+ try:
+ subprocess.check_call(
+ ["f2c"] + F2C_ARGS + ['-d', output_dir, fortran_filename]
+ )
+ except subprocess.CalledProcessError:
raise F2CError
def scrubF2CSource(c_file):
- fo = open(c_file, 'r')
- source = fo.read()
- fo.close()
+ with open(c_file) as fo:
+ source = fo.read()
source = clapack_scrub.scrubSource(source, verbose=True)
- fo = open(c_file, 'w')
- fo.write(HEADER)
- fo.write(source)
- fo.close()
+ with open(c_file, 'w') as fo:
+ fo.write(HEADER)
+ fo.write(source)
def main():
if len(sys.argv) != 4:
- print('Usage: %s wrapped_routines_file lapack_dir output_dir' % \
- (sys.argv[0],))
+ print(__doc__)
return
wrapped_routines_file = sys.argv[1]
lapack_src_dir = sys.argv[2]
@@ -256,11 +266,11 @@ def main():
dumpRoutineNames(library, output_dir)
- for typename in ['blas', 'dlapack', 'zlapack']:
- print('creating %s_lite.c ...' % typename)
- routines = library.allRoutinesByType(typename)
- fortran_file = os.path.join(output_dir, typename+'_lite.f')
+ for typename in types:
+ fortran_file = os.path.join(output_dir, '%s_lite.f' % typename)
c_file = fortran_file[:-2] + '.c'
+ print('creating %s ...' % c_file)
+ routines = library.allRoutinesByType(typename)
concatenateRoutines(routines, fortran_file)
try:
runF2C(fortran_file, output_dir)
@@ -269,5 +279,7 @@ def main():
break
scrubF2CSource(c_file)
+ print()
+
if __name__ == '__main__':
main()