Failed Conditions
Pull Request — master (#1201)
by Lasse
01:53
created

coalib/coala_main.py (1 issue)

1
from itertools import chain
2
from pyprint.ConsolePrinter import ConsolePrinter
3
import os
4
5
from coalib.output.printers.LogPrinter import LogPrinter
6
from coalib.processes.Processing import execute_section
7
from coalib.results.HiddenResult import HiddenResult
0 ignored issues
show
Unused HiddenResult imported from coalib.results.HiddenResult
Loading history...
8
from coalib.settings.ConfigurationGathering import gather_configuration
9
from coalib.misc.Exceptions import get_exitcode
10
from coalib.bears.BEAR_KIND import BEAR_KIND
11
from coalib.collecting.Collectors import collect_bears
12
from coalib.output.Interactions import fail_acquire_settings
13
from coalib.output.Tagging import tag_results, delete_tagged_results
14
15
16
do_nothing = lambda *args: True
17
18
19
def run_coala(log_printer=None,
20
              print_results=do_nothing,
21
              acquire_settings=fail_acquire_settings,
22
              print_section_beginning=do_nothing,
23
              nothing_done=do_nothing,
24
              show_bears=do_nothing):
25
    """
26
    This is a main method that should be usable for almost all purposes and
27
    reduces executing coala to one function call.
28
29
    :param log_printer:             A LogPrinter object to use for logging.
30
    :param print_results:           A callback that takes a LogPrinter, a
31
                                    section, a list of results to be printed,
32
                                    the file dict and the mutable file diff
33
                                    dict.
34
    :param acquire_settings:        The method to use for requesting settings.
35
                                    It will get a parameter which is a
36
                                    dictionary with the settings name as key
37
                                    and a list containing a description in [0]
38
                                    and the names of the bears who need this
39
                                    setting in all following indexes.
40
    :param print_section_beginning: A callback that will be called with a
41
                                    section name string whenever analysis of a
42
                                    new section is started.
43
    :param nothing_done:            A callback that will be called with only a
44
                                    log printer that shall indicate that
45
                                    nothing was done.
46
    :param show_bears:              A callback that will be called with first
47
                                    a list of local bears, second a list of
48
                                    global bears to output them. A third bool
49
                                    parameter may be used to indicate if a
50
                                    compressed output (True) or a normal output
51
                                    (False) is desired, the former being used
52
                                    for showing all available bears to the
53
                                    user.
54
    :return:                        A dictionary containing a list of results
55
                                    for all analyzed sections as key.
56
    """
57
    log_printer = log_printer or LogPrinter(ConsolePrinter())
58
59
    exitcode = 0
60
    results = None
61
    try:
62
        yielded_results = False
63
        did_nothing = True
64
        sections, local_bears, global_bears, targets = (
65
            gather_configuration(acquire_settings, log_printer))
66
67
        tag = str(sections['default'].get('tag', None))
68
        dtag = str(sections['default'].get('dtag', None))
69
70
        show_all_bears = bool(sections['default'].get('show_all_bears', False))
71
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
72
        if show_all_bears:
73
            show_bears_ = True
74
            for section in sections:
75
                bear_dirs = sections[section].bear_dirs()
76
                local_bears[section] = collect_bears(bear_dirs,
77
                                                     ["**"],
78
                                                     [BEAR_KIND.LOCAL],
79
                                                     log_printer)
80
                global_bears[section] = collect_bears(bear_dirs,
81
                                                      ["**"],
82
                                                      [BEAR_KIND.GLOBAL],
83
                                                      log_printer)
84
85
        if dtag != "None":
86
            delete_tagged_results(
87
                dtag,
88
                os.path.abspath(str(sections["default"].get("config"))))
89
90
        if show_bears_:
91
            show_bears(local_bears,
92
                       global_bears,
93
                       show_all_bears)
94
            did_nothing = False
95
        else:
96
            results = {}
97
            for section_name in sections:
98
                section = sections[section_name]
99
                if not section.is_enabled(targets):
100
                    continue
101
102
                print_section_beginning(section)
103
                section_result = execute_section(
104
                    section=section,
105
                    global_bear_list=global_bears[section_name],
106
                    local_bear_list=local_bears[section_name],
107
                    print_results=print_results,
108
                    log_printer=log_printer)
109
                yielded_results = yielded_results or section_result[0]
110
111
                results_for_section = []
112
                for value in chain(section_result[1].values(),
113
                                   section_result[2].values()):
114
                    if value is None:
115
                        continue
116
117
                    for result in value:
118
                        results_for_section.append(result)
119
120
                results[section_name] = results_for_section
121
                did_nothing = False
122
123
            if tag != "None":
124
                tag_results(
125
                    tag,
126
                    os.path.abspath(str(sections["default"].get("config"))),
127
                    results)
128
129
        if did_nothing:
130
            nothing_done(log_printer)
131
132
        if yielded_results:
133
            exitcode = 1
134
    except BaseException as exception:  # pylint: disable=broad-except
135
        exitcode = exitcode or get_exitcode(exception, log_printer)
136
137
    return results, exitcode
138