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

coalib.main()   B

Complexity

Conditions 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 33
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
    """
25
    >>> from coalib.tests.TestUtilities import execute_coala
26
    >>> from coalib.misc.Constants import VERSION
27
    >>> retval, output = execute_coala(main, 'coala-json', '-v')
28
    >>> retval
29
    0
30
    >>> assert output.strip() == VERSION
31
    """
32
    # Note: We parse the args here once to find the log printer to use.
33
    #       Also, commands like -h (help) and -v (version) are executed here.
34
    #       The args are again parsed later to find the settings and configs
35
    #       to use during analysis.
36
    arg_parser = default_arg_parser()
37
    try:
38
        args = arg_parser.parse_args()
39
    except BaseException as exception:  # Ignore PyLintBear
40
        return get_exitcode(exception)
41
42
    log_printer = None if args.text_logs else ListLogPrinter()
43
    results, exitcode = run_coala(log_printer=log_printer, autoapply=False)
44
45
    retval = {"results": results}
46
    if not args.text_logs:
47
        retval["logs"] = log_printer.logs
48
49
    print(json.dumps(retval,
50
                     cls=JSONEncoder,
51
                     sort_keys=True,
52
                     indent=2,
53
                     separators=(',', ': ')))
54
55
    return exitcode
56