X-Git-Url: http://git.onelab.eu/?p=nepi.git;a=blobdiff_plain;f=nepi%2Futil%2Fexecfuncs.py;fp=nepi%2Futil%2Fexecfuncs.py;h=502906c81c751dad825f136d6b0c9eb6ce45a825;hp=a8591f827bc6c4f4f0e3a6d2f9327bdde591499b;hb=1a578c87ac89d619a9a83f3ff83f239348ab4426;hpb=a5405fe3a84b359b41355fa02a3f569fbd4a25e6 diff --git a/nepi/util/execfuncs.py b/nepi/util/execfuncs.py index a8591f82..502906c8 100644 --- a/nepi/util/execfuncs.py +++ b/nepi/util/execfuncs.py @@ -25,24 +25,24 @@ import subprocess from six import PY2 def lexec(command, - user = None, - sudo = False, - env = None): + user = None, + sudo = False, + env = None): """ - Executes a local command, returns ((stdout,stderr),process) + Executes a local command, returns ((stdout, stderr), process) """ if env: export = '' for envkey, envval in env.items(): - export += '%s=%s ' % (envkey, envval) - command = "%s %s" % (export, command) + export += "{}={} ".format(envkey, envval) + command = "{} {}".format(export, command) if sudo: - command = "sudo %s" % command + command = "sudo {}".format(command) # XXX: Doing 'su user' blocks waiting for a password on stdin #elif user: - # command = "su %s ; %s " % (user, command) + # command = "su {} ; {} ".format(user, command) extras = {} if PY2 else {'universal_newlines' : True} proc = subprocess.Popen(command, @@ -52,7 +52,7 @@ def lexec(command, **extras) out = err = "" - log_msg = "lexec - command %s " % command + log_msg = "lexec - command {} ".format(command) try: out, err = proc.communicate() @@ -65,7 +65,7 @@ def lexec(command, def lcopy(source, dest, recursive = False): """ - Copies from/to localy. + Copies from/to locally. """ args = ["cp"] @@ -90,7 +90,7 @@ def lcopy(source, dest, recursive = False): out = err = "" command = " ".join(args) - log_msg = " lcopy - command %s " % command + log_msg = " lcopy - command {} ".format(command) try: out, err = proc.communicate() @@ -102,13 +102,13 @@ def lcopy(source, dest, recursive = False): return ((out, err), proc) def lspawn(command, pidfile, - stdout = '/dev/null', - stderr = STDOUT, - stdin = '/dev/null', - home = None, - create_home = False, - sudo = False, - user = None): + stdout = '/dev/null', + stderr = STDOUT, + stdin = '/dev/null', + home = None, + create_home = False, + sudo = False, + user = None): """ Spawn a local command such that it will continue working asynchronously. @@ -142,28 +142,24 @@ def lspawn(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, - } + escaped_pidfile = shell_escape(pidfile), + daemon_command = '{{ {{ {command} > {stdout} 2>{stderr} < {stdin} & }} ; echo $! 1 > {escaped_pidfile} ; }}'\ + .format(**locals()) - cmd = "%(create)s%(gohome)s rm -f %(pidfile)s ; %(sudo)s 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 else '', - } + cmd = "{create}{gohome} rm -f {pidfile} ; {sudo} 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 else '') - (out,err), proc = lexec(cmd) + (out, err), proc = lexec(cmd) 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) + return ((out, err), proc) def lgetpid(pidfile): """ @@ -178,7 +174,7 @@ def lgetpid(pidfile): or None if the pidfile isn't valid yet (maybe the process is still starting). """ - (out,err), proc = lexec("cat %s" % pidfile ) + (out, err), proc = lexec("cat {}".format(pidfile)) if proc.wait(): return None @@ -202,12 +198,10 @@ def lstatus(pid, ppid): One of NOT_STARTED, RUNNING, FINISHED """ - (out,err), proc = lexec( + (out, err), proc = lexec( # 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(ppid = ppid, pid = pid)) if proc.wait(): return ProcStatus.NOT_STARTED @@ -237,35 +231,34 @@ def lkill(pid, ppid, sudo = False, nowait = False): Nothing, should have killed the process """ - 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 {pid} -o pid h)".format(pid=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