Debian Codegraph @

util/code_structure_report.py

raw
1 import os
2 import importlib
3 import inspect
4 import pkgutil
5 from inspect import isfunction, isclass, ismethod
6
7 import DisplayCAL
8
9
10 def main():
11 """count the number of lines each method and function takes and report them, so we can see the progress.
12
13 The motivation of this script was to prove that for some code paths the functions/methods are extremely long (I saw
14 function more than 5000+ lines of code, this is one single function, and yes this script was to prove that it really
15 exist). This is not a good approach for maintainable code.
16 """
17 report = {}
18
19 path_traversal = [os.path.dirname(DisplayCAL.__file__)]
20 for module_info in pkgutil.walk_packages(path_traversal):
21 module_name = module_info.name
22 print(module_name)
23 try:
24 # pkgutil.get_loader(module_info)
25 module = importlib.import_module("DisplayCAL.%s" % module_info.name)
26 except ModuleNotFoundError:
27 continue
28
29 all_functions = inspect.getmembers(module, isfunction)
30 for func_name, func in all_functions:
31 source_lines = inspect.getsourcelines(func)[0]
32 source_length = len(source_lines)
33 key = "%s.%s" % (module_name, func_name)
34 report[key] = source_length
35 print(key)
36
37 all_classes = inspect.getmembers(module, isclass)
38 for class_name, class_ in all_classes:
39 methods = inspect.getmembers(class_, ismethod)
40 for method_name, method in methods:
41 try:
42 source_lines = inspect.getsourcelines(method)[0]
43 source_length = len(source_lines)
44 key = "%s.%s.%s" % (module_name, class_name, method_name)
45 report[key] = source_length
46 print(key)
47 except TypeError:
48 pass
49
50 print(report)
51
52
53 if __name__ == "__main__":
54 main()