Last active
September 7, 2018 13:22
-
-
Save coldfix/216c28c541fee6cf00a709ce7b7dc4db to your computer and use it in GitHub Desktop.
pyflakes precommit hook, checks the index directly, needs importable pyflakes
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 | |
import sys | |
import re | |
from subprocess import check_output, PIPE | |
import pyflakes.api | |
def git_file_content(fname): | |
"""Return file content as on git index.""" | |
return check_output(['git', 'show', ':'+fname]) | |
def get_modified_files(ext): | |
modified = re.compile(r'^(?:M|A).\s*(?P<name>.*\.%s)' % ext) | |
output = check_output(['git', 'status', '--porcelain']).decode('utf-8') | |
return [match.group('name') | |
for line in output.splitlines() | |
for match in [modified.match(line.strip())] | |
if match] | |
def check_python(): | |
"""Check modified python files in the index. | |
Return number of warnings.""" | |
return sum([pyflakes.api.check(git_file_content(fname), fname) | |
for fname in get_modified_files('py')]) | |
def main(): | |
sys.exit(check_python()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment