Stefan Reinauer | 57ead89 | 2020-06-12 14:38:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | This utilty generate json output to post comment in gerrit. |
| 6 | |
| 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 |
Martin Roth | 9e33723 | 2022-03-22 17:59:27 -0600 | [diff] [blame] | 13 | import re |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 14 | |
| 15 | data = {} |
| 16 | data['comments'] = [] |
| 17 | list_temp = {} |
| 18 | |
| 19 | def 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 Georgi | 39891c0 | 2021-03-15 21:07:05 +0100 | [diff] [blame] | 23 | "robot_id" : "checkpatch", |
| 24 | "robot_run_id" : sys.argv[3], |
| 25 | "url" : sys.argv[4], |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 26 | "line" : line_number, |
Yu-Ping Wu | d5e3367 | 2023-07-10 16:07:17 +0800 | [diff] [blame] | 27 | "message" : "`" + msg_output + "`", |
| 28 | }) |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 29 | |
| 30 | def parse_file(input_file): |
| 31 | fp = open (input_file, "r") |
| 32 | for line in fp: |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 33 | if line.startswith("ERROR:"): |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 34 | msg_output = line.split("ERROR:")[1].strip() |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 35 | elif line.startswith("WARNING:"): |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 36 | msg_output = line.split("WARNING:")[1].strip() |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 37 | elif ": FILE:" in line: |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 38 | 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 Roth | 9e33723 | 2022-03-22 17:59:27 -0600 | [diff] [blame] | 42 | 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 Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 47 | else: |
| 48 | continue |
| 49 | fp.close() |
| 50 | |
| 51 | def main(): |
Patrick Georgi | 39891c0 | 2021-03-15 21:07:05 +0100 | [diff] [blame] | 52 | if (len(sys.argv) < 5) or (sys.argv[1] == "-h"): |
Stefan Reinauer | 57ead89 | 2020-06-12 14:38:29 -0700 | [diff] [blame] | 53 | print("HELP:") |
Patrick Georgi | 39891c0 | 2021-03-15 21:07:05 +0100 | [diff] [blame] | 54 | print(sys.argv[0] + " <input file> <output file in json> <job-id> <job-url>") |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 55 | sys.exit() |
| 56 | |
Stefan Reinauer | 57ead89 | 2020-06-12 14:38:29 -0700 | [diff] [blame] | 57 | print(sys.argv[1]) |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 58 | parse_file(sys.argv[1]) |
Patrick Georgi | 39891c0 | 2021-03-15 21:07:05 +0100 | [diff] [blame] | 59 | data['robot_comments'] = list_temp |
Stefan Reinauer | 57ead89 | 2020-06-12 14:38:29 -0700 | [diff] [blame] | 60 | print(json.dumps(data)) |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 61 | out_file = open( sys.argv[2] , "w") |
| 62 | json.dump(data, out_file, sort_keys=True, indent=4) |
| 63 | out_file.close() |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | main() |