pre-commit-hooks/pre_commit_hooks/debug_statement_hook.py

87 lines
2.2 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
2014-03-15 05:23:08 +08:00
import argparse
import ast
import traceback
from collections.abc import Sequence
2020-02-06 03:10:42 +08:00
from typing import NamedTuple
2014-03-15 05:23:08 +08:00
DEBUG_STATEMENTS = {
'bpdb',
'ipdb',
'pdb',
2021-06-15 15:28:11 +08:00
'pdbr',
'pudb',
'pydevd_pycharm',
'q',
'rdb',
'rpdb',
'wdb',
}
2020-02-06 03:10:42 +08:00
class Debug(NamedTuple):
line: int
col: int
name: str
reason: str
2014-03-15 05:23:08 +08:00
class DebugStatementParser(ast.NodeVisitor):
2020-02-06 03:10:42 +08:00
def __init__(self) -> None:
self.breakpoints: list[Debug] = []
2014-03-15 05:23:08 +08:00
2020-02-06 03:10:42 +08:00
def visit_Import(self, node: ast.Import) -> None:
for name in node.names:
if name.name in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, name.name, 'imported')
self.breakpoints.append(st)
2014-03-15 05:23:08 +08:00
2020-02-06 03:10:42 +08:00
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
2014-03-15 05:23:08 +08:00
if node.module in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, node.module, 'imported')
self.breakpoints.append(st)
2014-03-15 05:23:08 +08:00
2020-02-06 03:10:42 +08:00
def visit_Call(self, node: ast.Call) -> None:
"""python3.7+ breakpoint()"""
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')
self.breakpoints.append(st)
self.generic_visit(node)
2014-03-15 05:23:08 +08:00
2020-02-06 03:10:42 +08:00
def check_file(filename: str) -> int:
try:
2018-06-18 15:00:38 +08:00
with open(filename, 'rb') as f:
ast_obj = ast.parse(f.read(), filename=filename)
except SyntaxError:
2020-02-06 03:10:42 +08:00
print(f'{filename} - Could not parse ast')
print()
print('\t' + traceback.format_exc().replace('\n', '\n\t'))
print()
return 1
visitor = DebugStatementParser()
2014-03-15 05:23:08 +08:00
visitor.visit(ast_obj)
for bp in visitor.breakpoints:
print(f'{filename}:{bp.line}:{bp.col}: {bp.name} {bp.reason}')
return int(bool(visitor.breakpoints))
2014-03-15 05:23:08 +08:00
def main(argv: Sequence[str] | None = None) -> int:
2014-03-15 05:23:08 +08:00
parser = argparse.ArgumentParser()
2014-03-15 05:51:42 +08:00
parser.add_argument('filenames', nargs='*', help='Filenames to run')
2014-03-15 06:18:29 +08:00
args = parser.parse_args(argv)
2014-03-15 05:23:08 +08:00
retv = 0
for filename in args.filenames:
retv |= check_file(filename)
2014-03-15 05:23:08 +08:00
return retv
if __name__ == '__main__':
raise SystemExit(main())