Skip to content

Instantly share code, notes, and snippets.

@novakboskov
Last active September 7, 2017 15:01
Show Gist options
  • Save novakboskov/f4af5ccee5b6754fbd162cb330dd43d9 to your computer and use it in GitHub Desktop.
Save novakboskov/f4af5ccee5b6754fbd162cb330dd43d9 to your computer and use it in GitHub Desktop.
"""This script checks if some function or method is called in a python
file and prints calls locations if any.
Usage example:
python function_detector.py play.py print cos m
"""
import ast
import sys
import os
class FunctionDetector(ast.NodeTransformer):
def __init__(self, foo_names):
super(FunctionDetector, self).__init__()
self.foo_names = foo_names
def visit_Call(self, n):
name = None
if isinstance(n.func, ast.Name) and n.func.id in self.foo_names:
name = n.func.id
elif isinstance(n.func, ast.Attribute) \
and n.func.attr in self.foo_names:
name = n.func.attr
if name:
print('{} is called at: {}:{}{}'.format(
name, n.lineno, n.col_offset, os.linesep))
self.generic_visit(n)
return n
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
AST = ast.parse(f.read())
FunctionDetector(sys.argv[2:]).visit(AST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment