aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/cpp_proto_wrapper.py
blob: c29ca53a1def81f3c343d62ed85508789e901738 (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
import sys
import os
import subprocess
import re
import argparse


FROM_RE = re.compile(r"((?:struct|class)\s+\S+\s+)final\s*:")
TO_RE = r"\1:"


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument('--outputs', nargs='+', required=True)
    parser.add_argument('subcommand', nargs='+')
    return parser.parse_args()


def patch_proto_file(text: str) -> tuple[str, int]:
    return re.subn(FROM_RE, TO_RE, text)


def main(namespace: argparse.Namespace) -> int:
    try:
        subprocess.check_output(namespace.subcommand, stdin=None, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        sys.stderr.write(
            '{} returned non-zero exit code {}.\n{}\n'.format(' '.join(e.cmd), e.returncode, e.output.decode('utf-8', errors='ignore'))
        )
        return e.returncode

    for output in namespace.outputs:
        with open(output, 'rt', encoding="utf-8") as f:
            patched_text, num_patches = patch_proto_file(f.read())
        if num_patches:
            with open(output, 'wt', encoding="utf-8") as f:
                f.write(patched_text)

    return 0


if __name__ == '__main__':
    sys.exit(main(parse_args()))