Skip to content

Commit e079de5

Browse files
committed
Merged in dalcinl/setup-clean (pull request #12)
setup: Add proper implementation of clean command Approved-by: Mikael Mortensen <[email protected]>
2 parents 074a9b5 + 244a184 commit e079de5

File tree

1 file changed

+76
-12
lines changed

1 file changed

+76
-12
lines changed

setup.py

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sysconfig
88
from distutils import ccompiler
99
from setuptools import setup
10+
from setuptools.dist import Distribution
1011
from setuptools.extension import Extension
1112
import numpy
1213

@@ -73,8 +74,10 @@ def get_fftw_libs():
7374
assert len(libs) > 0, "No FFTW libraries found in {}".format(library_dirs)
7475
return libs
7576

76-
def generate_extensions(fftwlibs):
77+
def generate_extensions(fftwlibs, force=True):
7778
"""Generate files with float and long double"""
79+
from distutils.dep_util import newer_group
80+
7881
for d in fftwlibs:
7982
if d == 'double':
8083
continue
@@ -87,21 +90,52 @@ def generate_extensions(fftwlibs):
8790
):
8891
src = os.path.join(fftwdir, fname)
8992
dst = os.path.join(fftwdir, fname.replace('fftw_', p))
90-
with open(src, 'r') as fin:
91-
code = fin.read()
92-
code = re.sub('fftw_', p, code)
93-
code = re.sub('double', d, code)
94-
with open(dst, 'w') as fout:
95-
fout.write(code)
96-
97-
def get_extensions(fftwlibs):
93+
if force or newer_group([src], dst, 'newer'):
94+
with open(src, 'r') as fin:
95+
code = fin.read()
96+
code = re.sub('fftw_', p, code)
97+
code = re.sub('double', d, code)
98+
with open(dst, 'w') as fout:
99+
fout.write(code)
100+
101+
def remove_extensions(fftwlibs):
102+
"""Remove generated files"""
103+
for fname in (
104+
'utilities.c',
105+
'fftw_xfftn.c',
106+
'fftwf_xfftn.c',
107+
'fftwl_xfftn.c',
108+
):
109+
dst = os.path.join(fftwdir, fname)
110+
try:
111+
os.remove(dst)
112+
except OSError:
113+
pass
114+
for d in fftwlibs:
115+
if d == 'double':
116+
continue
117+
p = 'fftw'+prec_map[d]+'_'
118+
for fname in (
119+
'fftw_planxfftn.h',
120+
'fftw_planxfftn.c',
121+
'fftw_xfftn.pyx',
122+
'fftw_xfftn.pxd',
123+
):
124+
dst = os.path.join(fftwdir, fname.replace('fftw_', p))
125+
try:
126+
os.remove(dst)
127+
except OSError:
128+
pass
129+
130+
def get_extensions():
98131
"""Return list of extension modules"""
99132
include_dirs = get_include_dirs()
100133
library_dirs = get_library_dirs()
101134
ext = [Extension("mpi4py_fft.fftw.utilities",
102135
sources=[os.path.join(fftwdir, "utilities.pyx")],
103136
include_dirs=include_dirs)]
104137

138+
fftwlibs = get_fftw_libs()
105139
for d, libs in fftwlibs.items():
106140
p = 'fftw'+prec_map[d]+'_'
107141
ext.append(Extension("mpi4py_fft.fftw.{}xfftn".format(p),
@@ -113,6 +147,37 @@ def get_extensions(fftwlibs):
113147
library_dirs=library_dirs))
114148
return ext
115149

150+
151+
class Dist(Distribution):
152+
153+
def get_command_class(self, command):
154+
get_command_class = Distribution.get_command_class
155+
156+
if 'build_ext' not in self.cmdclass:
157+
_build_ext = get_command_class(self, 'build_ext')
158+
159+
class build_ext(_build_ext):
160+
def run(self):
161+
fftw_libs = get_fftw_libs()
162+
generate_extensions(fftw_libs, self.force)
163+
_build_ext.run(self)
164+
165+
self.cmdclass['build_ext'] = build_ext
166+
167+
if 'clean' not in self.cmdclass:
168+
_clean = get_command_class(self, 'clean')
169+
170+
class clean(_clean):
171+
def run(self):
172+
fftw_libs = get_fftw_libs()
173+
remove_extensions(fftw_libs)
174+
_clean.run(self)
175+
176+
self.cmdclass['clean'] = clean
177+
178+
return get_command_class(self, command)
179+
180+
116181
def version():
117182
srcdir = os.path.join(cwd, 'mpi4py_fft')
118183
with open(os.path.join(srcdir, '__init__.py')) as f:
@@ -123,8 +188,6 @@ def version():
123188
long_description = fh.read()
124189

125190
if __name__ == '__main__':
126-
fftw_libs = get_fftw_libs()
127-
generate_extensions(fftw_libs)
128191
setup(name="mpi4py-fft",
129192
version=version(),
130193
description="mpi4py-fft -- Parallel Fast Fourier Transforms (FFTs) using MPI for Python",
@@ -147,7 +210,8 @@ def version():
147210
'Topic :: Scientific/Engineering :: Mathematics',
148211
'Topic :: Software Development :: Libraries :: Python Modules',
149212
],
150-
ext_modules=get_extensions(fftw_libs),
213+
distclass=Dist,
214+
ext_modules=get_extensions(),
151215
install_requires=["mpi4py", "numpy"],
152216
setup_requires=["setuptools>=18.0", "cython>=0.25"],
153217
keywords=['Python', 'FFTW', 'FFT', 'DCT', 'DST', 'MPI']

0 commit comments

Comments
 (0)