#!/usr/bin/env python3
import os
import re
import shutil
import subprocess
import sys
import tempfile

sys.exit(0)

def system(*args, **kwargs):
    kwargs.setdefault('stdout', subprocess.PIPE)
    proc = subprocess.Popen(args, **kwargs)
    out, err = proc.communicate()
    return ((out or b"").decode("utf-8"), err)


def main():
    modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE)
    files, file_err = system('git', 'status', '--porcelain')
    files = modified.findall(files)
    tempdir = tempfile.mkdtemp()

    print("Scanning modified files...")
    for name in files:
        print("\t%s" % name)
        filename = os.path.join(tempdir, name)
        filepath = os.path.dirname(filename)
        if not os.path.exists(filepath):
            os.makedirs(filepath)
        with open(filename, 'w') as f:
            system('git', 'show', ':' + name, stdout=f)
    pep8_out, pep8_err = system('pep8', '.', cwd=tempdir)
    flakes_out, flakes_err = system('pyflakes', '.', cwd=tempdir)
    test_status = "OK"
    try:
        subprocess.check_call(['make', 'check'])
    except subprocess.CalledProcessError:
        test_status = "FAIL"
    shutil.rmtree(tempdir)

    if pep8_out or flakes_out or test_status == "FAIL":
        print("\nTest status: %s" % test_status)
        if pep8_out:
            print("\nThe following PEP8 violations were found:\n")
            print(pep8_out)
        else:
            print("\nPEP8 Status: OK\n")
        if flakes_out:
            print("The following Python flakes were found:\n")
            print(flakes_out)
        else:
            print("\nPython Flakes Status: OK\n")
        stdin = sys.stdin
        sys.stdin = open('/dev/tty')
        data = input("Commit anyway? [y/N] ")
        sys.stdin = stdin
        if data.lower() == "y":
            print("Committing ...")
            sys.exit(0)
        print("Aborting commit ...")
        sys.exit(1)


if __name__ == '__main__':
    main()
