lint: convert checkpatch_json.py to python 3
[coreboot.git] / util / lint / checkpatch_json.py
blob8102c114b99151b7967bbe6951e0a641e6fd5fd4
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-only
4 """
5 This utilty generate json output to post comment in gerrit.
7 INPUT: output of checkpatch.pl.
8 OUTPUT: json format output that can be used to post comment in gerrit
9 """
10 import os
11 import sys
12 import json
14 data = {}
15 data['comments'] = []
16 list_temp = {}
18 def update_struct( file_path, msg_output, line_number):
19 if file_path not in list_temp:
20 list_temp[file_path] = []
21 list_temp[file_path].append({
22 "line" : line_number,
23 "message" : msg_output,}
26 def parse_file(input_file):
27 fp = open (input_file, "r")
28 for line in fp:
29 if line.startswith("ERROR:"):
30 msg_output = line.split("ERROR:")[1].strip()
31 elif line.startswith("WARNING:"):
32 msg_output = line.split("WARNING:")[1].strip()
33 elif ": FILE:" in line:
34 temp = line.split("FILE:")
35 file_path = temp[1].split(":")[0]
36 line_number = temp[1].split(":")[1]
37 update_struct( file_path.strip(), msg_output, str(line_number) )
38 else:
39 continue
40 fp.close()
42 def main():
43 if (len(sys.argv) < 3) or (sys.argv[1] == "-h"):
44 print("HELP:")
45 print(sys.argv[0] + " <input file> <output file in json>")
46 sys.exit()
48 print(sys.argv[1])
49 parse_file(sys.argv[1])
50 data['comments'] = list_temp
51 print(json.dumps(data))
52 out_file = open( sys.argv[2] , "w")
53 json.dump(data, out_file, sort_keys=True, indent=4)
54 out_file.close()
56 if __name__ == "__main__":
57 main()