File: code_structure_report.py

package info (click to toggle)
displaycal-py3 3.9.17-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 29,124 kB
  • sloc: python: 115,810; javascript: 11,545; xml: 598; sh: 257; makefile: 173
file content (56 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-

import os
import importlib
import inspect
import pkgutil
from inspect import isfunction, isclass, ismethod

import DisplayCAL


def main():
    """count the number of lines each method and function takes and report them, so we can see the progress.

    The motivation of this script was to prove that for some code paths the functions/methods are extremely long (I saw
    function more than 5000+ lines of code, this is one single function, and yes this script was to prove that it really
    exist). This is not a good approach for maintainable code.
    """
    report = {}

    path_traversal = [os.path.dirname(DisplayCAL.__file__)]
    for module_info in pkgutil.walk_packages(path_traversal):
        module_name = module_info.name
        print(module_name)
        try:
            # pkgutil.get_loader(module_info)
            module = importlib.import_module("DisplayCAL.%s" % module_info.name)
        except ModuleNotFoundError:
            continue

        all_functions = inspect.getmembers(module, isfunction)
        for func_name, func in all_functions:
            source_lines = inspect.getsourcelines(func)[0]
            source_length = len(source_lines)
            key = "%s.%s" % (module_name, func_name)
            report[key] = source_length
            print(key)

        all_classes = inspect.getmembers(module, isclass)
        for class_name, class_ in all_classes:
            methods = inspect.getmembers(class_, ismethod)
            for method_name, method in methods:
                try:
                    source_lines = inspect.getsourcelines(method)[0]
                    source_length = len(source_lines)
                    key = "%s.%s.%s" % (module_name, class_name, method_name)
                    report[key] = source_length
                    print(key)
                except TypeError:
                    pass

    print(report)


if __name__ == "__main__":
    main()