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: str, outfile: str): 33 subprocess.call(['sort', '-u', infile, '-o', outfile], shell=False) 34 35 36def _pick_line(line_info) -> bool: 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: str, out_name: str, prj_dir: str) -> bool: 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 relat_path = templist[0].strip().replace('\x00', '') 83 if relat_path: 84 templist[0] = os.path.realpath(relat_path) 85 templist[0] = templist[0].replace(prj_dir + "/", 86 "").strip() 87 temp = ":" 88 line_info = temp.join(templist) 89 90 out_fd.write(line_info) 91 out_fd.write("\r\n") 92 return True 93 94 95def _get_warn(log_file: str, warn_file: str, prj_dir: str): 96 if not os.path.exists(os.path.dirname(warn_file)): 97 os.makedirs(os.path.dirname(warn_file), exist_ok=True) 98 if os.path.exists(warn_file): 99 os.remove(warn_file) 100 temp_out_file = os.path.join(os.path.dirname(warn_file), 101 'temp_warning.txt') 102 result = _parse(log_file, temp_out_file, prj_dir) 103 if result: 104 _do_uniq(temp_out_file, warn_file) 105 # delete temp file 106 if os.path.exists(temp_out_file): 107 os.remove(temp_out_file) 108 109 110def main(argv) -> int: 111 """parse warning info from build log.""" 112 parser = argparse.ArgumentParser() 113 parser.add_argument('--build-log-file', help='log file', required=True) 114 parser.add_argument('--warning-out-file', 115 help='result file', 116 required=True) 117 args = parser.parse_args(argv) 118 119 log_file = args.build_log_file 120 warn_file = args.warning_out_file 121 prj_dir = os.getcwd() 122 _get_warn(log_file, warn_file, prj_dir) 123 return 0 124 125 126if __name__ == "__main__": 127 main(sys.argv[1:]) 128