From fbb6b864077da00e4d845d262b2fe85661ee5c1a Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 11 Jun 2010 16:18:12 +0200
Subject: [PATCH] import/export daemon: Simplify command building

Instead of appending strings, stage parts in a list. Building the "dd"
command is moved to a separate function.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/impexpd/__init__.py | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/lib/impexpd/__init__.py b/lib/impexpd/__init__.py
index b1078dbc5..e79729407 100644
--- a/lib/impexpd/__init__.py
+++ b/lib/impexpd/__init__.py
@@ -197,14 +197,10 @@ class CommandBuilder(object):
       ",".join(addr1), ",".join(addr2)
       ]
 
-  def _GetTransportCommand(self):
-    """Returns the command for the transport part of the daemon.
+  def _GetDdCommand(self):
+    """Returns the command for measuring throughput.
 
     """
-    socat_cmd = ("%s 2>&%d" %
-                 (utils.ShellQuoteArgs(self._GetSocatCommand()),
-                  self._socat_stderr_fd))
-
     dd_cmd = StringIO()
     # Setting LC_ALL since we want to parse the output and explicitely
     # redirecting stdin, as the background process (dd) would have /dev/null as
@@ -216,32 +212,46 @@ class CommandBuilder(object):
     # And wait for dd
     dd_cmd.write(" wait $pid;")
     dd_cmd.write(" }")
+    return dd_cmd.getvalue()
+
+  def _GetTransportCommand(self):
+    """Returns the command for the transport part of the daemon.
+
+    """
+    socat_cmd = ("%s 2>&%d" %
+                 (utils.ShellQuoteArgs(self._GetSocatCommand()),
+                  self._socat_stderr_fd))
+    dd_cmd = self._GetDdCommand()
 
     compr = self._opts.compress
 
     assert compr in constants.IEC_ALL
 
+    parts = []
+
     if self._mode == constants.IEM_IMPORT:
+      parts.append(socat_cmd)
+
       if compr == constants.IEC_GZIP:
-        transport_cmd = "%s | gunzip -c" % socat_cmd
-      else:
-        transport_cmd = socat_cmd
+        parts.append("gunzip -c")
+
+      parts.append(dd_cmd)
 
-      transport_cmd += " | %s" % dd_cmd.getvalue()
     elif self._mode == constants.IEM_EXPORT:
+      parts.append(dd_cmd)
+
       if compr == constants.IEC_GZIP:
-        transport_cmd = "gzip -c | %s" % socat_cmd
-      else:
-        transport_cmd = socat_cmd
+        parts.append("gzip -c")
+
+      parts.append(socat_cmd)
 
-      transport_cmd = "%s | %s" % (dd_cmd.getvalue(), transport_cmd)
     else:
       raise errors.GenericError("Invalid mode '%s'" % self._mode)
 
     # TODO: Run transport as separate user
     # The transport uses its own shell to simplify running it as a separate user
     # in the future.
-    return self.GetBashCommand(transport_cmd)
+    return self.GetBashCommand(" | ".join(parts))
 
   def GetCommand(self):
     """Returns the complete child process command.
-- 
GitLab