prettyfied sshfuncs - no change
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 11 Mar 2016 13:32:39 +0000 (14:32 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 11 Mar 2016 13:32:39 +0000 (14:32 +0100)
nepi/util/sshfuncs.py

index 7e22b5c..663eeb7 100644 (file)
@@ -42,9 +42,9 @@ logger = logging.getLogger("sshfuncs")
 
 def log(msg, level = logging.DEBUG, out = None, err = None):
     if out:
-        msg += " - OUT: %s " % out
+        msg += " - OUT: {} ".format(out)
     if err:
-        msg += " - ERROR: %s " % err
+        msg += " - ERROR: {} ".format(err)
     logger.log(level, msg)
 
 if hasattr(os, "devnull"):
@@ -84,9 +84,9 @@ def resolve_hostname(host):
         extras = {} if PY2 else {'universal_newlines' : True}
         p = subprocess.Popen(
             "ip -o addr list",
-            shell=True,
-            stdout=subprocess.PIPE,
-            stderr=subprocess.PIPE,
+            shell = True,
+            stdout = subprocess.PIPE,
+            stderr = subprocess.PIPE,
             **extras
         )
         stdout, stderr = p.communicate()
@@ -107,7 +107,7 @@ def gethostbyname(host):
             hostbyname = resolve_hostname(host)
             hostbyname_cache[host] = hostbyname
 
-            msg = " Added hostbyname %s - %s " % (host, hostbyname)
+            msg = " Added hostbyname {} - {} ".format(host, hostbyname)
             log(msg, logging.DEBUG)
 
     return hostbyname
@@ -133,7 +133,7 @@ def openssh_has_persist():
                 stdin = null,
                 **extras
             )
-            out,err = proc.communicate()
+            out, err = proc.communicate()
             proc.wait()
         
         vre = re.compile(r'OpenSSH_(?:[6-9]|5[.][8-9]|5[.][1-9][0-9]|[1-9][0-9]).*', re.I)
@@ -158,7 +158,7 @@ def make_server_key_args(server_key, host, port):
 
     """
     if port is not None:
-        host = '%s:%s' % (host, str(port))
+        host = '{}:{}'.format(host, str(port))
 
     # Create a temporary server key file
     tmp_known_hosts = tempfile.NamedTemporaryFile()
@@ -166,11 +166,11 @@ def make_server_key_args(server_key, host, port):
     hostbyname = gethostbyname(host) 
 
     # Add the intended host key
-    tmp_known_hosts.write('%s,%s %s\n' % (host, hostbyname, server_key))
+    tmp_known_hosts.write('{},{} {}\n'.format(host, hostbyname, server_key))
     
     # If we're not in strict mode, add user-configured keys
-    if os.environ.get('NEPI_STRICT_AUTH_MODE',"").lower() not in ('1','true','on'):
-        user_hosts_path = '%s/.ssh/known_hosts' % (os.environ.get('HOME',""),)
+    if os.environ.get('NEPI_STRICT_AUTH_MODE', "").lower() not in ('1', 'true', 'on'):
+        user_hosts_path = '{}/.ssh/known_hosts'.format(os.environ.get('HOME', ""))
         if os.access(user_hosts_path, os.R_OK):
             with open(user_hosts_path, "r") as f:
                 tmp_known_hosts.write(f.read())
@@ -183,10 +183,10 @@ def make_control_path(agent, forward_x11):
     ctrl_path = "/tmp/nepi_ssh"
 
     if agent:
-        ctrl_path +="_a"
+        ctrl_path += "_a"
 
     if forward_x11:
-        ctrl_path +="_x"
+        ctrl_path += "_x"
 
     ctrl_path += "-%r@%h:%p"
 
@@ -201,12 +201,12 @@ def shell_escape(s):
     else:
         # unsafe string - escape
         def escape(c):
-            if (32 <= ord(c) < 127 or c in ('\r','\n','\t')) and c not in ("'",'"'):
+            if (32 <= ord(c) < 127 or c in ('\r', '\n', '\t')) and c not in ("'", '"'):
                 return c
             else:
-                return "'$'\\x%02x''" % (ord(c),)
+                return "'$'\\x{:02x}''".format(ord(c))
         s = ''.join(map(escape, s))
-        return "'%s'" % (s,)
+        return "'{}'".format(s)
 
 def eintr_retry(func):
     """Retries a function invocation when a EINTR occurs"""
@@ -232,23 +232,23 @@ def eintr_retry(func):
     return rv
 
 def rexec(command, host, user, 
-        port = None,
-        gwuser = None,
-        gw = None, 
-        agent = True,
-        sudo = False,
-        identity = None,
-        server_key = None,
-        env = None,
-        tty = False,
-        connect_timeout = 30,
-        retry = 3,
-        persistent = True,
-        forward_x11 = False,
-        blocking = True,
-        strict_host_checking = True):
+          port = None,
+          gwuser = None,
+          gw = None, 
+          agent = True,
+          sudo = False,
+          identity = None,
+          server_key = None,
+          env = None,
+          tty = False,
+          connect_timeout = 30,
+          retry = 3,
+          persistent = True,
+          forward_x11 = False,
+          blocking = True,
+          strict_host_checking = True):
     """
-    Executes a remote command, returns ((stdout,stderr),process)
+    Executes a remote command, returns ((stdout, stderr), process)
     """
 
     tmp_known_hosts = None
@@ -259,7 +259,7 @@ def rexec(command, host, user,
     args = ['ssh', '-C',
             # Don't bother with localhost. Makes test easier
             '-o', 'NoHostAuthenticationForLocalhost=yes',
-            '-o', 'ConnectTimeout=%d' % (int(connect_timeout),),
+            '-o', 'ConnectTimeout={}'.format(connect_timeout),
             '-o', 'ConnectionAttempts=3',
             '-o', 'ServerAliveInterval=30',
             '-o', 'TCPKeepAlive=yes',
@@ -269,7 +269,7 @@ def rexec(command, host, user,
     if persistent and openssh_has_persist():
         args.extend([
             '-o', 'ControlMaster=auto',
-            '-o', 'ControlPath=%s' % (make_control_path(agent, forward_x11),),
+            '-o', 'ControlPath={}'.format(make_control_path(agent, forward_x11)),
             '-o', 'ControlPersist=60' ])
 
     if not strict_host_checking:
@@ -284,7 +284,7 @@ def rexec(command, host, user,
         args.append('-A')
 
     if port:
-        args.append('-p%d' % port)
+        args.append('-p{}'.format(port))
 
     if identity:
         identity = os.path.expanduser(identity)
@@ -300,14 +300,14 @@ def rexec(command, host, user,
     if server_key:
         # Create a temporary server key file
         tmp_known_hosts = make_server_key_args(server_key, host, port)
-        args.extend(['-o', 'UserKnownHostsFile=%s' % (tmp_known_hosts.name,)])
+        args.extend(['-o', 'UserKnownHostsFile={}'.format(tmp_known_hosts.name)])
 
     if sudo:
         command = "sudo " + command
 
     args.append(command)
 
-    log_msg = " rexec - host %s - command %s " % (str(host), " ".join(map(str, args))) 
+    log_msg = " rexec - host {} - command {} ".format(host, " ".join(map(str, args)))
 
     stdout = stderr = stdin = subprocess.PIPE
     if forward_x11:
@@ -344,12 +344,12 @@ def rcopy(source, dest,
 
     # Parse destination as <user>@<server>:<path>
     if isinstance(dest, str) and ':' in dest:
-        remspec, path = dest.split(':',1)
+        remspec, path = dest.split(':', 1)
     elif isinstance(source, str) and ':' in source:
-        remspec, path = source.split(':',1)
+        remspec, path = source.split(':', 1)
     else:
         raise ValueError("Both endpoints cannot be local")
-    user,host = remspec.rsplit('@',1)
+    user, host = remspec.rsplit('@', 1)
     
     # plain scp
     tmp_known_hosts = None
@@ -370,7 +370,7 @@ def rcopy(source, dest,
             '-o', 'TCPKeepAlive=yes' ]
             
     if port:
-        args.append('-P%d' % port)
+        args.append('-P{}'.format(port))
 
     if gw:
         proxycommand = _proxy_command(gw, gwuser, identity)
@@ -386,7 +386,7 @@ def rcopy(source, dest,
     if server_key:
         # Create a temporary server key file
         tmp_known_hosts = make_server_key_args(server_key, host, port)
-        args.extend(['-o', 'UserKnownHostsFile=%s' % (tmp_known_hosts.name,)])
+        args.extend(['-o', 'UserKnownHostsFile={}'.format(tmp_known_hosts.name)])
 
     if not strict_host_checking:
         # Do not check for Host key. Unsafe.
@@ -398,7 +398,7 @@ def rcopy(source, dest,
         if openssh_has_persist():
             args.extend([
                 '-o', 'ControlMaster=auto',
-                '-o', 'ControlPath=%s' % (make_control_path(False, False),)
+                '-o', 'ControlPath={}'.format(make_control_path(False, False))
                 ])
         args.append(source)
 
@@ -407,7 +407,7 @@ def rcopy(source, dest,
     else:
         args.append(dest)
 
-    log_msg = " rcopy - host %s - command %s " % (str(host), " ".join(map(str, args)))
+    log_msg = " rcopy - host {} - command {} ".format(host, " ".join(map(str, args)))
     
     return _retry_rexec(args, log_msg, env = None, retry = retry, 
             tmp_known_hosts = tmp_known_hosts,
@@ -478,23 +478,21 @@ def rspawn(command, pidfile,
     else:
         stderr = ' ' + stderr
     
-    daemon_command = '{ { %(command)s > %(stdout)s 2>%(stderr)s < %(stdin)s & } ; echo $! 1 > %(pidfile)s ; }' % {
-        'command' : command,
-        'pidfile' : shell_escape(pidfile),
-        'stdout' : stdout,
-        'stderr' : stderr,
-        'stdin' : stdin,
-    }
+    daemon_command = '{{ {{ {command} > {stdout} 2>{stderr} < {stdin} & }} ; echo $! 1 > {pidfile} ; }}'\
+                     .format(command = command,
+                             pidfile = shell_escape(pidfile),
+                             stdout = stdout,
+                             stderr = stderr,
+                             stdin = stdin)
     
-    cmd = "%(create)s%(gohome)s rm -f %(pidfile)s ; %(sudo)s nohup bash -c %(command)s " % {
-            'command' : shell_escape(daemon_command),
-            'sudo' : 'sudo -S' if sudo else '',
-            'pidfile' : shell_escape(pidfile),
-            'gohome' : 'cd %s ; ' % (shell_escape(home),) if home else '',
-            'create' : 'mkdir -p %s ; ' % (shell_escape(home),) if create_home and home else '',
-        }
-
-    (out,err),proc = rexec(
+    cmd = "{create}{gohome} rm -f {pidfile} ; {sudo} nohup bash -c {command} "\
+          .format(command = shell_escape(daemon_command),
+                  sudo = 'sudo -S' if sudo else '',
+                  pidfile = shell_escape(pidfile),
+                  gohome = 'cd {} ; '.format(shell_escape(home)) if home else '',
+                  create = 'mkdir -p {} ; '.format(shell_escape(home)) if create_home and home else '')
+
+    (out, err), proc = rexec(
         cmd,
         host = host,
         port = port,
@@ -506,10 +504,10 @@ def rspawn(command, pidfile,
         server_key = server_key,
         tty = tty,
         strict_host_checking = strict_host_checking ,
-        )
+    )
     
     if proc.wait():
-        raise RuntimeError("Failed to set up application on host %s: %s %s" % (host, out,err,))
+        raise RuntimeError("Failed to set up application on host {}: {} {}".format(host, out, err))
 
     return ((out, err), proc)
 
@@ -540,10 +538,8 @@ def rgetpid(pidfile,
         or None if the pidfile isn't valid yet (can happen when process is staring up)
 
     """
-    (out,err),proc = rexec(
-        "cat %(pidfile)s" % {
-            'pidfile' : pidfile,
-        },
+    (out, err), proc = rexec(
+        "cat {}".format(pidfile),
         host = host,
         port = port,
         user = user,
@@ -553,7 +549,7 @@ def rgetpid(pidfile,
         identity = identity,
         server_key = server_key,
         strict_host_checking = strict_host_checking
-        )
+    )
         
     if proc.wait():
         return None
@@ -588,12 +584,10 @@ def rstatus(pid, ppid,
         :rtype: int (One of NOT_STARTED, RUNNING, FINISHED)
     
     """
-    (out,err),proc = rexec(
+    (out, err), proc = rexec(
         # Check only by pid. pid+ppid does not always work (especially with sudo) 
-        " (( ps --pid %(pid)d -o pid | grep -c %(pid)d && echo 'wait')  || echo 'done' ) | tail -n 1" % {
-            'ppid' : ppid,
-            'pid' : pid,
-        },
+        " (( ps --pid {pid} -o pid | grep -c {pid} && echo 'wait')  || echo 'done' ) | tail -n 1"\
+        .format(**locals()),
         host = host,
         port = port,
         user = user,
@@ -603,7 +597,7 @@ def rstatus(pid, ppid,
         identity = identity,
         server_key = server_key,
         strict_host_checking = strict_host_checking
-        )
+    )
     
     if proc.wait():
         return ProcStatus.NOT_STARTED
@@ -647,36 +641,32 @@ def rkill(pid, ppid,
         :type sudo: bool
         
     """
-    subkill = "$(ps --ppid %(pid)d -o pid h)" % { 'pid' : pid }
-    cmd = """
-SUBKILL="%(subkill)s" ;
-%(sudo)s kill -- -%(pid)d $SUBKILL || /bin/true
-%(sudo)s kill %(pid)d $SUBKILL || /bin/true
+    subkill = "$(ps --ppid {} -o pid h)".format(pid)
+    cmd_format = """
+SUBKILL="{subkill}" ;
+{sudo} kill -- -{pid} $SUBKILL || /bin/true
+{sudo} kill {pid} $SUBKILL || /bin/true
 for x in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ; do 
     sleep 0.2 
-    if [ `ps --pid %(pid)d -o pid | grep -c %(pid)d` == '0' ]; then
+    if [ `ps --pid {pid} -o pid | grep -c {pid}` == '0' ]; then
         break
     else
-        %(sudo)s kill -- -%(pid)d $SUBKILL || /bin/true
-        %(sudo)s kill %(pid)d $SUBKILL || /bin/true
+        {sudo} kill -- -{pid} $SUBKILL || /bin/true
+        {sudo} kill {pid} $SUBKILL || /bin/true
     fi
     sleep 1.8
 done
-if [ `ps --pid %(pid)d -o pid | grep -c %(pid)d` != '0' ]; then
-    %(sudo)s kill -9 -- -%(pid)d $SUBKILL || /bin/true
-    %(sudo)s kill -9 %(pid)d $SUBKILL || /bin/true
+if [ `ps --pid {pid} -o pid | grep -c {pid}` != '0' ]; then
+    {sudo} kill -9 -- -{pid} $SUBKILL || /bin/true
+    {sudo} kill -9 {pid} $SUBKILL || /bin/true
 fi
 """
     if nowait:
-        cmd = "( %s ) >/dev/null 2>/dev/null </dev/null &" % (cmd,)
-
-    (out,err),proc = rexec(
-        cmd % {
-            'ppid' : ppid,
-            'pid' : pid,
-            'sudo' : 'sudo -S' if sudo else '',
-            'subkill' : subkill,
-        },
+        cmd_format = "( {} ) >/dev/null 2>/dev/null </dev/null &".format(cmd_format)
+
+    sudo = 'sudo -S' if sudo else ''
+    (out, err), proc = rexec(
+        cmd_format.format(**locals()),
         host = host,
         port = port,
         user = user,
@@ -753,15 +743,15 @@ def _retry_rexec(args,
                 
                 if skip:
                     t = x*2
-                    msg = "SLEEPING %d ... ATEMPT %d - command %s " % ( 
-                            t, x, " ".join(args))
+                    msg = "SLEEPING {} ... ATEMPT {} - command {} "\
+                          .format(t, x, " ".join(args))
                     log(msg, logging.DEBUG)
 
                     time.sleep(t)
                     continue
             break
         except RuntimeError as e:
-            msg = " rexec EXCEPTION - TIMEOUT -> %s \n %s" % ( e.args, log_msg )
+            msg = " rexec EXCEPTION - TIMEOUT -> {} \n {}".format(e.args, log_msg)
             log(msg, logging.DEBUG, out, err)
 
             if retry <= 0:
@@ -916,12 +906,12 @@ def _proxy_command(gw, gwuser, gwidentity):
 
     proxycommand = 'ProxyCommand=ssh -q '
     if gwidentity:
-        proxycommand += '-i %s ' % os.path.expanduser(gwidentity)
+        proxycommand += '-i {} '.format(os.path.expanduser(gwidentity))
     if gwuser:
-        proxycommand += '%s' % gwuser
+        proxycommand += '{}'.format(gwuser)
     else:
         proxycommand += '%r'
-    proxycommand += '@%s -W %%h:%%p' % gw
+    proxycommand += '@{} -W %h:%p'.format(gw)
 
     return proxycommand