2015-09-22 19:13:06 +02:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
2018-10-20 02:16:55 +02:00
|
|
|
import argparse
|
2015-09-22 19:13:06 +02:00
|
|
|
import ast
|
2018-10-20 02:16:55 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2015-09-22 19:13:06 +02:00
|
|
|
import yaml
|
|
|
|
|
2018-10-20 02:16:55 +02:00
|
|
|
|
2015-09-22 19:13:06 +02:00
|
|
|
class DefinitionVisitor(ast.NodeVisitor):
|
|
|
|
def __init__(self):
|
2020-09-18 15:56:44 +02:00
|
|
|
super().__init__()
|
2015-09-22 19:13:06 +02:00
|
|
|
self.functions = {}
|
|
|
|
self.classes = {}
|
|
|
|
self.names = {}
|
|
|
|
self.attrs = set()
|
|
|
|
self.definitions = {
|
|
|
|
"def": self.functions,
|
|
|
|
"class": self.classes,
|
|
|
|
"names": self.names,
|
|
|
|
"attrs": self.attrs,
|
|
|
|
}
|
|
|
|
|
|
|
|
def visit_Name(self, node):
|
|
|
|
self.names.setdefault(type(node.ctx).__name__, set()).add(node.id)
|
|
|
|
|
|
|
|
def visit_Attribute(self, node):
|
|
|
|
self.attrs.add(node.attr)
|
|
|
|
for child in ast.iter_child_nodes(node):
|
|
|
|
self.visit(child)
|
|
|
|
|
|
|
|
def visit_ClassDef(self, node):
|
|
|
|
visitor = DefinitionVisitor()
|
|
|
|
self.classes[node.name] = visitor.definitions
|
|
|
|
for child in ast.iter_child_nodes(node):
|
|
|
|
visitor.visit(child)
|
|
|
|
|
|
|
|
def visit_FunctionDef(self, node):
|
|
|
|
visitor = DefinitionVisitor()
|
|
|
|
self.functions[node.name] = visitor.definitions
|
|
|
|
for child in ast.iter_child_nodes(node):
|
|
|
|
visitor.visit(child)
|
|
|
|
|
|
|
|
|
|
|
|
def non_empty(defs):
|
|
|
|
functions = {name: non_empty(f) for name, f in defs["def"].items()}
|
|
|
|
classes = {name: non_empty(f) for name, f in defs["class"].items()}
|
|
|
|
result = {}
|
2018-10-20 02:16:55 +02:00
|
|
|
if functions:
|
|
|
|
result["def"] = functions
|
|
|
|
if classes:
|
|
|
|
result["class"] = classes
|
2015-09-22 19:13:06 +02:00
|
|
|
names = defs["names"]
|
|
|
|
uses = []
|
|
|
|
for name in names.get("Load", ()):
|
|
|
|
if name not in names.get("Param", ()) and name not in names.get("Store", ()):
|
|
|
|
uses.append(name)
|
|
|
|
uses.extend(defs["attrs"])
|
2018-10-20 02:16:55 +02:00
|
|
|
if uses:
|
|
|
|
result["uses"] = uses
|
2015-09-22 19:13:06 +02:00
|
|
|
result["names"] = names
|
|
|
|
result["attrs"] = defs["attrs"]
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def definitions_in_code(input_code):
|
|
|
|
input_ast = ast.parse(input_code)
|
|
|
|
visitor = DefinitionVisitor()
|
|
|
|
visitor.visit(input_ast)
|
|
|
|
definitions = non_empty(visitor.definitions)
|
|
|
|
return definitions
|
|
|
|
|
|
|
|
|
|
|
|
def definitions_in_file(filepath):
|
|
|
|
with open(filepath) as f:
|
|
|
|
return definitions_in_code(f.read())
|
|
|
|
|
|
|
|
|
|
|
|
def defined_names(prefix, defs, names):
|
|
|
|
for name, funcs in defs.get("def", {}).items():
|
|
|
|
names.setdefault(name, {"defined": []})["defined"].append(prefix + name)
|
|
|
|
defined_names(prefix + name + ".", funcs, names)
|
|
|
|
|
|
|
|
for name, funcs in defs.get("class", {}).items():
|
|
|
|
names.setdefault(name, {"defined": []})["defined"].append(prefix + name)
|
|
|
|
defined_names(prefix + name + ".", funcs, names)
|
|
|
|
|
|
|
|
|
2015-12-01 15:29:42 +01:00
|
|
|
def used_names(prefix, item, defs, names):
|
2015-09-22 19:13:06 +02:00
|
|
|
for name, funcs in defs.get("def", {}).items():
|
2015-12-01 15:29:42 +01:00
|
|
|
used_names(prefix + name + ".", name, funcs, names)
|
2015-09-22 19:13:06 +02:00
|
|
|
|
|
|
|
for name, funcs in defs.get("class", {}).items():
|
2015-12-01 15:29:42 +01:00
|
|
|
used_names(prefix + name + ".", name, funcs, names)
|
2015-09-22 19:13:06 +02:00
|
|
|
|
2016-03-17 11:59:12 +01:00
|
|
|
path = prefix.rstrip(".")
|
2015-09-22 19:13:06 +02:00
|
|
|
for used in defs.get("uses", ()):
|
|
|
|
if used in names:
|
2016-03-17 11:59:12 +01:00
|
|
|
if item:
|
|
|
|
names[item].setdefault("uses", []).append(used)
|
|
|
|
names[used].setdefault("used", {}).setdefault(item, []).append(path)
|
2015-09-22 19:13:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-09-23 11:17:50 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Find definitions.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--unused", action="store_true", help="Only list unused definitions"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ignore", action="append", metavar="REGEXP", help="Ignore a pattern"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"--pattern", action="append", metavar="REGEXP", help="Search for a pattern"
|
2015-09-23 11:17:50 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"directories",
|
|
|
|
nargs="+",
|
|
|
|
metavar="DIR",
|
|
|
|
help="Directories to search for definitions",
|
2015-09-23 11:17:50 +02:00
|
|
|
)
|
2015-12-01 15:29:42 +01:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"--referrers",
|
|
|
|
default=0,
|
|
|
|
type=int,
|
|
|
|
help="Include referrers up to the given depth",
|
2015-12-01 15:29:42 +01:00
|
|
|
)
|
2016-03-17 11:59:12 +01:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"--referred",
|
|
|
|
default=0,
|
|
|
|
type=int,
|
|
|
|
help="Include referred down to the given depth",
|
2016-03-17 11:59:12 +01:00
|
|
|
)
|
2015-12-01 15:29:42 +01:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"--format", default="yaml", help="Output format, one of 'yaml' or 'dot'"
|
2015-12-01 15:29:42 +01:00
|
|
|
)
|
2015-09-23 11:17:50 +02:00
|
|
|
args = parser.parse_args()
|
2015-09-22 19:13:06 +02:00
|
|
|
|
|
|
|
definitions = {}
|
2015-09-23 11:17:50 +02:00
|
|
|
for directory in args.directories:
|
2021-04-20 12:50:49 +02:00
|
|
|
for root, _, files in os.walk(directory):
|
2015-09-23 11:17:50 +02:00
|
|
|
for filename in files:
|
|
|
|
if filename.endswith(".py"):
|
|
|
|
filepath = os.path.join(root, filename)
|
|
|
|
definitions[filepath] = definitions_in_file(filepath)
|
2015-09-22 19:13:06 +02:00
|
|
|
|
|
|
|
names = {}
|
|
|
|
for filepath, defs in definitions.items():
|
|
|
|
defined_names(filepath + ":", defs, names)
|
|
|
|
|
|
|
|
for filepath, defs in definitions.items():
|
2015-12-01 15:29:42 +01:00
|
|
|
used_names(filepath + ":", None, defs, names)
|
2015-09-22 19:13:06 +02:00
|
|
|
|
2015-09-23 11:17:50 +02:00
|
|
|
patterns = [re.compile(pattern) for pattern in args.pattern or ()]
|
|
|
|
ignore = [re.compile(pattern) for pattern in args.ignore or ()]
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
for name, definition in names.items():
|
|
|
|
if patterns and not any(pattern.match(name) for pattern in patterns):
|
|
|
|
continue
|
|
|
|
if ignore and any(pattern.match(name) for pattern in ignore):
|
|
|
|
continue
|
|
|
|
if args.unused and definition.get("used"):
|
|
|
|
continue
|
|
|
|
result[name] = definition
|
|
|
|
|
2015-12-01 15:29:42 +01:00
|
|
|
referrer_depth = args.referrers
|
|
|
|
referrers = set()
|
|
|
|
while referrer_depth:
|
|
|
|
referrer_depth -= 1
|
|
|
|
for entry in result.values():
|
2015-12-01 22:17:58 +01:00
|
|
|
for used_by in entry.get("used", ()):
|
2015-12-01 15:29:42 +01:00
|
|
|
referrers.add(used_by)
|
|
|
|
for name, definition in names.items():
|
2018-10-20 02:16:55 +02:00
|
|
|
if name not in referrers:
|
2015-12-01 15:29:42 +01:00
|
|
|
continue
|
|
|
|
if ignore and any(pattern.match(name) for pattern in ignore):
|
|
|
|
continue
|
|
|
|
result[name] = definition
|
|
|
|
|
2016-03-17 11:59:12 +01:00
|
|
|
referred_depth = args.referred
|
|
|
|
referred = set()
|
|
|
|
while referred_depth:
|
|
|
|
referred_depth -= 1
|
|
|
|
for entry in result.values():
|
|
|
|
for uses in entry.get("uses", ()):
|
|
|
|
referred.add(uses)
|
|
|
|
for name, definition in names.items():
|
2018-10-20 02:16:55 +02:00
|
|
|
if name not in referred:
|
2016-03-17 11:59:12 +01:00
|
|
|
continue
|
|
|
|
if ignore and any(pattern.match(name) for pattern in ignore):
|
|
|
|
continue
|
|
|
|
result[name] = definition
|
|
|
|
|
2015-12-01 15:29:42 +01:00
|
|
|
if args.format == "yaml":
|
|
|
|
yaml.dump(result, sys.stdout, default_flow_style=False)
|
|
|
|
elif args.format == "dot":
|
2018-10-20 02:16:55 +02:00
|
|
|
print("digraph {")
|
2015-12-01 15:29:42 +01:00
|
|
|
for name, entry in result.items():
|
2018-10-20 02:16:55 +02:00
|
|
|
print(name)
|
2015-12-01 15:29:42 +01:00
|
|
|
for used_by in entry.get("used", ()):
|
|
|
|
if used_by in result:
|
2018-10-20 02:16:55 +02:00
|
|
|
print(used_by, "->", name)
|
|
|
|
print("}")
|
2015-12-01 15:29:42 +01:00
|
|
|
else:
|
|
|
|
raise ValueError("Unknown format %r" % (args.format))
|