"""Miscellaneous functions.

run_command borrowed from Cheesecake - See CREDITS.

"""

import os
import signal
import time
from subprocess import Popen, STDOUT


def get_yolk_dir():
    """Return location we store config files and data."""
    return os.path.abspath('%s/.yolk' % os.path.expanduser('~'))


def run_command(cmd, env=None, max_timeout=None):
    r"""Run command and return its return status code and its output.

    >>> run_command('true')
    (0, '')

    >>> run_command('false')
    (1, '')

    >>> run_command('echo hello world')
    (0, 'hello world\n')

    """
    arglist = cmd.split()

    output = os.tmpfile()
    try:
        pipe = Popen(arglist, stdout=output, stderr=STDOUT, env=env)
    except Exception as errmsg:
        return 1, errmsg

    # Wait only max_timeout seconds.
    if max_timeout:
        start = time.time()
        while pipe.poll() is None:
            time.sleep(0.1)
            if time.time() - start > max_timeout:
                os.kill(pipe.pid, signal.SIGINT)
                pipe.wait()
                return 1, 'Time exceeded'

    pipe.wait()
    output.seek(0)
    return pipe.returncode, output.read()


def command_successful(cmd):
    """Returns True if command exited normally, False otherwise."""
    return_code, _output = run_command(cmd)
    return return_code == 0

@@ -102,21 +102,20 @@
 rt s
-ignal
+hlex
 %0Aimport 
 time
@@ -114,17 +114,21 @@
 ort 
-time%0Afrom
+signal%0Aimport
  sub
@@ -138,29 +138,20 @@
 cess
- 
+%0A
 import 
-Popen, STDOUT
+time
 %0A%0A%0Ad
@@ -580,125 +580,208 @@
     
-arglist = cmd.split()%0A%0A    output = os.tmpfile()%0A    try:%0A        pipe = Popen(arglist, stdout=output, stderr=STDOUT,
+try:%0A        pipe = subprocess.Popen(shlex.split(cmd),%0A                                stdout=subprocess.PIPE,%0A                                stderr=subprocess.STDOUT,%0A                               
  env
@@ -1182,27 +1182,8 @@
 t()%0A
-    output.seek(0)%0A
     
@@ -1210,20 +1210,44 @@
 de, 
-output.read(
+pipe.communicate()%5B0%5D.decode('utf-8'
 )%0A%0A%0A

Add Python 3 compatibility