提交 fa779b65 编写于 作者: D Daniel P. Berrange 提交者: Luiz Capitulino

Add support for pretty-printing response in qmp-shell

Add a '-p' arg to the QMP/qmp-shell test program, which uses
the python pprint module to pretty-print the dictionary
returned from a command

  $ qmp-shell -p /tmp/qemu
  Welcome to the QMP low-level shell!
  Connected to QEMU 1.1.50

  (QEMU) query-cpus
  {   u'return': [   {   u'CPU': 0,
                         u'current': True,
                         u'halted': True,
                         u'pc': 1048556,
                         u'thread_id': 7108}]}
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
Signed-off-by: NLuiz Capitulino <lcapitulino@redhat.com>
上级 f45ddd14
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
import qmp import qmp
import readline import readline
import sys import sys
import pprint
class QMPCompleter(list): class QMPCompleter(list):
def complete(self, text, state): def complete(self, text, state):
...@@ -52,10 +53,11 @@ class QMPShellBadPort(QMPShellError): ...@@ -52,10 +53,11 @@ class QMPShellBadPort(QMPShellError):
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
# _execute_cmd()). Let's design a better one. # _execute_cmd()). Let's design a better one.
class QMPShell(qmp.QEMUMonitorProtocol): class QMPShell(qmp.QEMUMonitorProtocol):
def __init__(self, address): def __init__(self, address, pp=None):
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
self._greeting = None self._greeting = None
self._completer = None self._completer = None
self._pp = pp
def __get_address(self, arg): def __get_address(self, arg):
""" """
...@@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol): ...@@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
if resp is None: if resp is None:
print 'Disconnected' print 'Disconnected'
return False return False
print resp
if self._pp is not None:
self._pp.pprint(resp)
else:
print resp
return True return True
def connect(self): def connect(self):
...@@ -222,22 +228,36 @@ def die(msg): ...@@ -222,22 +228,36 @@ def die(msg):
def fail_cmdline(option=None): def fail_cmdline(option=None):
if option: if option:
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option) sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
sys.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n') sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
sys.exit(1) sys.exit(1)
def main(): def main():
addr = '' addr = ''
qemu = None
hmp = False
pp = None
try: try:
if len(sys.argv) == 2: for arg in sys.argv[1:]:
qemu = QMPShell(sys.argv[1]) if arg == "-H":
addr = sys.argv[1] if qemu is not None:
elif len(sys.argv) == 3: fail_cmdline(arg)
if sys.argv[1] != '-H': hmp = True
fail_cmdline(sys.argv[1]) elif arg == "-p":
qemu = HMPShell(sys.argv[2]) if pp is not None:
addr = sys.argv[2] fail_cmdline(arg)
else: pp = pprint.PrettyPrinter(indent=4)
fail_cmdline() else:
if qemu is not None:
fail_cmdline(arg)
if hmp:
qemu = HMPShell(arg)
else:
qemu = QMPShell(arg, pp)
addr = arg
if qemu is None:
fail_cmdline()
except QMPShellBadPort: except QMPShellBadPort:
die('bad port number in command-line') die('bad port number in command-line')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册