5 # git grep -l '\.qmp(' | xargs ./scripts/python_qmp_updater.py
10 from typing
import Optional
12 start_reg
= re
.compile(r
'^(?P<padding> *)(?P<res>\w+) = (?P<vm>.*).qmp\(',
15 success_reg_templ
= re
.sub('\n *', '', r
"""
16 (\n*{padding}(?P<comment>\#.*$))?
19 self.assert_qmp\({res},\ 'return',\ {{}}\)
21 assert\ {res}\['return'\]\ ==\ {{}}
23 assert\ {res}\ ==\ {{'return':\ {{}}}}
25 self.assertEqual\({res}\['return'\],\ {{}}\)
28 some_check_templ
= re
.sub('\n *', '', r
"""
29 (\n*{padding}(?P<comment>\#.*$))?
30 \s*self.assert_qmp\({res},""")
33 def tmatch(template
: str, text
: str,
34 padding
: str, res
: str) -> Optional
[re
.Match
[str]]:
35 return re
.match(template
.format(padding
=padding
, res
=res
), text
,
39 def find_closing_brace(text
: str, start
: int) -> int:
41 Having '(' at text[start] search for pairing ')' and return its index.
43 assert text
[start
] == '('
47 for i
in range(start
+ 1, len(text
)):
58 def update(text
: str) -> str:
62 m
= start_reg
.search(text
)
67 result
+= text
[:m
.start()]
70 args_end
= find_closing_brace(text
, args_ind
- 1)
72 all_args
= text
[args_ind
:args_end
].split(',', 1)
75 args
= None if len(all_args
) == 1 else all_args
[1]
77 unchanged_call
= text
[m
.start():args_end
+1]
78 text
= text
[args_end
+1:]
80 padding
, res
, vm
= m
.group('padding', 'res', 'vm')
82 m
= tmatch(success_reg_templ
, text
, padding
, res
)
85 result
+= unchanged_call
87 if ('query-' not in name
and
88 'x-debug-block-dirty-bitmap-sha256' not in name
and
89 not tmatch(some_check_templ
, text
, padding
, res
)):
90 print(unchanged_call
+ text
[:200] + '...\n\n')
94 if m
.group('comment'):
95 result
+= f
'{padding}{m.group("comment")}\n'
97 result
+= f
'{padding}{vm}.cmd({name}'
103 m_args
= re
.search('(?P<pad> *).*$', args
)
104 assert m_args
is not None
106 cur_padding
= len(m_args
.group('pad'))
107 expected
= len(f
'{padding}{res} = {vm}.qmp(')
108 drop
= len(f
'{res} = ')
109 if cur_padding
== expected
- 1:
110 # tolerate this bad style
112 elif cur_padding
< expected
- 1:
113 # assume nothing to do
117 args
= re
.sub('\n' + ' ' * drop
, '\n', args
)
123 text
= text
[m
.end():]
128 for fname
in sys
.argv
[1:]:
130 with
open(fname
) as f
:
135 with
open(fname
, 'w') as f
: