From nobody Tue Apr 30 06:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502224900752872.3490085343632; Tue, 8 Aug 2017 13:41:40 -0700 (PDT) Received: from localhost ([::1]:44423 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBJr-0001GJ-5z for importer@patchew.org; Tue, 08 Aug 2017 16:41:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57514) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBI6-0008Hn-3g for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfBI4-0000MU-S9 for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44190) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfBI4-0000Lp-Jg for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:48 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 98329552E9 for ; Tue, 8 Aug 2017 20:39:47 +0000 (UTC) Received: from localhost (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A0394DA88; Tue, 8 Aug 2017 20:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 98329552E9 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=ehabkost@redhat.com From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 17:39:31 -0300 Message-Id: <20170808203935.30021-2-ehabkost@redhat.com> In-Reply-To: <20170808203935.30021-1-ehabkost@redhat.com> References: <20170808203935.30021-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 08 Aug 2017 20:39:47 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.11 v2 1/5] qmp-shell: Use optparse module X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , Markus Armbruster , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It makes command-line parsing and generation of help text much simpler. The optparse module is deprecated since Python 2.7, but argparse is not available in Python 2.6 (the minimum Python version required for building QEMU). Signed-off-by: Eduardo Habkost Reviewed-by: Stefan Hajnoczi --- Changes v1 -> v2: * Use optparse module, as the minimum Python version for building QEMU is 2.6 * Reported-by: Stefan Hajnoczi * Suggested-by: "Daniel P. Berrange" --- scripts/qmp/qmp-shell | 63 +++++++++++++++++++----------------------------= ---- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index 860ffb2..ad72ef9 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -73,6 +73,7 @@ import sys import os import errno import atexit +import optparse =20 class QMPCompleter(list): def complete(self, text, state): @@ -393,52 +394,34 @@ def die(msg): sys.stderr.write('ERROR: %s\n' % msg) sys.exit(1) =20 -def fail_cmdline(option=3DNone): - if option: - sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % optio= n) - sys.stderr.write('qmp-shell [ -v ] [ -p ] [ -H ] [ -N ] < UNIX socket = path> | < TCP address:port >\n') - sys.stderr.write(' -v Verbose (echo command sent and received)\= n') - sys.stderr.write(' -p Pretty-print JSON\n') - sys.stderr.write(' -H Use HMP interface\n') - sys.stderr.write(' -N Skip negotiate (for qemu-ga)\n') - sys.exit(1) - def main(): - addr =3D '' - qemu =3D None - hmp =3D False - pretty =3D False - verbose =3D False - negotiate =3D True + parser =3D optparse.OptionParser(description=3D'QMP shell utility') + parser.set_usage("%prog [options] | ") + parser.add_option('-v', action=3D'store_true', dest=3D'verbose', + help=3D'Verbose (echo command sent and received)') + parser.add_option('-p', action=3D'store_true', dest=3D'pretty', + help=3D'Pretty-print JSON') + parser.add_option('-H', action=3D'store_true', dest=3D'hmp', + help=3D'Use HMP interface') + parser.add_option('-N', action=3D'store_false', dest=3D'negotiate', + default=3DTrue, help=3D'Skip negotiate (for qemu-ga)') + opts,args =3D parser.parse_args() + + if len(args) !=3D 1: + parser.print_help(sys.stderr) + sys.exit(1) + addr =3D args[0] =20 try: - for arg in sys.argv[1:]: - if arg =3D=3D "-H": - if qemu is not None: - fail_cmdline(arg) - hmp =3D True - elif arg =3D=3D "-p": - pretty =3D True - elif arg =3D=3D "-N": - negotiate =3D False - elif arg =3D=3D "-v": - verbose =3D True - else: - if qemu is not None: - fail_cmdline(arg) - if hmp: - qemu =3D HMPShell(arg) - else: - qemu =3D QMPShell(arg, pretty) - addr =3D arg - - if qemu is None: - fail_cmdline() + if opts.hmp: + qemu =3D HMPShell(addr) + else: + qemu =3D QMPShell(addr, opts.pretty) except QMPShellBadPort: die('bad port number in command-line') =20 try: - qemu.connect(negotiate) + qemu.connect(opts.negotiate) except qmp.QMPConnectError: die('Didn\'t get QMP greeting message') except qmp.QMPCapabilitiesError: @@ -447,7 +430,7 @@ def main(): die('Could not connect to %s' % addr) =20 qemu.show_banner() - qemu.set_verbosity(verbose) + qemu.set_verbosity(opts.verbose) while qemu.read_exec_command(qemu.get_prompt()): pass qemu.close() --=20 2.9.4 From nobody Tue Apr 30 06:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502224910251621.5763098156594; Tue, 8 Aug 2017 13:41:50 -0700 (PDT) Received: from localhost ([::1]:44424 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBK0-0001MZ-Ub for importer@patchew.org; Tue, 08 Aug 2017 16:41:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57729) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBID-0008LW-Aa for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfBIA-0000RY-5E for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35835) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfBI9-0000QK-Ve for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:54 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E273066199 for ; Tue, 8 Aug 2017 20:39:52 +0000 (UTC) Received: from localhost (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0341D60317; Tue, 8 Aug 2017 20:39:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E273066199 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=ehabkost@redhat.com From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 17:39:32 -0300 Message-Id: <20170808203935.30021-3-ehabkost@redhat.com> In-Reply-To: <20170808203935.30021-1-ehabkost@redhat.com> References: <20170808203935.30021-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 08 Aug 2017 20:39:53 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.11 v2 2/5] qmp-shell: Pass split cmdargs to __build_cmd() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , Markus Armbruster , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This will allow us to implement a method to run a command that is already split in a list. Reviewed-by: Stefan Hajnoczi Tested-by: John Snow Reviewed-by: John Snow Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * (none) --- scripts/qmp/qmp-shell | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index ad72ef9..f93c1cf 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -212,15 +212,13 @@ class QMPShell(qmp.QEMUMonitorProtocol): raise QMPShellError('Cannot set "%s" multiple times' %= key) parent[optpath[-1]] =3D value =20 - def __build_cmd(self, cmdline): + def __build_cmd(self, cmdargs): """ Build a QMP input object from a user provided command-line in the following format: =20 < command-name > [ arg-name1=3Darg1 ] ... [ arg-nameN=3DargN ] """ - cmdargs =3D cmdline.split() - # Transactional CLI entry/exit: if cmdargs[0] =3D=3D 'transaction(': self._transmode =3D True @@ -247,7 +245,7 @@ class QMPShell(qmp.QEMUMonitorProtocol): finalize =3D True self.__cli_expr(cmdargs[1:], action['data']) self._actions.append(action) - return self.__build_cmd(')') if finalize else None + return self.__build_cmd([')']) if finalize else None =20 # Standard command: parse and return it to be executed. qmpcmd =3D { 'execute': cmdargs[0], 'arguments': {} } @@ -262,8 +260,9 @@ class QMPShell(qmp.QEMUMonitorProtocol): print str(jsobj) =20 def _execute_cmd(self, cmdline): + cmdargs =3D cmdline.split() try: - qmpcmd =3D self.__build_cmd(cmdline) + qmpcmd =3D self.__build_cmd(cmdargs) except Exception as e: print 'Error while parsing command line: %s' % e print 'command format: ', --=20 2.9.4 From nobody Tue Apr 30 06:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502224912463155.47740745975364; Tue, 8 Aug 2017 13:41:52 -0700 (PDT) Received: from localhost ([::1]:44426 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBK3-0001P9-8u for importer@patchew.org; Tue, 08 Aug 2017 16:41:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57728) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBID-0008LU-AD for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfBIB-0000Sw-Vn for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44726) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfBIB-0000SS-PI for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:55 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BD65F3A92E3 for ; Tue, 8 Aug 2017 20:39:54 +0000 (UTC) Received: from localhost (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 534E960317; Tue, 8 Aug 2017 20:39:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BD65F3A92E3 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=ehabkost@redhat.com From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 17:39:33 -0300 Message-Id: <20170808203935.30021-4-ehabkost@redhat.com> In-Reply-To: <20170808203935.30021-1-ehabkost@redhat.com> References: <20170808203935.30021-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 08 Aug 2017 20:39:54 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.11 v2 3/5] qmp-shell: execute_cmdargs() method X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , Markus Armbruster , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This will allow us to execute a command that was already split in a list. Reviewed-by: Stefan Hajnoczi Tested-by: John Snow Reviewed-by: John Snow Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * (none) --- scripts/qmp/qmp-shell | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index f93c1cf..4b9a420 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -260,7 +260,9 @@ class QMPShell(qmp.QEMUMonitorProtocol): print str(jsobj) =20 def _execute_cmd(self, cmdline): - cmdargs =3D cmdline.split() + return self.execute_cmdargs(cmdline.split()) + + def execute_cmdargs(self, cmdargs): try: qmpcmd =3D self.__build_cmd(cmdargs) except Exception as e: @@ -386,6 +388,9 @@ class HMPShell(QMPShell): print '%s: %s' % (resp['error']['class'], resp['error']['desc'= ]) return True =20 + def execute_cmdargs(self, cmdargs): + return self._execute_cmd(' '.join(cmdargs)) + def show_banner(self): QMPShell.show_banner(self, msg=3D'Welcome to the HMP shell!') =20 --=20 2.9.4 From nobody Tue Apr 30 06:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502225033799497.44479222982056; Tue, 8 Aug 2017 13:43:53 -0700 (PDT) Received: from localhost ([::1]:44433 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBM0-0003AJ-EN for importer@patchew.org; Tue, 08 Aug 2017 16:43:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57791) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBIF-0008Q2-7A for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:40:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfBID-0000Ue-Rs for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:5972) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfBID-0000Te-MN for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:57 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9BD2266199 for ; Tue, 8 Aug 2017 20:39:56 +0000 (UTC) Received: from localhost (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AC0860317; Tue, 8 Aug 2017 20:39:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9BD2266199 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=ehabkost@redhat.com From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 17:39:34 -0300 Message-Id: <20170808203935.30021-5-ehabkost@redhat.com> In-Reply-To: <20170808203935.30021-1-ehabkost@redhat.com> References: <20170808203935.30021-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 08 Aug 2017 20:39:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.11 v2 4/5] qmp-shell: Accept QMP command as argument X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , Markus Armbruster , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is useful for testing QMP commands in scripts. Example usage, combined with 'jq' for filtering the results: $ ./scripts/qmp/qmp-shell /tmp/qmp qom-list path=3D/ | jq -r .return[].na= me machine type chardevs backend $ Signed-off-by: Eduardo Habkost Reviewed-by: Stefan Hajnoczi --- Changes v1 -> v2: * Rewritten using optparse module --- scripts/qmp/qmp-shell | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index 4b9a420..4b7374e 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -400,7 +400,7 @@ def die(msg): =20 def main(): parser =3D optparse.OptionParser(description=3D'QMP shell utility') - parser.set_usage("%prog [options] | ") + parser.set_usage("%prog [options] | [COMMAND [ARG=3DVALUE]...]") parser.add_option('-v', action=3D'store_true', dest=3D'verbose', help=3D'Verbose (echo command sent and received)') parser.add_option('-p', action=3D'store_true', dest=3D'pretty', @@ -411,10 +411,11 @@ def main(): default=3DTrue, help=3D'Skip negotiate (for qemu-ga)') opts,args =3D parser.parse_args() =20 - if len(args) !=3D 1: + if len(args) < 1: parser.print_help(sys.stderr) sys.exit(1) addr =3D args[0] + cmdargs =3D args[1:] =20 try: if opts.hmp: @@ -433,10 +434,13 @@ def main(): except qemu.error: die('Could not connect to %s' % addr) =20 - qemu.show_banner() qemu.set_verbosity(opts.verbose) - while qemu.read_exec_command(qemu.get_prompt()): - pass + if len(cmdargs): + qemu.execute_cmdargs(cmdargs) + else: + qemu.show_banner() + while qemu.read_exec_command(qemu.get_prompt()): + pass qemu.close() =20 if __name__ =3D=3D '__main__': --=20 2.9.4 From nobody Tue Apr 30 06:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15022250382391010.253128995432; Tue, 8 Aug 2017 13:43:58 -0700 (PDT) Received: from localhost ([::1]:44435 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBM5-0003Fw-36 for importer@patchew.org; Tue, 08 Aug 2017 16:43:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57835) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dfBIH-0008Rw-6W for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:40:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dfBIF-0000W5-Rj for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:40:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36914) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dfBIF-0000VX-J8 for qemu-devel@nongnu.org; Tue, 08 Aug 2017 16:39:59 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9D2ADCAA72 for ; Tue, 8 Aug 2017 20:39:58 +0000 (UTC) Received: from localhost (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1972860317; Tue, 8 Aug 2017 20:39:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9D2ADCAA72 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=ehabkost@redhat.com From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Tue, 8 Aug 2017 17:39:35 -0300 Message-Id: <20170808203935.30021-6-ehabkost@redhat.com> In-Reply-To: <20170808203935.30021-1-ehabkost@redhat.com> References: <20170808203935.30021-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 08 Aug 2017 20:39:58 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH for-2.11 v2 5/5] Remove scripts/qmp/qmp X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , Markus Armbruster , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The only purpose of scripts/qmp/qmp was the ability to run QMP commands non-interactively. Now it is possible to run qmp-shell non-interactively by providing a QMP command a command-line argument, making scripts/qmp/qmp obsolete. Reviewed-by: Stefan Hajnoczi Tested-by: John Snow Reviewed-by: John Snow Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * (none) --- scripts/qmp/qmp | 126 ----------------------------------------------------= ---- 1 file changed, 126 deletions(-) delete mode 100755 scripts/qmp/qmp diff --git a/scripts/qmp/qmp b/scripts/qmp/qmp deleted file mode 100755 index 514b539..0000000 --- a/scripts/qmp/qmp +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/python -# -# QMP command line tool -# -# Copyright IBM, Corp. 2011 -# -# Authors: -# Anthony Liguori -# -# This work is licensed under the terms of the GNU GPLv2 or later. -# See the COPYING file in the top-level directory. - -import sys, os -from qmp import QEMUMonitorProtocol - -def print_response(rsp, prefix=3D[]): - if type(rsp) =3D=3D list: - i =3D 0 - for item in rsp: - if prefix =3D=3D []: - prefix =3D ['item'] - print_response(item, prefix[:-1] + ['%s[%d]' % (prefix[-1], i)= ]) - i +=3D 1 - elif type(rsp) =3D=3D dict: - for key in rsp.keys(): - print_response(rsp[key], prefix + [key]) - else: - if len(prefix): - print '%s: %s' % ('.'.join(prefix), rsp) - else: - print '%s' % (rsp) - -def main(args): - path =3D None - - # Use QMP_PATH if it's set - if os.environ.has_key('QMP_PATH'): - path =3D os.environ['QMP_PATH'] - - while len(args): - arg =3D args[0] - - if arg.startswith('--'): - arg =3D arg[2:] - if arg.find('=3D') =3D=3D -1: - value =3D True - else: - arg, value =3D arg.split('=3D', 1) - - if arg in ['path']: - if type(value) =3D=3D str: - path =3D value - elif arg in ['help']: - os.execlp('man', 'man', 'qmp') - else: - print 'Unknown argument "%s"' % arg - - args =3D args[1:] - else: - break - - if not path: - print "QMP path isn't set, use --path=3Dqmp-monitor-address or set= QMP_PATH" - return 1 - - if len(args): - command, args =3D args[0], args[1:] - else: - print 'No command found' - print 'Usage: "qmp [--path=3Dqmp-monitor-address] qmp-cmd argument= s"' - return 1 - - if command in ['help']: - os.execlp('man', 'man', 'qmp') - - srv =3D QEMUMonitorProtocol(path) - srv.connect() - - def do_command(srv, cmd, **kwds): - rsp =3D srv.cmd(cmd, kwds) - if rsp.has_key('error'): - raise Exception(rsp['error']['desc']) - return rsp['return'] - - commands =3D map(lambda x: x['name'], do_command(srv, 'query-commands'= )) - - srv.close() - - if command not in commands: - fullcmd =3D 'qmp-%s' % command - try: - os.environ['QMP_PATH'] =3D path - os.execvp(fullcmd, [fullcmd] + args) - except OSError as exc: - if exc.errno =3D=3D 2: - print 'Command "%s" not found.' % (fullcmd) - return 1 - raise - return 0 - - srv =3D QEMUMonitorProtocol(path) - srv.connect() - - arguments =3D {} - for arg in args: - if not arg.startswith('--'): - print 'Unknown argument "%s"' % arg - return 1 - - arg =3D arg[2:] - if arg.find('=3D') =3D=3D -1: - value =3D True - else: - arg, value =3D arg.split('=3D', 1) - - if arg in ['help']: - os.execlp('man', 'man', 'qmp-%s' % command) - return 1 - - arguments[arg] =3D value - - rsp =3D do_command(srv, command, **arguments) - print_response(rsp) - -if __name__ =3D=3D '__main__': - sys.exit(main(sys.argv[1:])) --=20 2.9.4