aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/python/gen_includes.py
blob: 0e024cc49d8b9959e8a6fa985267e8f145a92d76 (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
import sys
import os
import errno
from os import listdir
from os.path import dirname, relpath, join


def ensure_dir_exists(path):
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise


def make_dir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)


def files(directory):
    for dirpath, dirnames, filenames in os.walk(directory):
        for name in filenames:
            yield relpath(join(dirpath, name), directory)


def headers_set(directory):
    return {
        f for f in files(directory)
        if f.endswith('.h') and not f.startswith('internal/')
    }


if __name__ == "__main__":

    python2_path = sys.argv[1]
    python3_path = sys.argv[2]
    output_path = sys.argv[3]

    ensure_dir_exists(join('.', python2_path))
    ensure_dir_exists(join('.', python3_path))

    only_headers2 = headers_set(python2_path)
    only_headers3 = headers_set(python3_path)
    all_headers = only_headers2 | only_headers3

    for header in all_headers:
        path = join(output_path, header)
        make_dir(dirname(path))
        f = open(path, 'w')
        f.write('#pragma once\n\n')
        f.write('#ifdef USE_PYTHON3\n')
        if (header in only_headers3):
            f.write('#include <' + join(python3_path, header) + '>\n')
        else:
            f.write('#error "No <' + header + '> in Python3"\n')
        f.write('#else\n')
        if (header in only_headers2):
            f.write('#include <' + join(python2_path, header) + '>\n')
        else:
            f.write('#error "No <' + header + '> in Python2"\n')
        f.write('#endif\n')