Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

coalib/coala_main.py (3 issues)

1
import os
2
3
from pyprint.ConsolePrinter import ConsolePrinter
4
5
from coalib import coala_delete_orig
6
from coalib.misc.Exceptions import get_exitcode
7
from coalib.output.Interactions import fail_acquire_settings
8
from coalib.output.printers.LogPrinter import LogPrinter
9
from coalib.output.Tagging import delete_tagged_results, tag_results
10
from coalib.processes.Processing import execute_section, simplify_section_result
11
from coalib.settings.ConfigurationGathering import gather_configuration
12
13
do_nothing = lambda *args: True
14
15
16
def run_coala(log_printer=None,
17
              print_results=do_nothing,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable do_nothing does not seem to be defined.
Loading history...
18
              acquire_settings=fail_acquire_settings,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable fail_acquire_settings does not seem to be defined.
Loading history...
19
              print_section_beginning=do_nothing,
20
              nothing_done=do_nothing,
21
              autoapply=True):
22
    """
23
    This is a main method that should be usable for almost all purposes and
24
    reduces executing coala to one function call.
25
26
    :param log_printer:             A LogPrinter object to use for logging.
27
    :param print_results:           A callback that takes a LogPrinter, a
28
                                    section, a list of results to be printed,
29
                                    the file dict and the mutable file diff
30
                                    dict.
31
    :param acquire_settings:        The method to use for requesting settings.
32
                                    It will get a parameter which is a
33
                                    dictionary with the settings name as key
34
                                    and a list containing a description in [0]
35
                                    and the names of the bears who need this
36
                                    setting in all following indexes.
37
    :param print_section_beginning: A callback that will be called with a
38
                                    section name string whenever analysis of a
39
                                    new section is started.
40
    :param nothing_done:            A callback that will be called with only a
41
                                    log printer that shall indicate that
42
                                    nothing was done.
43
    :param autoapply:               Set to False to autoapply nothing by
44
                                    default; this is overridable via any
45
                                    configuration file/CLI.
46
    :return:                        A dictionary containing a list of results
47
                                    for all analyzed sections as key.
48
    """
49
    log_printer = log_printer or LogPrinter(ConsolePrinter())
50
51
    exitcode = 0
52
    results = {}
53
    file_dicts = {}
54
    try:
55
        yielded_results = yielded_unfixed_results = False
56
        did_nothing = True
57
        sections, local_bears, global_bears, targets = gather_configuration(
58
            acquire_settings,
59
            log_printer,
60
            autoapply=autoapply)
61
62
        tag = str(sections['default'].get('tag', None))
63
        dtag = str(sections['default'].get('dtag', None))
64
        config_file = os.path.abspath(str(sections["default"].get("config")))
65
66
        # Deleting all .orig files, so the latest files are up to date!
67
        coala_delete_orig.main(log_printer, sections["default"])
68
69
        delete_tagged_results(dtag, config_file, log_printer)
70
71
        for section_name, section in sections.items():
72
            if not section.is_enabled(targets):
73
                continue
74
75
            print_section_beginning(section)
76
            section_result = execute_section(
77
                section=section,
78
                global_bear_list=global_bears[section_name],
79
                local_bear_list=local_bears[section_name],
80
                print_results=print_results,
81
                log_printer=log_printer)
82
            yielded, yielded_unfixed, results[section_name] = (
83
                simplify_section_result(section_result))
84
85
            yielded_results = yielded_results or yielded
86
            yielded_unfixed_results = (
87
                yielded_unfixed_results or yielded_unfixed)
88
            did_nothing = False
89
90
            file_dicts[section_name] = section_result[3]
91
92
        tag_results(tag, config_file, results, log_printer)
93
94
        if did_nothing:
95
            nothing_done(log_printer)
96
        elif yielded_unfixed_results:
97
            exitcode = 1
98
        elif yielded_results:
99
            exitcode = 5
100
    except BaseException as exception:  # pylint: disable=broad-except
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable BaseException does not seem to be defined.
Loading history...
101
        exitcode = exitcode or get_exitcode(exception, log_printer)
102
103
    return results, exitcode, file_dicts
104