dhparser_cythonize.py 1.86 KB
Newer Older
eckhart's avatar
eckhart committed
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
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# build.py - support for building cython extensions with poetry


import os
import sys
from setuptools import setup


try:
    from Cython.Build import cythonize
    has_cython = True
except ImportError:
    has_cython = False
    def cythonize(filename, **options):
        return []


cythonize_modules = [
    'DHParser/stringview.py',
    'DHParser/toolkit.py',
    'DHParser/preprocess.py',
    'DHParser/error.py',
    'DHParser/syntaxtree.py',
    'DHParser/log.py',
    'DHParser/parse.py',
    'DHParser/trace.py',
    'DHParser/transform.py',
    'DHParser/compile.py',
    'DHParser/ebnf.py',
]


def build(setup_kwargs={}):
    if has_cython:

        # find path of DHParser-module
        scriptdir = os.path.dirname(os.path.abspath(__file__))
        i = scriptdir.find('DHParser')
        k = len('DHParser-submodule') if scriptdir[i:].startswith('DHParser-submodule') \
            else len('DHParser')
        if i >= 0:
            dhparserdir = scriptdir[:i + k]
            if dhparserdir not in sys.path:
                sys.path.append(dhparserdir)
        else:
            dhparserdir = ''

        save_cwd = os.getcwd()
        os.chdir(dhparserdir)

        # delete stale c and object-files
        for name in os.listdir(dhparserdir):
            if name.endswith('.c') or name.endswith('.pyd') or name.endswith('.so'):
                os.remove(name)

        # build cyhton modules inplace
        sys.argv.extend(['build_ext', '--inplace'])
        setup(
            name='DHParser',
            ext_modules=cythonize(cythonize_modules, nthreads=0, annotate=False),
            zip_safe=False,
        )

        os.chdir(save_cwd)

    else:
        print('DHParser.scripts.build_cython: Cannot build Cython modules, '
              'because Cython has not installed!\nTry running: pip install cython')


if __name__ == "__main__":
    build()