Skip to contentSkip to conversation

scripts: Add TCP endpoints for qom-* scripts

PATCH New [email protected] 1 participant 2 messages Aug 13, 2016

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 scripts: Add ability for qom-* python scripts to target tcp endpoints

In reply to [email protected]

From: Carl Allendorph <[email protected]>

The current code for QEMUMonitorProtocol accepts both a unix socket endpoint as a string and a tcp endpoint as a tuple. Most of the scripts that use this class don't massage the command line argument to generate a tuple. This patch refactors qmp-shell slightly to reuse the existing parsing of the "host:port" string for all the qom-* scripts.

---
 scripts/qmp/qmp-shell | 22 ++--------------------
 scripts/qmp/qmp.py    | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 23 deletions(-)
scripts/qmp/qmp-shell +2 -20
index 0373b24..8a2a437 100755
@@ -83,9 +83,6 @@ class QMPCompleter(list):
 class QMPShellError(Exception):
     pass
 
-class QMPShellBadPort(QMPShellError):
-    pass
-
 class FuzzyJSON(ast.NodeTransformer):
     '''This extension of ast.NodeTransformer filters literal "true/false/null"
     values in an AST and replaces them by proper "True/False/None" values that
@@ -103,28 +100,13 @@ 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))
+        qmp.QEMUMonitorProtocol.__init__(self, address)
         self._greeting = None
         self._completer = None
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
 
-    def __get_address(self, arg):
-        """
-        Figure out if the argument is in the port:host form, if it's not it's
-        probably a file path.
-        """
-        addr = arg.split(':')
-        if len(addr) == 2:
-            try:
-                port = int(addr[1])
-            except ValueError:
-                raise QMPShellBadPort
-            return ( addr[0], port )
-        # socket path
-        return arg
-
     def _fill_completion(self):
         for cmd in self.cmd('query-commands')['return']:
             self._completer.append(cmd['name'])
@@ -400,7 +382,7 @@ def main():
 
         if qemu is None:
             fail_cmdline()
-    except QMPShellBadPort:
+    except qmp.QMPShellBadPort:
         die('bad port number in command-line')
 
     try:
scripts/qmp/qmp.py +20 -3
index 62d3651..261ece8 100644
@@ -25,21 +25,23 @@ class QMPCapabilitiesError(QMPError):
 class QMPTimeoutError(QMPError):
     pass
 
+class QMPShellBadPort(QMPError):
+    pass
+
 class QEMUMonitorProtocol:
     def __init__(self, address, server=False, debug=False):
         """
         Create a QEMUMonitorProtocol class.
 
         @param address: QEMU address, can be either a unix socket path (string)
-                        or a tuple in the form ( address, port ) for a TCP
-                        connection
+                        or a TCP endpoint (string in the format "host:port")
         @param server: server mode listens on the socket (bool)
         @raise socket.error on socket connection errors
         @note No connection is established, this is done by the connect() or
               accept() methods
         """
         self.__events = []
-        self.__address = address
+        self.__address = self.__get_address(address)
         self._debug = debug
         self.__sock = self.__get_sock()
         if server:
@@ -47,6 +49,21 @@ class QEMUMonitorProtocol:
             self.__sock.bind(self.__address)
             self.__sock.listen(1)
 
+    def __get_address(self, arg):
+        """
+        Figure out if the argument is in the port:host form, if it's not it's
+        probably a file path.
+        """
+        addr = arg.split(':')
+        if len(addr) == 2:
+            try:
+                port = int(addr[1])
+            except ValueError:
+                raise QMPShellBadPort
+            return ( addr[0], port )
+        # socket path
+        return arg
+
     def __get_sock(self):
         if isinstance(self.__address, tuple):
             family = socket.AF_INET
Signature · 1 line
2.7.4
#0 lore raw

PATCH 1/1 scripts: Add TCP endpoints for qom-* scripts

From: Carl Allendorph <[email protected]>

I've created a patch for bug #1612908. The current docs for the scripts in the "scripts/qmp/" directory suggest that both unix sockets and tcp endpoints can be used. The TCP endpoints don't work for most of the scripts, with notable exception of 'qmp-shell'. This patch attempts to refactor the process of distinguishing between unix path endpoints and tcp endpoints to work for all of these scripts.

Carl Allendorph (1):
scripts: Add ability for qom-* python scripts to target tcp endpoints

 scripts/qmp/qmp-shell | 22 ++--------------------
 scripts/qmp/qmp.py    | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 23 deletions(-)

--
2.7.4