From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
"Christian Borntraeger" <borntraeger@linux.ibm.com>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
qemu-arm@nongnu.org,
"Vladimir Sementsov-Ogievskiy" <v.sementsov-og@mail.ru>,
"Ani Sinha" <anisinha@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Beraldo Leal" <bleal@redhat.com>,
qemu-s390x@nongnu.org, "Peter Xu" <peterx@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Markus Armbruster" <armbru@redhat.com>,
"Hanna Reitz" <hreitz@redhat.com>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>,
"John Snow" <jsnow@redhat.com>, "Eric Blake" <eblake@redhat.com>,
"Aurelien Jarno" <aurelien@aurel32.net>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Fabiano Rosas" <farosas@suse.de>,
qemu-block@nongnu.org, "Eduardo Habkost" <eduardo@habkost.net>,
"Juan Quintela" <quintela@redhat.com>,
"Leonardo Bras" <leobras@redhat.com>,
"Joel Stanley" <joel@jms.id.au>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Andrew Jeffery" <andrew@aj.id.au>,
"Cédric Le Goater" <clg@kaod.org>,
"Eric Farman" <farman@linux.ibm.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Halil Pasic" <pasic@linux.ibm.com>,
"Cleber Rosa" <crosa@redhat.com>
Subject: [PULL 24/25] scripts: add python_qmp_updater.py
Date: Fri, 13 Oct 2023 15:09:39 -0400 [thread overview]
Message-ID: <20231013190941.3699288-25-jsnow@redhat.com> (raw)
In-Reply-To: <20231013190941.3699288-1-jsnow@redhat.com>
From: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
A script, to update the pattern
result = self.vm.qmp(...)
self.assert_qmp(result, 'return', {})
(and some similar ones) into
self.vm.cmd(...)
Used in the next commit
"python: use vm.cmd() instead of vm.qmp() where appropriate"
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 20231006154125.1068348-15-vsementsov@yandex-team.ru
Signed-off-by: John Snow <jsnow@redhat.com>
---
scripts/python_qmp_updater.py | 136 ++++++++++++++++++++++++++++++++++
1 file changed, 136 insertions(+)
create mode 100755 scripts/python_qmp_updater.py
diff --git a/scripts/python_qmp_updater.py b/scripts/python_qmp_updater.py
new file mode 100755
index 0000000000..494a169812
--- /dev/null
+++ b/scripts/python_qmp_updater.py
@@ -0,0 +1,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)
--
2.41.0
next prev parent reply other threads:[~2023-10-13 19:18 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-13 19:09 [PULL 00/25] Python patches John Snow
2023-10-13 19:09 ` [PULL 01/25] python/machine: move socket setup out of _base_args property John Snow
2023-10-13 19:09 ` [PULL 02/25] python/machine: close sock_pair in cleanup path John Snow
2023-10-13 19:09 ` [PULL 03/25] python/console_socket: accept existing FD in initializer John Snow
2023-10-13 19:09 ` [PULL 04/25] python/machine: use socketpair() for console connections John Snow
2023-10-13 19:09 ` [PULL 05/25] python/machine: use socketpair() for qtest connection John Snow
2023-10-13 19:09 ` [PULL 06/25] python/machine: remove unused sock_dir argument John Snow
2023-10-13 19:09 ` [PULL 07/25] Python/iotests: Add type hint for nbd module John Snow
2023-10-13 19:09 ` [PULL 08/25] python/qmp: remove Server.wait_closed() call for Python 3.12 John Snow
2023-10-13 19:09 ` [PULL 09/25] configure: fix error message to say Python 3.8 John Snow
2023-10-13 19:09 ` [PULL 10/25] Python: Enable python3.12 support John Snow
2023-10-13 19:09 ` [PULL 11/25] python/qemu/qmp/legacy: cmd(): drop cmd_id unused argument John Snow
2023-10-13 19:09 ` [PULL 12/25] qmp_shell.py: _fill_completion() use .command() instead of .cmd() John Snow
2023-10-13 19:09 ` [PULL 13/25] scripts/cpu-x86-uarch-abi.py: " John Snow
2023-10-13 19:09 ` [PULL 14/25] python: rename QEMUMonitorProtocol.cmd() to cmd_raw() John Snow
2023-10-13 19:09 ` [PULL 15/25] python/qemu: rename command() to cmd() John Snow
2023-10-13 19:09 ` [PULL 16/25] python/machine.py: upgrade vm.cmd() method John Snow
2023-10-13 19:09 ` [PULL 17/25] iotests: QemuStorageDaemon: add cmd() method like in QEMUMachine John Snow
2023-10-13 19:09 ` [PULL 18/25] iotests: add some missed checks of qmp result John Snow
2023-10-13 19:09 ` [PULL 19/25] iotests: refactor some common qmp result checks into generic pattern John Snow
2023-10-13 19:09 ` [PULL 20/25] iotests: drop some extra semicolons John Snow
2023-10-13 19:09 ` [PULL 21/25] iotests: drop some extra ** in qmp() call John Snow
2023-10-13 19:09 ` [PULL 22/25] iotests.py: pause_job(): drop return value John Snow
2023-10-13 19:09 ` [PULL 23/25] tests/vm/basevm.py: use cmd() instead of qmp() John Snow
2023-10-13 19:09 ` John Snow [this message]
2023-10-13 19:09 ` [PULL 25/25] python: use vm.cmd() instead of vm.qmp() where appropriate John Snow
2023-10-16 19:21 ` [PULL 00/25] Python patches Stefan Hajnoczi
2023-10-16 23:44 ` John Snow
2023-10-17 11:06 ` Vladimir Sementsov-Ogievskiy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231013190941.3699288-25-jsnow@redhat.com \
--to=jsnow@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=andrew@aj.id.au \
--cc=anisinha@redhat.com \
--cc=armbru@redhat.com \
--cc=aurelien@aurel32.net \
--cc=bleal@redhat.com \
--cc=borntraeger@linux.ibm.com \
--cc=clg@kaod.org \
--cc=crosa@redhat.com \
--cc=eblake@redhat.com \
--cc=eduardo@habkost.net \
--cc=farman@linux.ibm.com \
--cc=farosas@suse.de \
--cc=hreitz@redhat.com \
--cc=joel@jms.id.au \
--cc=kwolf@redhat.com \
--cc=leobras@redhat.com \
--cc=mst@redhat.com \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=quintela@redhat.com \
--cc=thuth@redhat.com \
--cc=v.sementsov-og@mail.ru \
--cc=vsementsov@yandex-team.ru \
--cc=wainersm@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).