aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/numpy/include_gen.py
blob: 60d187f8142e792892440007e5dcfaa8f1aa80e8 (plain) (blame)
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
62
63
64
65
#!/usr/bin/env python3
import os
import shutil
from os.path import dirname, exists, join, relpath

template = '''\
#ifdef USE_PYTHON3
#{}include <{}>
#else
#{}include <{}>
#endif
'''


def main():
    os.chdir(dirname(__file__))
    if exists('include'):
        shutil.rmtree('include')
    include_gen('contrib/python/numpy', ['numpy'])


def include_gen(root, subpaths):
    for path in list_subpaths(subpaths):
        out = join('include', path)
        py2 = join('py2', path)
        py3 = join('py3', path)
        makedir(dirname(out))
        with open(out, 'w') as f:
            f.write(template.format(
                '' if exists(py3) else 'error #',
                join(root, py3),
                '' if exists(py2) else 'error #',
                join(root, py2),
            ))


def is_header(s):
    return s.endswith(('.h', '.hpp'))


def list_subpaths(subpaths, roots=('py2', 'py3'), test=is_header):
    seen = set()
    for root in roots:
        for subpath in subpaths:
            for dirpath, _, filenames in os.walk(join(root, subpath)):
                rootrel = relpath(dirpath, root)
                for filename in filenames:
                    if test(filename):
                        seen.add(join(rootrel, filename))
                    if dirpath.endswith('numpy/core/src/umath') and filename == 'funcs.inc':
                        seen.add(join(rootrel, filename))
                    if dirpath.endswith('numpy/core/include/numpy') and filename in ('__multiarray_api.c', '__ufunc_api.c', '__umath_generated.c'):
                        seen.add(join(rootrel, filename))
                    if filename.endswith(('.dispatch.c', '.dispatch.cpp')):
                        seen.add(join(rootrel, filename))
    return seen


def makedir(path):
    if not exists(path):
        os.makedirs(path)


if __name__ == '__main__':
    main()