aboutsummaryrefslogtreecommitdiff
path: root/scripts/gen-crashhandler-default-go.py
blob: e080ed9943b876ac906ab15dde47c530d7280205 (plain)
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
#!/usr/bin/env python3

import sys
import argparse
import kconfiglib
import re

def findItem(node, name):
    while node:
        if isinstance(node.item, kconfiglib.Symbol):
            if node.item.name == name:
                return node.item.str_value

        if node.list:
            found = findItem(node.list, name)
            if found is not None:
                return found

        node = node.next

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--kconfig", nargs="?", default="Kconfig", help="Top-level Kconfig")
    parser.add_argument("--dotconfig", nargs="?", help=".config")
    parser.add_argument("--input", type=argparse.FileType('r'), help="default.go.in")
    parser.add_argument("--output", type=argparse.FileType('w'), default=sys.stdout, help="Output location")
    args = parser.parse_args()

    kconf = kconfiglib.Kconfig(args.kconfig)
    if args.dotconfig is not None:
        kconf.load_config(args.dotconfig)

    marker = re.compile('.+(@(\w+)@)')
    for line in args.input:
        found = marker.match(line)
        if found:
            print(str.replace(line, found.group(1), findItem(kconf.top_node, found.group(2))), end='', file=args.output)
        else:
            print(line, end='', file=args.output)