1#!/usr/bin/env python3 2#-*- coding: UTF-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16""" 17 18Usage: get_warnings.py --build-log-file out/phone-release/build.log \ 19 --warning-out-file out/phone-release/warning_list.txt 20 21Generate the project notice files, including both text and xml files. 22 23""" 24 25import argparse 26import os 27import sys 28import re 29import subprocess 30 31 32def _do_uniq(infile, outfile): 33 subprocess.call(['sort', '-u', infile, '-o', outfile], shell=False) 34 35 36def _pick_line(line_info): 37 result = False 38 parser_keys = [ 39 ": Warning", ": warning", "warning:", "Warning:", "WARNING:" 40 ] 41 for _key in parser_keys: 42 if len(re.findall(_key, line_info, re.S)) >= 1: 43 result = True 44 break 45 return result 46 47 48def _parse(in_name, out_name, prj_dir): 49 if not os.path.exists(in_name): 50 print("warning: build log file {} is not exists.".format(in_name)) 51 return False 52 53 with open(in_name, "r", encoding='utf-8', errors='ignore') as in_fd: 54 os.makedirs(os.path.dirname(out_name), exist_ok=True) 55 with open(out_name, "w") as out_fd: 56 while True: 57 line_info = in_fd.readline() 58 line_info = line_info.replace("\r", "") 59 if line_info == "": 60 break 61 # Solve the non-standard printing in kernel compilation --begin 62 if (line_info.find("kernel/linux-") > 63 1) and (": warning:" in line_info): 64 line_info = line_info[line_info.find("kernel/linux-"):] 65 # Solve the non-standard printing in kernel compilation --end 66 if _pick_line(line_info): 67 while True: 68 if line_info.startswith("../"): 69 line_info = line_info[3:] 70 elif line_info.startswith("./"): 71 line_info = line_info[2:] 72 elif line_info.startswith("\""): 73 line_info = line_info[1:] 74 elif line_info.startswith(":"): 75 line_info = line_info[1:] 76 elif line_info.startswith(" "): 77 line_info = line_info[1:] 78 else: 79 break 80 # solving relative path 81 templist = line_info.split(":") 82 templist[0] = os.path.abspath(templist[0]) 83 templist[0] = templist[0].replace(prj_dir + "/", 84 "").strip() 85 temp = ":" 86 line_info = temp.join(templist) 87 88 out_fd.write(line_info) 89 out_fd.write("\r\n") 90 return True 91 92 93def _get_warn(log_file, warn_file, prj_dir): 94 if not os.path.exists(os.path.dirname(warn_file)): 95 os.makedirs(os.path.dirname(warn_file), exist_ok=True) 96 if os.path.exists(warn_file): 97 os.remove(warn_file) 98 temp_out_file = os.path.join(os.path.dirname(warn_file), 99 'temp_warning.txt') 100 result = _parse(log_file, temp_out_file, prj_dir) 101 if result: 102 _do_uniq(temp_out_file, warn_file) 103 # delete temp file 104 if os.path.exists(temp_out_file): 105 os.remove(temp_out_file) 106 107 108def main(argv): 109 """parse warning info from build log.""" 110 parser = argparse.ArgumentParser() 111 parser.add_argument('--build-log-file', help='log file', required=True) 112 parser.add_argument('--warning-out-file', 113 help='result file', 114 required=True) 115 args = parser.parse_args(argv) 116 117 log_file = args.build_log_file 118 warn_file = args.warning_out_file 119 prj_dir = os.getcwd() 120 _get_warn(log_file, warn_file, prj_dir) 121 return 0 122 123 124if __name__ == "__main__": 125 main(sys.argv[1:]) 126