diff options
author | Peter Maydell | 2017-09-16 15:36:16 +0200 |
---|---|---|
committer | Peter Maydell | 2017-09-16 15:36:16 +0200 |
commit | 5ee53d1593dfc071275b13b1228c70bb88f4aaee (patch) | |
tree | 1e58882bef9497fe270f52417759fad69884162a /scripts/qmp | |
parent | Merge remote-tracking branch 'remotes/stefanha/tags/tracing-pull-request' int... (diff) | |
parent | qemu.py: include debug information on launch error (diff) | |
download | qemu-5ee53d1593dfc071275b13b1228c70bb88f4aaee.tar.gz qemu-5ee53d1593dfc071275b13b1228c70bb88f4aaee.tar.xz qemu-5ee53d1593dfc071275b13b1228c70bb88f4aaee.zip |
Merge remote-tracking branch 'remotes/ehabkost/tags/python-next-pull-request' into staging
Python queue, 2017-09-15
# gpg: Signature made Sat 16 Sep 2017 00:14:01 BST
# gpg: using RSA key 0x2807936F984DC5A6
# gpg: Good signature from "Eduardo Habkost <ehabkost@redhat.com>"
# Primary key fingerprint: 5A32 2FD5 ABC4 D3DB ACCF D1AA 2807 936F 984D C5A6
* remotes/ehabkost/tags/python-next-pull-request:
qemu.py: include debug information on launch error
qemu.py: improve message on negative exit code
qemu.py: use os.path.null instead of /dev/null
qemu.py: avoid writing to stdout/stderr
qemu.py: fix is_running() return before first launch()
qtest.py: Few pylint/style fixes
qmp.py: Avoid overriding a builtin object
qmp.py: Avoid "has_key" usage
qmp.py: Use object-based class for QEMUMonitorProtocol
qmp.py: Couple of pylint/style fixes
qemu.py: Use custom exceptions rather than Exception
qemu.py: Simplify QMP key-conversion
qemu.py: Use iteritems rather than keys()
qemu|qtest: Avoid dangerous arguments
qemu.py: Pylint/style fixes
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'scripts/qmp')
-rwxr-xr-x | scripts/qmp/qmp-shell | 4 | ||||
-rw-r--r-- | scripts/qmp/qmp.py | 49 |
2 files changed, 32 insertions, 21 deletions
diff --git a/scripts/qmp/qmp-shell b/scripts/qmp/qmp-shell index 860ffb27f2..be449de621 100755 --- a/scripts/qmp/qmp-shell +++ b/scripts/qmp/qmp-shell @@ -106,7 +106,7 @@ class FuzzyJSON(ast.NodeTransformer): # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): def __init__(self, address, pretty=False): - qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) + super(QMPShell, self).__init__(self.__get_address(address)) self._greeting = None self._completer = None self._pretty = pretty @@ -281,7 +281,7 @@ class QMPShell(qmp.QEMUMonitorProtocol): return True def connect(self, negotiate): - self._greeting = qmp.QEMUMonitorProtocol.connect(self, negotiate) + self._greeting = super(QMPShell, self).connect(negotiate) self.__completer_setup() def show_banner(self, msg='Welcome to the QMP low-level shell!'): diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py index 62d3651967..ef12e8a1a0 100644 --- a/scripts/qmp/qmp.py +++ b/scripts/qmp/qmp.py @@ -13,19 +13,30 @@ import errno import socket import sys + class QMPError(Exception): pass + class QMPConnectError(QMPError): pass + class QMPCapabilitiesError(QMPError): pass + class QMPTimeoutError(QMPError): pass -class QEMUMonitorProtocol: + +class QEMUMonitorProtocol(object): + + #: Socket's error class + error = socket.error + #: Socket's timeout + timeout = socket.timeout + def __init__(self, address, server=False, debug=False): """ Create a QEMUMonitorProtocol class. @@ -42,6 +53,7 @@ class QEMUMonitorProtocol: self.__address = address self._debug = debug self.__sock = self.__get_sock() + self.__sockfile = None if server: self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__sock.bind(self.__address) @@ -56,7 +68,7 @@ class QEMUMonitorProtocol: def __negotiate_capabilities(self): greeting = self.__json_read() - if greeting is None or not greeting.has_key('QMP'): + if greeting is None or "QMP" not in greeting: raise QMPConnectError # Greeting seems ok, negotiate capabilities resp = self.cmd('qmp_capabilities') @@ -78,8 +90,6 @@ class QEMUMonitorProtocol: continue return resp - error = socket.error - def __get_events(self, wait=False): """ Check for new events in the stream and cache them in __events. @@ -89,8 +99,8 @@ class QEMUMonitorProtocol: @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. """ # Check for new events regardless and pull them into the cache: @@ -167,38 +177,41 @@ class QEMUMonitorProtocol: print >>sys.stderr, "QMP:<<< %s" % resp return resp - def cmd(self, name, args=None, id=None): + def cmd(self, name, args=None, cmd_id=None): """ Build a QMP command and send it to the QMP Monitor. @param name: command name (string) @param args: command arguments (dict) - @param id: command id (dict, list, string or int) + @param cmd_id: command id (dict, list, string or int) """ - qmp_cmd = { 'execute': name } + qmp_cmd = {'execute': name} if args: qmp_cmd['arguments'] = args - if id: - qmp_cmd['id'] = id + if cmd_id: + qmp_cmd['id'] = cmd_id return self.cmd_obj(qmp_cmd) def command(self, cmd, **kwds): + """ + Build and send a QMP command to the monitor, report errors if any + """ ret = self.cmd(cmd, kwds) - if ret.has_key('error'): + if "error" in ret: raise Exception(ret['error']['desc']) return ret['return'] def pull_event(self, wait=False): """ - Get and delete the first available QMP event. + Pulls a single event. @param wait (bool): block until an event is available. @param wait (float): If wait is a float, treat it as a timeout value. @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. @return The first available QMP event, or None. """ @@ -217,8 +230,8 @@ class QEMUMonitorProtocol: @raise QMPTimeoutError: If a timeout float is provided and the timeout period elapses. - @raise QMPConnectError: If wait is True but no events could be retrieved - or if some other error occurred. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. @return The list of available QMP events. """ @@ -235,8 +248,6 @@ class QEMUMonitorProtocol: self.__sock.close() self.__sockfile.close() - timeout = socket.timeout - def settimeout(self, timeout): self.__sock.settimeout(timeout) |