Angel Pons | 7ec15c8 | 2018-11-17 09:47:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 2 | # 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 | """ |
| 17 | This utilty generate json output to post comment in gerrit. |
| 18 | |
| 19 | INPUT: output of checkpatch.pl. |
| 20 | OUTPUT: json format output that can be used to post comment in gerrit |
| 21 | """ |
| 22 | import os |
| 23 | import sys |
| 24 | import json |
| 25 | |
| 26 | data = {} |
| 27 | data['comments'] = [] |
| 28 | list_temp = {} |
| 29 | |
| 30 | def 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 | |
| 38 | def parse_file(input_file): |
| 39 | fp = open (input_file, "r") |
| 40 | for line in fp: |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 41 | if line.startswith("ERROR:"): |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 42 | msg_output = line.split("ERROR:")[1].strip() |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 43 | elif line.startswith("WARNING:"): |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 44 | msg_output = line.split("WARNING:")[1].strip() |
Naresh G Solanki | fb7eaa5 | 2018-06-20 14:53:57 +0530 | [diff] [blame] | 45 | elif ": FILE:" in line: |
Naresh G Solanki | a6464b7 | 2018-01-25 19:02:25 +0530 | [diff] [blame] | 46 | 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 | |
| 54 | def 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 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |