Created
April 22, 2015 16:28
-
-
Save kalloc/b7c6365e53c8c917afdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# For Mac OS X's users: | |
# git clone [email protected]:int3h/SublimeFixPath.git \ | |
# ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/FixMacPath | |
# | |
import sys | |
import pep8 | |
import io | |
import os.path | |
import subprocess | |
try: | |
import pyflakes.api | |
except ImportError as e: | |
print("!!!! pip3 install pyflakes && pip install pyflakes !!!!") | |
raise | |
def validate_py(filename, contents): | |
result = pyflakes.api.checkPath(filename=filename) | |
lines = io.StringIO(contents).readlines() | |
result_pep8 = pep8.Checker( | |
filename=filename, | |
lines=lines, | |
ignore=["E711", "E712"], | |
max_line_length=99, | |
show_sources=True) | |
return not result and not result_pep8.check_all() | |
def validate_js(filename, contents): | |
proc = subprocess.Popen(["./node_modules/.bin/gulp", "jscs-stdin"], | |
stdin=subprocess.PIPE, cwd="front") | |
proc.communicate(contents.encode("utf-8")) | |
proc.stdin.close() | |
if proc.wait(): | |
return False | |
proc = subprocess.Popen(["./node_modules/.bin/gulp", "eslint-stdin"], | |
stdin=subprocess.PIPE, cwd="front") | |
proc.communicate(contents.encode("utf-8")) | |
proc.stdin.close() | |
if proc.wait(): | |
return False | |
return True | |
def validate_json(filename, contents): | |
return True | |
def validate_yml(filename, contents): | |
return True | |
def validate_styl(filename, contents): | |
return True | |
checks = { | |
".py": validate_py, | |
".js": validate_js, | |
# ".jsx": validate_js, | |
# ".json": validate_json, | |
# ".yml": validate_yml, | |
# ".styl": validate_styl | |
} | |
def read_staged(filename): | |
path = ":./" + filename | |
try: | |
return subprocess.check_output(["git", "cat-file", "-p", path], | |
stderr=subprocess.PIPE).decode("utf-8") | |
except subprocess.CalledProcessError: | |
return None | |
def hook(): | |
last = "HEAD" | |
if subprocess.call(["git", "rev-parse", "--verify", "HEAD"], | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE): | |
last = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" | |
lines = subprocess.check_output(["git", "diff-index", last, | |
"--cached", "--name-only"]) | |
valid = True | |
for filename in filter(None, lines.decode().split("\n")): | |
_, ext = os.path.splitext(filename) | |
if ext in checks: | |
contents = read_staged(filename) | |
if contents is None: | |
continue | |
if not checks[ext](filename, contents): | |
print("%s failed to validate" % filename) | |
valid = False | |
if not valid: | |
sys.exit(-1) | |
if __name__ == "__main__": | |
hook() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment