aboutsummaryrefslogtreecommitdiff
path: root/scripts/python_qmp_updater.py
blob: 494a1698124a6c90bb51c28b6dacde1ab3f43f20 (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
#
# Intended usage:
#
# git grep -l '\.qmp(' | xargs ./scripts/python_qmp_updater.py
#

import re
import sys
from typing import Optional

start_reg = re.compile(r'^(?P<padding> *)(?P<res>\w+) = (?P<vm>.*).qmp\(',
                       flags=re.MULTILINE)

success_reg_templ = re.sub('\n *', '', r"""
    (\n*{padding}(?P<comment>\#.*$))?
    \n*{padding}
    (
        self.assert_qmp\({res},\ 'return',\ {{}}\)
    |
        assert\ {res}\['return'\]\ ==\ {{}}
    |
        assert\ {res}\ ==\ {{'return':\ {{}}}}
    |
        self.assertEqual\({res}\['return'\],\ {{}}\)
    )""")

some_check_templ = re.sub('\n *', '', r"""
    (\n*{padding}(?P<comment>\#.*$))?
    \s*self.assert_qmp\({res},""")


def tmatch(template: str, text: str,
           padding: str, res: str) -> Optional[re.Match[str]]:
    return re.match(template.format(padding=padding, res=res), text,
                    flags=re.MULTILINE)


def find_closing_brace(text: str, start: int) -> int:
    """
    Having '(' at text[start] search for pairing ')' and return its index.
    """
    assert text[start] == '('

    height = 1

    for i in range(start + 1, len(text)):
        if text[i] == '(':
            height += 1
        elif text[i] == ')':
            height -= 1
        if height == 0:
            return i

    raise ValueError


def update(text: str) -> str:
    result = ''

    while True:
        m = start_reg.search(text)
        if m is None:
            result += text
            break

        result += text[:m.start()]

        args_ind = m.end()
        args_end = find_closing_brace(text, args_ind - 1)

        all_args = text[args_ind:args_end].split(',', 1)

        name = all_args[0]
        args = None if len(all_args) == 1 else all_args[1]

        unchanged_call = text[m.start():args_end+1]
        text = text[args_end+1:]

        padding, res, vm = m.group('padding', 'res', 'vm')

        m = tmatch(success_reg_templ, text, padding, res)

        if m is None:
            result += unchanged_call

            if ('query-' not in name and
                    'x-debug-block-dirty-bitmap-sha256' not in name and
                    not tmatch(some_check_templ, text, padding, res)):
                print(unchanged_call + text[:200] + '...\n\n')

            continue

        if m.group('comment'):
            result += f'{padding}{m.group("comment")}\n'

        result += f'{padding}{vm}.cmd({name}'

        if args:
            result += ','

            if '\n' in args:
                m_args = re.search('(?P<pad> *).*$', args)
                assert m_args is not None

                cur_padding = len(m_args.group('pad'))
                expected = len(f'{padding}{res} = {vm}.qmp(')
                drop = len(f'{res} = ')
                if cur_padding == expected - 1:
                    # tolerate this bad style
                    drop -= 1
                elif cur_padding < expected - 1:
                    # assume nothing to do
                    drop = 0

                if drop:
                    args = re.sub('\n' + ' ' * drop, '\n', args)

            result += args

        result += ')'

        text = text[m.end():]

    return result


for fname in sys.argv[1:]:
    print(fname)
    with open(fname) as f:
        t = f.read()

    t = update(t)

    with open(fname, 'w') as f:
        f.write(t)