Skip to content

Commit 461519b

Browse files
committed
Merge pull request pymssql#278 from pymssql/issue273
setup.py: Remove extra files created during build.
2 parents dd42d5e + e8090aa commit 461519b

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

setup.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
ROOT = osp.abspath(osp.dirname(__file__))
6464

6565
def fpath(*parts):
66-
"""Return fully qualified path for parts, e.g. fpath('a', 'b') -> '<this dir>/a/b'"""
66+
"""
67+
Return fully qualified path for parts, e.g.
68+
fpath('a', 'b') -> '<this dir>/a/b'
69+
"""
6770
return osp.join(ROOT, *parts)
6871

6972
have_c_files = osp.exists(fpath('_mssql.c')) and osp.exists(fpath('pymssql.c'))
@@ -82,6 +85,7 @@ def fpath(*parts):
8285
Distribution(dict(setup_requires='Cython>=0.19.1'))
8386

8487
from Cython.Distutils import build_ext as _build_ext
88+
from distutils.dir_util import remove_tree
8589
import struct
8690

8791
def extract_version():
@@ -94,6 +98,24 @@ def extract_version():
9498

9599
return version
96100

101+
@contextlib.contextmanager
102+
def fs_cleanup(files=None, dirs=None):
103+
"""
104+
A context manager to remove ``files`` and ``dirs`` from the
105+
source tree. Useful to cleanup anciliary intermediate files.
106+
"""
107+
yield
108+
if files:
109+
for fname in files:
110+
path = fpath(fname)
111+
if osp.exists(path):
112+
os.remove(path)
113+
if dirs:
114+
for dname in dirs:
115+
path = fpath(dname)
116+
if osp.exists(path):
117+
remove_tree(path)
118+
97119
@contextlib.contextmanager
98120
def stdchannel_redirected(stdchannel, dest_filename):
99121
"""
@@ -179,9 +201,10 @@ def add_dir_if_exists(filtered_dirs, *dirs):
179201

180202
libraries = ['sybdb']
181203

182-
with stdchannel_redirected(sys.stderr, os.devnull):
183-
if compiler.has_function('clock_gettime', libraries=['rt']):
184-
libraries.append('rt')
204+
with fs_cleanup(files=['a.out'], dirs=['tmp']):
205+
with stdchannel_redirected(sys.stderr, os.devnull):
206+
if compiler.has_function('clock_gettime', libraries=['rt']):
207+
libraries.append('rt')
185208

186209
usr_local = '/usr/local'
187210
if osp.exists(usr_local):
@@ -274,6 +297,7 @@ def build_extensions(self):
274297
e.libraries.extend(libraries)
275298
_build_ext.build_extensions(self)
276299

300+
277301
class clean(_clean):
278302
"""
279303
Subclass clean so it removes all the Cython generated files.

0 commit comments

Comments
 (0)