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,
**extras)
out = err = ""
- log_msg = "lexec - command %s " % command
+ log_msg = "lexec - command {} ".format(command)
try:
out, err = proc.communicate()
def lcopy(source, dest, recursive = False):
"""
- Copies from/to localy.
+ Copies from/to locally.
"""
args = ["cp"]
out = err = ""
command = " ".join(args)
- log_msg = " lcopy - command %s " % command
+ log_msg = " lcopy - command {} ".format(command)
try:
out, err = proc.communicate()
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.
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):
"""
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
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
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 </dev/null &" % (cmd,)
+ cmd = "( {} ) >/dev/null 2>/dev/null </dev/null &".format(cmd)
- (out,err),proc = lexec(
- cmd % {
- 'ppid' : ppid,
- 'pid' : pid,
- 'sudo' : 'sudo -S' if sudo else '',
- 'subkill' : subkill,
- })
+ (out, err), proc = lexec(
+ cmd_format.format(
+ ppid = ppid,
+ pid = pid,
+ sudo = 'sudo -S' if sudo else '',
+ subkill = subkill))