various cosmetic
[nepi.git] / nepi / util / sshfuncs.py
index 663eeb7..fa7df4b 100644 (file)
@@ -38,13 +38,15 @@ from six import PY2
 
 _re_inet = re.compile("\d+:\s+(?P<name>[a-z0-9_-]+)\s+inet6?\s+(?P<inet>[a-f0-9.:/]+)\s+(brd\s+[0-9.]+)?.*scope\s+global.*") 
 
+# to debug, just use
+# logging.getLogger('sshfuncs').setLevel(logging.DEBUG)
 logger = logging.getLogger("sshfuncs")
 
 def log(msg, level = logging.DEBUG, out = None, err = None):
     if out:
-        msg += " - OUT: {} ".format(out)
+        msg += " - OUT is {} long".format(len(out))
     if err:
-        msg += " - ERROR: {} ".format(err)
+        msg += " - ERR is {} long".format(len(err))
     logger.log(level, msg)
 
 if hasattr(os, "devnull"):
@@ -307,16 +309,14 @@ def rexec(command, host, user,
 
     args.append(command)
 
-    log_msg = " rexec - host {} - command {} ".format(host, " ".join(map(str, args)))
+    log_msg = " rexec - host {} - command {} ".format(host, pretty_args(args))
 
     stdout = stderr = stdin = subprocess.PIPE
     if forward_x11:
         stdout = stderr = stdin = None
 
     return _retry_rexec(args, log_msg, 
-                        stderr = stderr,
-                        stdin = stdin,
-                        stdout = stdout,
+                        stdin = stdin, stdout = stdout, stderr = stderr,
                         env = env, 
                         retry = retry, 
                         tmp_known_hosts = tmp_known_hosts,
@@ -407,11 +407,11 @@ def rcopy(source, dest,
     else:
         args.append(dest)
 
-    log_msg = " rcopy - host {} - command {} ".format(host, " ".join(map(str, args)))
+    log_msg = " rcopy - host {} - command {} ".format(host, pretty_args(args))
     
     return _retry_rexec(args, log_msg, env = None, retry = retry, 
-            tmp_known_hosts = tmp_known_hosts,
-            blocking = True)
+                        tmp_known_hosts = tmp_known_hosts,
+                        blocking = True)
 
 def rspawn(command, pidfile, 
            stdout = '/dev/null', 
@@ -683,6 +683,12 @@ fi
 
     return (out, err), proc
 
+# add quotes around a shell arg only if it has spaces
+def pretty_arg(shell_arg):
+    return shell_arg if ' ' not in shell_arg else "'{}'".format(shell_arg)
+def pretty_args(shell_args):
+    return " ".join([pretty_arg(shell_arg) for shell_arg in shell_args])
+
 def _retry_rexec(args,
                  log_msg,
                  stdout = subprocess.PIPE,
@@ -695,7 +701,7 @@ def _retry_rexec(args,
 
     for x in range(retry):
         # display command actually invoked when debug is turned on
-        message = " ".join( [ "'{}'".format(arg) for arg in args ] )
+        message = pretty_args(args)
         log("sshfuncs: invoking {}".format(message), logging.DEBUG)
         extras = {} if PY2 else {'universal_newlines' : True}
         # connects to the remote host and starts a remote connection
@@ -831,7 +837,7 @@ def _communicate(proc, input, timeout=None, err_on_timeout=True):
             # we can write up to PIPE_BUF bytes without risk
             # blocking.  POSIX defines PIPE_BUF >= 512
             bytes_written = os.write(proc.stdin.fileno(),
-                    buffer(input, input_offset, 512))
+                                     buffer(input, input_offset, 512))
             input_offset += bytes_written
 
             if input_offset >= len(input):
@@ -848,6 +854,7 @@ def _communicate(proc, input, timeout=None, err_on_timeout=True):
                 proc.stdout.close()
                 read_set.remove(proc.stdout)
             stdout.append(data)
+            log("have read {} bytes from stdout".format(len(stdout)))
 
         # likewise
         if proc.stderr in rlist:
@@ -856,6 +863,7 @@ def _communicate(proc, input, timeout=None, err_on_timeout=True):
                 proc.stderr.close()
                 read_set.remove(proc.stderr)
             stderr.append(data)
+            log("have read {} bytes from stderr".format(len(stdout)))
 
     # All data exchanged.  Translate lists into strings.
     if stdout is not None: