Completed
Pull Request — master (#1451)
by Abdeali
01:38
created

coalib.main()   B

Complexity

Conditions 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 31
rs 8.5806
1
# This program is free software: you can redistribute it and/or modify it
2
# under the terms of the GNU Affero General Public License as published by the
3
# Free Software Foundation, either version 3 of the License, or (at your
4
# option) any later version.
5
#
6
# This program is distributed in the hope that it will be useful, but WITHOUT
7
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
9
# for more details.
10
#
11
# You should have received a copy of the GNU Affero General Public License
12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
13
14
import json
15
16
from coalib.output.printers.ListLogPrinter import ListLogPrinter
17
from coalib.output.JSONEncoder import JSONEncoder
18
from coalib.coala_main import run_coala
19
from coalib.misc.Exceptions import get_exitcode
20
from coalib.parsing.DefaultArgParser import default_arg_parser
21
22
23
def main():
24
    # Note: We parse the args here once to find the log printer to use.
25
    #       Also, commands like -h (help) and -v (version) are executed here.
26
    #       The args are again parsed later to find the settings and configs
27
    #       to use during analysis.
28
    arg_parser = default_arg_parser()
29
    try:
30
        args = arg_parser.parse_args()
31
    except BaseException as exception:  # pylint: disable=broad-except
32
        return get_exitcode(exception)
33
34
    text_logs = args.text_logs
35
    if text_logs:
36
        # Use the default log printer in coala
37
        log_printer = None
38
    else:
39
        log_printer = ListLogPrinter()
40
41
    results, exitcode = run_coala(log_printer=log_printer, autoapply=False)
42
43
    retval = {"results": results}
44
    if not text_logs:
45
        retval["logs"] = log_printer.logs
46
47
    print(json.dumps(retval,
48
                     cls=JSONEncoder,
49
                     sort_keys=True,
50
                     indent=2,
51
                     separators=(',', ': ')))
52
53
    return exitcode
54