aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/python/gen_includes.py
blob: 89306ee8bd70857591b543507c3d34434131aa17 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3

import pathlib
import re


def files(directory):
    for dirpath, dirnames, filenames in directory.walk():
        for name in filenames:
            yield str((dirpath / name).relative_to(directory))


def headers_set(directory):
    return {
        f
        for f in files(directory)
        if f.endswith(".h")
        and (not f.startswith("internal/") or f.startswith("internal/pycore_frame.h"))
        and not re.match(r"^pyconfig[.-].+\.h$", f)
    }


def write_headers(
    all_headers,
    only_headers2,
    only_headers3,
    arcadia_root,
    output_path,
    python2_path,
    python3_path,
    define,
):
    for header in all_headers:
        path = output_path / header
        path.parent.mkdir(parents=True, exist_ok=True)
        with path.open("w") as f:
            f.write("#pragma once\n\n")
            f.write(f"#ifdef {define}\n")
            if header in only_headers3:
                f.write(f"#include <{(python3_path / header).relative_to(arcadia_root)}>\n")
            else:
                f.write(f'#error "No <{header}> in Python3"\n')
            f.write("#else\n")
            if header in only_headers2:
                f.write(f"#include <{(python2_path / header).relative_to(arcadia_root)}>\n")
            else:
                f.write(f'#error "No <{header}> in Python2"\n')
            f.write("#endif\n")


if __name__ == "__main__":
    cur_dir = pathlib.Path.cwd()
    arcadia_root = cur_dir.parent.parent.parent

    python2_path = arcadia_root / "contrib" / "tools" / "python" / "src" / "Include"
    python3_path = arcadia_root / "contrib" / "tools" / "python3" / "Include"
    output_path = arcadia_root / "contrib" / "libs" / "python" / "Include"

    python3_prev_path = arcadia_root / "contrib" / "tools" / "python3_prev" / "Include"
    output_prev_path = arcadia_root / "contrib" / "libs" / "python" / "Include_prev"

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

    all_headers = only_headers2 | only_headers3

    write_headers(
        all_headers,
        only_headers2,
        only_headers3,
        arcadia_root,
        output_path,
        python2_path,
        python3_path,
        "USE_PYTHON3",
    )

    if python3_prev_path.exists():
        only_headers3_prev = headers_set(python3_prev_path)
        all_headers_prev = only_headers2 | only_headers3_prev

        write_headers(
            all_headers_prev,
            only_headers2,
            only_headers3_prev,
            arcadia_root,
            output_prev_path,
            python2_path,
            python3_prev_path,
            "USE_PYTHON3_PREV",
        )