blob: c8dd36df448ce70ca8a703ffc4f758d7a384b13d [file] [log] [blame]
Angel Pons7ec15c82018-11-17 09:47:27 +01001#!/usr/bin/env python2
Naresh G Solankia6464b72018-01-25 19:02:25 +05302# Copyright (C) 2018 Intel Corporation.
3# written by Naresh G Solanki<naresh.solanki@intel.com> and
4# Maulik V Vaghela <maulik.v.vaghela@intel.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; version 2 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15
16"""
17This utilty generate json output to post comment in gerrit.
18
19INPUT: output of checkpatch.pl.
20OUTPUT: json format output that can be used to post comment in gerrit
21"""
22import os
23import sys
24import json
25
26data = {}
27data['comments'] = []
28list_temp = {}
29
30def update_struct( file_path, msg_output, line_number):
31 if file_path not in list_temp:
32 list_temp[file_path] = []
33 list_temp[file_path].append({
34 "line" : line_number,
35 "message" : msg_output,}
36 )
37
38def parse_file(input_file):
39 fp = open (input_file, "r")
40 for line in fp:
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053041 if line.startswith("ERROR:"):
Naresh G Solankia6464b72018-01-25 19:02:25 +053042 msg_output = line.split("ERROR:")[1].strip()
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053043 elif line.startswith("WARNING:"):
Naresh G Solankia6464b72018-01-25 19:02:25 +053044 msg_output = line.split("WARNING:")[1].strip()
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053045 elif ": FILE:" in line:
Naresh G Solankia6464b72018-01-25 19:02:25 +053046 temp = line.split("FILE:")
47 file_path = temp[1].split(":")[0]
48 line_number = temp[1].split(":")[1]
49 update_struct( file_path.strip(), msg_output, str(line_number) )
50 else:
51 continue
52 fp.close()
53
54def main():
55 if (len(sys.argv) < 3) or (sys.argv[1] == "-h"):
56 print "HELP:"
57 print sys.argv[0] + " <input file> <output file in json>"
58 sys.exit()
59
60 print sys.argv[1]
61 parse_file(sys.argv[1])
62 data['comments'] = list_temp
63 print json.dumps(data)
64 out_file = open( sys.argv[2] , "w")
65 json.dump(data, out_file, sort_keys=True, indent=4)
66 out_file.close()
67
68if __name__ == "__main__":
69 main()