# Path: bento/utils/utils.py
# def pprint(color, s, fout=None):
#     if fout is None:
#         fout = sys.stderr
#     fout.write('%s%s%s\n' % (COLORS(color), s, COLORS('NORMAL')))
# 
# def extract_exception():
#     """Extract the last exception.
# 
#     Used to avoid the except ExceptionType as e, which cannot be written the
#     same across supported versions. I.e::
# 
#         try:
#             ...
#         except Exception, e:
#             ...
# 
#     becomes:
# 
#         try:
#             ...
#         except Exception:
#             e = extract_exception()
#     """
#     return sys.exc_info()[1]
# 
# def comma_list_split(s):
#     lexer = CommaListLexer(s)
#     ret = []
#     t = lexer.get_token()
#     while t != lexer.eof:
#         ret.append(t)
#         t = lexer.get_token()
# 
#     return ret
# 
# Path: bento/convert/utils.py
# def whole_test(setup_py, verbose, log):
#     if verbose:
#         show_output = True
#     else:
#         show_output = False
# 
#     if not test_can_run(setup_py, show_output, log):
#         pass
#     if verbose:
#         pprint("YELLOW", "----------------- Testing distutils ------------------")
#     use_distutils = test_distutils(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "----------------- Testing setuptools -----------------")
#     use_setuptools = test_setuptools(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "------------ Testing numpy.distutils -----------------")
#     use_numpy = test_numpy(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "--- Testing numpy.distutils patched by setuptools ----")
#     use_setuptools_numpy = test_setuptools_numpy(setup_py, show_output, log)
#     if verbose:
#         print("Is distutils ? %d" % use_distutils)
#         print("Is setuptools ? %d" % use_setuptools)
#         print("Is numpy distutils ? %d" % use_numpy)
#         print("Is setuptools numpy ? %d" % use_setuptools_numpy)
# 
#     if use_distutils and not (use_setuptools or use_numpy or use_setuptools_numpy):
#         return "distutils"
#     elif use_setuptools  and not (use_numpy or use_setuptools_numpy):
#         return "setuptools"
#     elif use_numpy  and not use_setuptools_numpy:
#         return "numpy.distutils"
#     elif use_setuptools_numpy:
#         return "setuptools + numpy.distutils converter"
#     else:
#         return "Unsupported converter"


import sys
import traceback
import optparse
import os.path as op
from six.moves \
    import \
        cStringIO
from bento.utils.utils \
    import \
        pprint, extract_exception, comma_list_split
from bento.core.package \
    import \
        static_representation
from bento.commands.core \
    import \
        Command
from bento.errors \
    import \
        UsageException, ConvertionError
from bento.convert.core \
    import \
        detect_monkeys, monkey_patch, analyse_setup_py, build_pkg
from bento.convert.utils \
    import \
        whole_test

        convert_log = "convert.log"
        log = open(convert_log, "w")
        try:
            try:
                convert(ctx, filename, setup_args, monkey_patch_mode, o.verbose, output, log, show_output)
            except ConvertionError:
                raise
            except Exception:
                e = extract_exception()
                log.write("Error while converting - traceback:\n")
                tb = sys.exc_info()[2]
                traceback.print_tb(tb, file=log)
                msg = "Error while converting %s - you may look at %s for " \
                      "details (Original exception: %s %s)" 
                raise ConvertionError(msg % (filename, convert_log, type(e), str(e)))
        finally:
            log.flush()
            log.close()

def convert(ctx, filename, setup_args, monkey_patch_mode, verbose, output, log, show_output=True):
    if monkey_patch_mode == "automatic":
        try:
            if verbose:
                pprint("PINK",
                       "Catching monkey (this may take a while) ...")
            monkey_patch_mode = detect_monkeys(filename, show_output, log)
            if verbose:
                pprint("PINK", "Detected mode: %s" % monkey_patch_mode)
        except ValueError:

# Path: bento/utils/utils.py
# def pprint(color, s, fout=None):
#     if fout is None:
#         fout = sys.stderr
#     fout.write('%s%s%s\n' % (COLORS(color), s, COLORS('NORMAL')))
# 
# def extract_exception():
#     """Extract the last exception.
# 
#     Used to avoid the except ExceptionType as e, which cannot be written the
#     same across supported versions. I.e::
# 
#         try:
#             ...
#         except Exception, e:
#             ...
# 
#     becomes:
# 
#         try:
#             ...
#         except Exception:
#             e = extract_exception()
#     """
#     return sys.exc_info()[1]
# 
# def comma_list_split(s):
#     lexer = CommaListLexer(s)
#     ret = []
#     t = lexer.get_token()
#     while t != lexer.eof:
#         ret.append(t)
#         t = lexer.get_token()
# 
#     return ret
# 
# Path: bento/convert/utils.py
# def whole_test(setup_py, verbose, log):
#     if verbose:
#         show_output = True
#     else:
#         show_output = False
# 
#     if not test_can_run(setup_py, show_output, log):
#         pass
#     if verbose:
#         pprint("YELLOW", "----------------- Testing distutils ------------------")
#     use_distutils = test_distutils(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "----------------- Testing setuptools -----------------")
#     use_setuptools = test_setuptools(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "------------ Testing numpy.distutils -----------------")
#     use_numpy = test_numpy(setup_py, show_output, log)
#     if verbose:
#         pprint("YELLOW", "--- Testing numpy.distutils patched by setuptools ----")
#     use_setuptools_numpy = test_setuptools_numpy(setup_py, show_output, log)
#     if verbose:
#         print("Is distutils ? %d" % use_distutils)
#         print("Is setuptools ? %d" % use_setuptools)
#         print("Is numpy distutils ? %d" % use_numpy)
#         print("Is setuptools numpy ? %d" % use_setuptools_numpy)
# 
#     if use_distutils and not (use_setuptools or use_numpy or use_setuptools_numpy):
#         return "distutils"
#     elif use_setuptools  and not (use_numpy or use_setuptools_numpy):
#         return "setuptools"
#     elif use_numpy  and not use_setuptools_numpy:
#         return "numpy.distutils"
#     elif use_setuptools_numpy:
#         return "setuptools + numpy.distutils converter"
#     else:
#         return "Unsupported converter"


# Path: bento/convert/commands.py
import sys
import traceback
import optparse
import os.path as op
from six.moves \
    import \
        cStringIO
from bento.utils.utils \
    import \
        pprint, extract_exception, comma_list_split
from bento.core.package \
    import \
        static_representation
from bento.commands.core \
    import \
        Command
from bento.errors \
    import \
        UsageException, ConvertionError
from bento.convert.core \
    import \
        detect_monkeys, monkey_patch, analyse_setup_py, build_pkg
from bento.convert.utils \
    import \
        whole_test


        convert_log = "convert.log"
        log = open(convert_log, "w")
        try:
            try:
                convert(ctx, filename, setup_args, monkey_patch_mode, o.verbose, output, log, show_output)
            except ConvertionError:
                raise
            except Exception:
                e = extract_exception()
                log.write("Error while converting - traceback:\n")
                tb = sys.exc_info()[2]
                traceback.print_tb(tb, file=log)
                msg = "Error while converting %s - you may look at %s for " \
                      "details (Original exception: %s %s)" 
                raise ConvertionError(msg % (filename, convert_log, type(e), str(e)))
        finally:
            log.flush()
            log.close()

def convert(ctx, filename, setup_args, monkey_patch_mode, verbose, output, log, show_output=True):
    if monkey_patch_mode == "automatic":
        try:
            if verbose:
                pprint("PINK",
                       "Catching monkey (this may take a while) ...")
            monkey_patch_mode = detect_monkeys(filename, show_output, log)
            if verbose:
                pprint("PINK", "Detected mode: %s" % monkey_patch_mode)
        except ValueError:

            e = extract_exception()