-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctest_extmods.py
executable file
·61 lines (48 loc) · 1.68 KB
/
doctest_extmods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""Run doctests in extension modules of <pkg_name>
Collect extension modules in <pkg_name>
Run doctests in each extension module
Examples
----------
%prog dipy
"""
import sys
import os
from os.path import dirname, relpath, sep, join as pjoin, abspath
from distutils.sysconfig import get_config_vars
import doctest
from optparse import OptionParser
EXT_EXT = get_config_vars('SO')[0]
def get_ext_modules(pkg_name):
pkg = __import__(pkg_name, fromlist=[''])
pkg_dir = abspath(dirname(pkg.__file__))
# pkg_root = __import__(pkg_name)
ext_modules = []
for dirpath, dirnames, filenames in os.walk(pkg_dir):
reldir = relpath(dirpath, pkg_dir)
if reldir == '.':
reldir = ''
for filename in filenames:
if filename.endswith(EXT_EXT):
froot = filename[:-len(EXT_EXT)]
mod_path = pjoin(reldir, froot)
mod_uri = pkg_name + '.' + mod_path.replace(sep, '.')
# fromlist=[''] results in submodule being returned, rather
# than the top level module. See help(__import__)
mod = __import__(mod_uri, fromlist=[''])
ext_modules.append(mod)
return ext_modules
def main():
usage = "usage: %prog [options] <pkg_name>\n\n" + __doc__
parser = OptionParser(usage=usage)
opts, args = parser.parse_args()
if len(args) == 0:
parser.print_help()
sys.exit(1)
mod_name = args[0]
mods = get_ext_modules(mod_name)
for mod in mods:
print("Testing module: " + mod.__name__)
doctest.testmod(mod, optionflags=doctest.NORMALIZE_WHITESPACE)
if __name__ == '__main__':
main()