forked from python-security/pyt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_counter.py
More file actions
49 lines (39 loc) · 1.36 KB
/
func_counter.py
File metadata and controls
49 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Module used for counting number of functions
to get an estimate og how big the CFG should be"""
import ast
from pyt.cfg import generate_ast, get_call_names_as_string
from pyt.project_handler import get_modules
function_calls = list()
functions = dict()
classes = dict()
class Counter(ast.NodeVisitor):
def visit_Call(self, node):
n = get_call_names_as_string(node.func)
function_calls.append(n)
self.generic_visit(node)
# Husk return, save vars overhead
def visit_FunctionDef(self, node):
if node.name in functions:
node.name += '¤'
functions[node.name] = len(node.body)
for n in node.body:
self.visit(n)
def visit_ClassDef(self, node):
if node.name in classes:
node.name += '¤'
classes[node.name] = len(node.body)
for n in node.body:
self.visit(n)
if __name__ == '__main__':
module_paths = (m[1] for m in get_modules('../flaskbb/flaskbb'))
for p in module_paths:
print(p)
t = generate_ast(p)
c = Counter()
c.visit(t)
max_func_len = max(functions.values())
max_class_len = max(classes.values())
restore_stuff = 6 # varies
print(len(function_calls))
print('estimate stuff: ', max_func_len*len(function_calls))
print('estimate stuff: ', max_class_len*len(function_calls))