2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-03-15 05:23:08 +08:00
|
|
|
import argparse
|
|
|
|
import ast
|
2014-06-20 08:41:53 +08:00
|
|
|
import traceback
|
2024-10-12 07:30:07 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-07-01 23:15:46 +08:00
|
|
|
DEBUG_STATEMENTS = {
|
2023-08-02 21:47:15 +08:00
|
|
|
'bpdb',
|
2020-07-01 23:15:46 +08:00
|
|
|
'ipdb',
|
|
|
|
'pdb',
|
2021-06-15 15:28:11 +08:00
|
|
|
'pdbr',
|
2020-07-01 23:15:46 +08:00
|
|
|
'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
|
|
|
|
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
class DebugStatementParser(ast.NodeVisitor):
|
2020-02-06 03:10:42 +08:00
|
|
|
def __init__(self) -> None:
|
2022-01-16 08:24:05 +08:00
|
|
|
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:
|
2018-05-14 06:24:42 +08:00
|
|
|
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:
|
2018-05-14 06:24:42 +08:00
|
|
|
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:
|
2018-05-14 06:24:42 +08:00
|
|
|
"""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
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
|
2020-02-06 03:10:42 +08:00
|
|
|
def check_file(filename: str) -> int:
|
2014-06-20 08:41:53 +08:00
|
|
|
try:
|
2018-06-18 15:00:38 +08:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
ast_obj = ast.parse(f.read(), filename=filename)
|
2014-06-20 08:41:53 +08:00
|
|
|
except SyntaxError:
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'{filename} - Could not parse ast')
|
2014-06-20 08:41:53 +08:00
|
|
|
print()
|
|
|
|
print('\t' + traceback.format_exc().replace('\n', '\n\t'))
|
|
|
|
print()
|
|
|
|
return 1
|
2018-05-14 06:24:42 +08:00
|
|
|
|
|
|
|
visitor = DebugStatementParser()
|
2014-03-15 05:23:08 +08:00
|
|
|
visitor.visit(ast_obj)
|
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
for bp in visitor.breakpoints:
|
2022-04-07 04:55:26 +08:00
|
|
|
print(f'{filename}:{bp.line}:{bp.col}: {bp.name} {bp.reason}')
|
2018-05-14 06:24:42 +08:00
|
|
|
|
|
|
|
return int(bool(visitor.breakpoints))
|
2014-03-15 05:23:08 +08:00
|
|
|
|
2018-05-14 06:24:42 +08:00
|
|
|
|
2022-01-16 08:24:05 +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:
|
2018-05-14 06:24:42 +08:00
|
|
|
retv |= check_file(filename)
|
2014-03-15 05:23:08 +08:00
|
|
|
return retv
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|