#!/usr/bin/env python
"""Runs code checkers on current directory

Written by Peter Duerr
"""
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import


from subprocess import Popen, PIPE
import sys
from datetime import datetime

from pyexperiment.utils.printers import print_blue  # pylint: disable=E0611

CHECKERS = [
    ['pep8', './pyexperiment'],
    ['pep8', './tests'],
    ['pep8', './lint'],
    ['pep8', './examples/minimal_cli'],
    ['pep8', './examples/minimal_config'],
    ['pep8', './examples/test_cli'],
    ['pep8', './examples/example'],
    ['pep8', './examples/minimal_logging'],
    ['pep8', './examples/stopwatch'],
    ['pep8', './examples/numbers'],
    ['pep8', './examples/replicate'],
    ['pep8', './examples/memoize'],
    ['pep8', './examples/benchmarks'],
    ['pylint', './pyexperiment', '--reports=n', '--disable=I0011'],
    ['pylint', './tests', '--reports=n', '--disable=I0011',
     '--ignore-imports=y'],
    ['pylint', './lint', '--reports=n', '--disable=I0011'],
    ['pylint', './examples/minimal_cli', '--reports=n'],
    ['pylint', './examples/minimal_config', '--reports=n', '--disable=I0011'],
    ['pylint', './examples/test_cli', '--reports=n'],
    ['pylint', './examples/example', '--reports=n', '--disable=I0011,E1101'],
    ['pylint', './examples/minimal_logging', '--reports=n',
     '--disable=I0011,E1101'],
    ['pylint', './examples/stopwatch', '--reports=n', '--disable=I0011,E1101'],
    ['pylint', './examples/numbers', '--reports=n'],
    ['pylint', './examples/optimize', '--reports=n', '--disable=I0011,E1101'],
    ['pylint', './examples/replicate', '--reports=n', '--disable=I0011,E1101'],
    ['pylint', './examples/memoize', '--reports=n', '--disable=I0011,E1101'],
    ['pylint', './examples/benchmarks', '--reports=n'],
]
"""List of checkers to run and their arguments
"""

if __name__ == "__main__":
    try:
        TIC = datetime.now()
        CHECKERS_RUN = 0
        ERROR = False
        for checker in CHECKERS:
            process = Popen(checker, stdout=PIPE)
            sys.stdout.write('.')
            sys.stdout.flush()
            CHECKERS_RUN += 1
            header = "Output from %s" % " ".join(checker)
            output = process.communicate()
            message = ""
            for elem in output:
                if elem is not None and not len(elem) == 0:
                    message += elem
                    ERROR = True
            if not message == "":
                print_blue(header)  # pylint: disable=no-member
                print_blue("=" * len(header))  # pylint: disable=no-member
                print(message)
                print_blue("=" * len(header))  # pylint: disable=no-member

        TOC = datetime.now()
        print("\n" + ''.join(['-']*70))
        print("Ran %i checkers in %fs\n" % (CHECKERS_RUN,
                                            (TOC - TIC).total_seconds()))

        if not ERROR:
            print("OK")
    except KeyboardInterrupt:
        pass
