# Path: pykern/pkdebug.py
# def pkdc(fmt, *args, **kwargs):
#     """Conditional print a message to `output` selectively based on `control`.
# 
#     Controlled output that you can leave in your code permanently.
# 
#     Args:
#         fmt (str): how to :func:`str.format`
#         args: what to format
#         kwargs: what to format
#     """
#     # Since calls are left in for product, this check has
#     # some value.
#     if _have_control:
#         _printer._write(fmt, args, kwargs, with_control=True)
# 
# def pkdexc():
#     """Return last exception and stack as a string
# 
#     Must be called from an ``except``. This function removes
#     the last two calls from the stack. It joins the traceback
#     so you get a complete stack trace.
# 
#     Will catch exceptions during the formatting and returns a
#     string in all cases.
# 
#     Example::
# 
#         try:
#             something
#         except:
#             pkdp(pkdexc())
# 
#     Returns:
#         str: formatted exception and stack trace
#     """
#     try:
#         e = sys.exc_info()
#         if hasattr(traceback, 'TracebackException'):
#             return ''.join(
#                 traceback.format_exception(*e) \
#                 + ['\nException was printed at:\n\n'] \
#                 + traceback.format_exception_only(e[0], e[1]) \
#                 + traceback.format_stack()[:-2],
#             )
# 
#         else:
#             return ''.join(
#                 traceback.format_exception_only(e[0], e[1]) \
#                 + traceback.format_stack()[:-2] \
#                 + traceback.format_tb(e[2]),
#             )
# 
#     except Exception as e:
#         return 'pykern.pkdebug.pkdexc: unable to retrieve exception info'
# 
# def pkdp(fmt_or_arg, *args, **kwargs):
#     """Print a message to `output` unconditionally, possibly returning its arg
# 
#     Use for print statements in your code that you don't intend
#     to keep in the system.
# 
#     Use `pkdlog` for permanent log messages.
# 
#     Args:
#         fmt_or_arg (object): how to :func:`str.format`, or object to print
#         args: what to format
#         kwargs: what to format
# 
#     Returns:
#         object: Will return fmt_or_arg, if args and kwargs are empty
#     """
#     if args or kwargs:
#         _printer._write(fmt_or_arg, args, kwargs, with_control=False)
#     else:
#         _printer._write('{}', [fmt_or_arg], {}, with_control=False)
#         return fmt_or_arg


from pykern.pkdebug import pkdc, pkdexc, pkdp
import os
import signal
import six
import subprocess
import threading
        import psutil, time
            # process starts. Then polling less frequently
            # helps avoid thrashing, especially with mpi.
            t = .5
        return s

    try:
        stdout = output
        if isinstance(output, six.string_types):
            stdout = open(output, 'w')
        stderr = subprocess.STDOUT if stdout else None
        for sig in _SIGNALS:
            signal.signal(sig, signal_handler)
        p = subprocess.Popen(
            cmd,
            stdin=open(os.devnull),
            stdout=stdout,
            stderr=stderr,
            env=env,
        )
        pid = p.pid
        if msg:
            msg('{}: started: {}', pid, cmd)
        s = wait_pid()
        p = None
        if s != 0:
            raise RuntimeError('error exit({})'.format(s))
        if msg:
            msg('{}: normal exit(0): {}', pid, cmd)
    except Exception as e:
        if msg:

# Path: pykern/pkdebug.py
# def pkdc(fmt, *args, **kwargs):
#     """Conditional print a message to `output` selectively based on `control`.
# 
#     Controlled output that you can leave in your code permanently.
# 
#     Args:
#         fmt (str): how to :func:`str.format`
#         args: what to format
#         kwargs: what to format
#     """
#     # Since calls are left in for product, this check has
#     # some value.
#     if _have_control:
#         _printer._write(fmt, args, kwargs, with_control=True)
# 
# def pkdexc():
#     """Return last exception and stack as a string
# 
#     Must be called from an ``except``. This function removes
#     the last two calls from the stack. It joins the traceback
#     so you get a complete stack trace.
# 
#     Will catch exceptions during the formatting and returns a
#     string in all cases.
# 
#     Example::
# 
#         try:
#             something
#         except:
#             pkdp(pkdexc())
# 
#     Returns:
#         str: formatted exception and stack trace
#     """
#     try:
#         e = sys.exc_info()
#         if hasattr(traceback, 'TracebackException'):
#             return ''.join(
#                 traceback.format_exception(*e) \
#                 + ['\nException was printed at:\n\n'] \
#                 + traceback.format_exception_only(e[0], e[1]) \
#                 + traceback.format_stack()[:-2],
#             )
# 
#         else:
#             return ''.join(
#                 traceback.format_exception_only(e[0], e[1]) \
#                 + traceback.format_stack()[:-2] \
#                 + traceback.format_tb(e[2]),
#             )
# 
#     except Exception as e:
#         return 'pykern.pkdebug.pkdexc: unable to retrieve exception info'
# 
# def pkdp(fmt_or_arg, *args, **kwargs):
#     """Print a message to `output` unconditionally, possibly returning its arg
# 
#     Use for print statements in your code that you don't intend
#     to keep in the system.
# 
#     Use `pkdlog` for permanent log messages.
# 
#     Args:
#         fmt_or_arg (object): how to :func:`str.format`, or object to print
#         args: what to format
#         kwargs: what to format
# 
#     Returns:
#         object: Will return fmt_or_arg, if args and kwargs are empty
#     """
#     if args or kwargs:
#         _printer._write(fmt_or_arg, args, kwargs, with_control=False)
#     else:
#         _printer._write('{}', [fmt_or_arg], {}, with_control=False)
#         return fmt_or_arg


# Path: pykern/pksubprocess.py
from pykern.pkdebug import pkdc, pkdexc, pkdp
import os
import signal
import six
import subprocess
import threading
        import psutil, time

            # process starts. Then polling less frequently
            # helps avoid thrashing, especially with mpi.
            t = .5
        return s

    try:
        stdout = output
        if isinstance(output, six.string_types):
            stdout = open(output, 'w')
        stderr = subprocess.STDOUT if stdout else None
        for sig in _SIGNALS:
            signal.signal(sig, signal_handler)
        p = subprocess.Popen(
            cmd,
            stdin=open(os.devnull),
            stdout=stdout,
            stderr=stderr,
            env=env,
        )
        pid = p.pid
        if msg:
            msg('{}: started: {}', pid, cmd)
        s = wait_pid()
        p = None
        if s != 0:
            raise RuntimeError('error exit({})'.format(s))
        if msg:
            msg('{}: normal exit(0): {}', pid, cmd)
    except Exception as e:
        if msg:

            msg('{}: exception: {} {}', pid, cmd, pkdexc())