pre-commit-hooks/pre_commit_hooks/check_ast.py

34 lines
883 B
Python
Raw Permalink Normal View History

from __future__ import annotations
2015-08-05 04:45:41 +08:00
import argparse
import ast
2017-07-06 03:38:21 +08:00
import platform
2015-08-05 04:45:41 +08:00
import sys
import traceback
from collections.abc import Sequence
2015-08-05 04:45:41 +08:00
def main(argv: Sequence[str] | None = None) -> int:
2015-08-05 04:45:41 +08:00
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
retval = 0
for filename in args.filenames:
try:
2018-06-18 15:00:38 +08:00
with open(filename, 'rb') as f:
ast.parse(f.read(), filename=filename)
2015-08-05 04:45:41 +08:00
except SyntaxError:
2020-02-06 03:10:42 +08:00
impl = platform.python_implementation()
version = sys.version.split()[0]
print(f'{filename}: failed parsing with {impl} {version}:')
2020-02-04 00:41:48 +08:00
tb = ' ' + traceback.format_exc().replace('\n', '\n ')
2020-02-06 03:10:42 +08:00
print(f'\n{tb}')
2015-08-05 04:45:41 +08:00
retval = 1
return retval
if __name__ == '__main__':
raise SystemExit(main())