Failed Conditions
Pull Request — master (#1311)
by Sudheesh
01:49
created

coalib.main()   B

Complexity

Conditions 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 31
rs 8.0894
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.coala_main import run_coala
17
from coalib.output.JSONEncoder import JSONEncoder
18
from coalib.output.printers.ListLogPrinter import ListLogPrinter
19
from coalib.parsing.DefaultArgParser import default_arg_parser
20
21
22
def main():
23
    # Note: We parse the args here once to find the log printer to use.
24
    #       Also, commands like -h (help) and -v (version) are executed here.
25
    #       The args are again parsed later to find the settings and configs
26
    #       to use during analysis.
27
    arg_parser = default_arg_parser()
28
    args = arg_parser.parse_args()
29
30
    log_printer = None if args.text_logs else ListLogPrinter()
31
    results, exitcode = run_coala(log_printer=log_printer, autoapply=False)
32
33
    retval = {"results": results}
34
    if not args.text_logs:
35
        retval["logs"] = log_printer.logs
36
37
    if args.output:
38
        filename = str(args.output)
39
        with open(filename, 'w+') as fp:
40
            json.dump(retval, fp,
41
                      cls=JSONEncoder,
42
                      sort_keys=True,
43
                      indent=2,
44
                      separators=(',', ': '))
45
    else:
46
        print(json.dumps(retval,
47
                         cls=JSONEncoder,
48
                         sort_keys=True,
49
                         indent=2,
50
                         separators=(',', ': ')))
51
52
    return exitcode
53