# Path: peru/async_helpers.py
# async def gather_coalescing_exceptions(coros, display, *, verbose):
#     '''The tricky thing about running multiple coroutines in parallel is what
#     we're supposed to do when one of them raises an exception. The approach
#     we're using here is to catch exceptions and keep waiting for other tasks to
#     finish. At the end, we reraise a GatheredExceptions error, if any
#     exceptions were caught.
# 
#     Another minor detail: We also want to make sure to start coroutines in the
#     order given, so that they end up appearing to the user alphabetically in
#     the fancy display. Note that asyncio.gather() puts coroutines in a set
#     internally, so we schedule coroutines *before* we give them to gather().
#     '''
# 
#     exceptions = []
#     reprs = []
# 
#     async def catching_wrapper(coro):
#         try:
#             return (await coro)
#         except Exception as e:
#             exceptions.append(e)
#             if isinstance(e, PrintableError) and not verbose:
#                 reprs.append(e.message)
#             else:
#                 reprs.append(traceback.format_exc())
#             return None
# 
#     # Suppress a deprecation warning in Python 3.5, while continuing to support
#     # 3.3 and early 3.4 releases.
#     if hasattr(asyncio, 'ensure_future'):
#         schedule = getattr(asyncio, 'ensure_future')
#     else:
#         schedule = getattr(asyncio, 'async')
# 
#     futures = [schedule(catching_wrapper(coro)) for coro in coros]
# 
#     results = await asyncio.gather(*futures)
# 
#     if exceptions:
#         raise GatheredExceptions(exceptions, reprs)
#     else:
#         return results
# 
# def run_task(coro):
#     return EVENT_LOOP.run_until_complete(coro)
# 
# Path: peru/runtime.py
# async def Runtime(args, env):
#     'This is the async constructor for the _Runtime class.'
#     r = _Runtime(args, env)
#     await r._init_cache()
#     return r


import collections
import json
import os
import sys
import tempfile
import docopt
from .async_helpers import gather_coalescing_exceptions, run_task
from . import compat
from .error import PrintableError
from . import imports
from . import parser
from .runtime import Runtime
def force_utf8_in_ascii_mode_hack():
    '''In systems without a UTF8 locale configured, Python will default to
    ASCII mode for stdout and stderr. This causes our fancy display to fail
    with encoding errors. In particular, you run into this if you try to run
    peru inside of Docker. This is a hack to force emitting UTF8 in that case.
    Hopefully it doesn't break anything important.'''
    if sys.stdout.encoding == 'ANSI_X3.4-1968':
        sys.stdout = open(
            sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
        sys.stderr = open(
            sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)


# Called as a setup.py entry point, or from __main__.py (`python3 -m peru`).
def main(*, argv=None, env=None, nocatch=False):
    force_utf8_in_ascii_mode_hack()

    if argv is None:
        argv = sys.argv[1:]
    if env is None:
        env = os.environ.copy()

    args = docopt_parse_args(argv)
    command = args['<command>']

    ret = maybe_print_help_and_return(args)
    if ret is not None:
        return ret

    try:

# Path: peru/async_helpers.py
# async def gather_coalescing_exceptions(coros, display, *, verbose):
#     '''The tricky thing about running multiple coroutines in parallel is what
#     we're supposed to do when one of them raises an exception. The approach
#     we're using here is to catch exceptions and keep waiting for other tasks to
#     finish. At the end, we reraise a GatheredExceptions error, if any
#     exceptions were caught.
# 
#     Another minor detail: We also want to make sure to start coroutines in the
#     order given, so that they end up appearing to the user alphabetically in
#     the fancy display. Note that asyncio.gather() puts coroutines in a set
#     internally, so we schedule coroutines *before* we give them to gather().
#     '''
# 
#     exceptions = []
#     reprs = []
# 
#     async def catching_wrapper(coro):
#         try:
#             return (await coro)
#         except Exception as e:
#             exceptions.append(e)
#             if isinstance(e, PrintableError) and not verbose:
#                 reprs.append(e.message)
#             else:
#                 reprs.append(traceback.format_exc())
#             return None
# 
#     # Suppress a deprecation warning in Python 3.5, while continuing to support
#     # 3.3 and early 3.4 releases.
#     if hasattr(asyncio, 'ensure_future'):
#         schedule = getattr(asyncio, 'ensure_future')
#     else:
#         schedule = getattr(asyncio, 'async')
# 
#     futures = [schedule(catching_wrapper(coro)) for coro in coros]
# 
#     results = await asyncio.gather(*futures)
# 
#     if exceptions:
#         raise GatheredExceptions(exceptions, reprs)
#     else:
#         return results
# 
# def run_task(coro):
#     return EVENT_LOOP.run_until_complete(coro)
# 
# Path: peru/runtime.py
# async def Runtime(args, env):
#     'This is the async constructor for the _Runtime class.'
#     r = _Runtime(args, env)
#     await r._init_cache()
#     return r


# Path: peru/main.py
import collections
import json
import os
import sys
import tempfile
import docopt
from .async_helpers import gather_coalescing_exceptions, run_task
from . import compat
from .error import PrintableError
from . import imports
from . import parser
from .runtime import Runtime

def force_utf8_in_ascii_mode_hack():
    '''In systems without a UTF8 locale configured, Python will default to
    ASCII mode for stdout and stderr. This causes our fancy display to fail
    with encoding errors. In particular, you run into this if you try to run
    peru inside of Docker. This is a hack to force emitting UTF8 in that case.
    Hopefully it doesn't break anything important.'''
    if sys.stdout.encoding == 'ANSI_X3.4-1968':
        sys.stdout = open(
            sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
        sys.stderr = open(
            sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)


# Called as a setup.py entry point, or from __main__.py (`python3 -m peru`).
def main(*, argv=None, env=None, nocatch=False):
    force_utf8_in_ascii_mode_hack()

    if argv is None:
        argv = sys.argv[1:]
    if env is None:
        env = os.environ.copy()

    args = docopt_parse_args(argv)
    command = args['<command>']

    ret = maybe_print_help_and_return(args)
    if ret is not None:
        return ret

    try:

        runtime = run_task(Runtime(args, env))