blob: 341bc57733dff914e46d085825a19562d50c5785 [file] [log] [blame]
Stefan Reinauer57ead892020-06-12 14:38:29 -07001#!/usr/bin/env python3
Patrick Georgi7333a112020-05-08 20:48:04 +02002# SPDX-License-Identifier: GPL-2.0-only
Naresh G Solankia6464b72018-01-25 19:02:25 +05303
4"""
5This utilty generate json output to post comment in gerrit.
6
7INPUT: output of checkpatch.pl.
8OUTPUT: json format output that can be used to post comment in gerrit
9"""
10import os
11import sys
12import json
Martin Roth9e337232022-03-22 17:59:27 -060013import re
Naresh G Solankia6464b72018-01-25 19:02:25 +053014
15data = {}
16data['comments'] = []
17list_temp = {}
18
19def update_struct( file_path, msg_output, line_number):
20 if file_path not in list_temp:
21 list_temp[file_path] = []
22 list_temp[file_path].append({
Patrick Georgi39891c02021-03-15 21:07:05 +010023 "robot_id" : "checkpatch",
24 "robot_run_id" : sys.argv[3],
25 "url" : sys.argv[4],
Naresh G Solankia6464b72018-01-25 19:02:25 +053026 "line" : line_number,
27 "message" : msg_output,}
28 )
29
30def parse_file(input_file):
31 fp = open (input_file, "r")
32 for line in fp:
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053033 if line.startswith("ERROR:"):
Naresh G Solankia6464b72018-01-25 19:02:25 +053034 msg_output = line.split("ERROR:")[1].strip()
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053035 elif line.startswith("WARNING:"):
Naresh G Solankia6464b72018-01-25 19:02:25 +053036 msg_output = line.split("WARNING:")[1].strip()
Naresh G Solankifb7eaa52018-06-20 14:53:57 +053037 elif ": FILE:" in line:
Naresh G Solankia6464b72018-01-25 19:02:25 +053038 temp = line.split("FILE:")
39 file_path = temp[1].split(":")[0]
40 line_number = temp[1].split(":")[1]
41 update_struct( file_path.strip(), msg_output, str(line_number) )
Martin Roth9e337232022-03-22 17:59:27 -060042 elif re.search("^\d+:\Z",line) != "None" and line.startswith("#"):
43 file_path="/COMMIT_MSG"
44 line = line.replace('#', '')
45 line_number = int(line.split(":")[0]) + 2
46 update_struct( file_path.strip(), msg_output, str(line_number) )
Naresh G Solankia6464b72018-01-25 19:02:25 +053047 else:
48 continue
49 fp.close()
50
51def main():
Patrick Georgi39891c02021-03-15 21:07:05 +010052 if (len(sys.argv) < 5) or (sys.argv[1] == "-h"):
Stefan Reinauer57ead892020-06-12 14:38:29 -070053 print("HELP:")
Patrick Georgi39891c02021-03-15 21:07:05 +010054 print(sys.argv[0] + " <input file> <output file in json> <job-id> <job-url>")
Naresh G Solankia6464b72018-01-25 19:02:25 +053055 sys.exit()
56
Stefan Reinauer57ead892020-06-12 14:38:29 -070057 print(sys.argv[1])
Naresh G Solankia6464b72018-01-25 19:02:25 +053058 parse_file(sys.argv[1])
Patrick Georgi39891c02021-03-15 21:07:05 +010059 data['robot_comments'] = list_temp
Stefan Reinauer57ead892020-06-12 14:38:29 -070060 print(json.dumps(data))
Naresh G Solankia6464b72018-01-25 19:02:25 +053061 out_file = open( sys.argv[2] , "w")
62 json.dump(data, out_file, sort_keys=True, indent=4)
63 out_file.close()
64
65if __name__ == "__main__":
66 main()