mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-01 02:56:52 +00:00
Add a hook to verify python ast.
This commit is contained in:
parent
003e43251a
commit
8a8aaf5a60
5 changed files with 59 additions and 0 deletions
36
pre_commit_hooks/check_ast.py
Normal file
36
pre_commit_hooks/check_ast.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import ast
|
||||
import os.path
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
|
||||
def check_ast(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
_, interpreter = os.path.split(sys.executable)
|
||||
|
||||
retval = 0
|
||||
for filename in args.filenames:
|
||||
|
||||
try:
|
||||
ast.parse(open(filename, 'rb').read(), filename=filename)
|
||||
except SyntaxError:
|
||||
print('{0}: failed parsing with {1}:'.format(
|
||||
filename, interpreter,
|
||||
))
|
||||
print('\n{0}'.format(
|
||||
' ' + traceback.format_exc().replace('\n', '\n ')
|
||||
))
|
||||
retval = 1
|
||||
return retval
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(check_ast())
|
||||
Loading…
Add table
Add a link
Reference in a new issue