#!/usr/bin/python
# __script__=hook
""" Pre-commit script for git"""

import subprocess
from python import git
from python import id_python
from python import lint


def main():
  print 'Beginning pre commit proceedures.'
  if git.has_exit_code('rev-parse', '--verify', 'HEAD'):
    against='4b825dc642cb6eb9a060e54bf8d69288fbee4904'
  else:
    against='HEAD'

  errors=''
  for file in (
      git.get_output('diff', '--cached', '--name-only', against).split()
      ):
    if id_python.is_python(file): 			
      print "Linting {0}".format(file),
      print_dots()
		
      is_valid, new_errors = lint.pylint(file)
      if is_valid:
        print "Passed"
      elif is_valid is None:
        print "Skipped"
      else:
        errors = "{0}Errors for {1}:\n{2}".format(
            errors,
            file,
            new_errors
						)
        print "Errors"
		
    print "Checking for ASCII in {0}".format(file),
    print_dots()
    try:
      file.split('/')[-1].decode('ascii')
    except UnicodeDecoderError:
      warning = "Warning: You have a non-ascii character in your file name."
      errors = "{0}\n{1}".format(errors, warning)
      print "Errors"
    else:
      print "Done"
	
  if errors:
    print "\n{0}".format(errors)
    exit(5)
  else:
    exit(0)

			
def print_dots():
	for each in range(10):
		print ".",
	
	
if __name__ == '__main__':
	main() 