1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2022 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 16import argparse 17import os 18import sys 19import subprocess 20from gn_check.check_gn_online import CheckGnOnline 21from bundle_check.bundle_check_online import BundleCheckOnline 22from csct_online_prehandle import GiteeCsctPrehandler 23 24 25class CsctOnline(object): 26 """This is a component static checker online class""" 27 28 version = "" 29 log_verbose = False 30 pr_list = "" 31 32 def __init__(self, pr_list="", log_verbose=False) -> None: 33 self.version = "0.0.1" 34 self.pr_list = pr_list 35 self.log_verbose = log_verbose 36 37 def __verbose_print(self, verbose_flag, print_content): 38 if verbose_flag is True: 39 print(print_content) 40 41 def __print_pretty(self, errs_info): 42 try: 43 from prettytable import PrettyTable 44 print('already exist prettytable') 45 except Exception: 46 print('no prettytable') 47 ret = subprocess.Popen( 48 [sys.executable, "-m", "pip", "install", "prettytable"], 49 stdout=subprocess.PIPE, 50 stderr=subprocess.PIPE, 51 errors="replace", 52 ) 53 print('installing prettytable') 54 try: 55 _, __ = ret.communicate() 56 print('prettytable installed successfully') 57 from prettytable import PrettyTable 58 except Exception: 59 print('prettytable installed failed') 60 61 62 table = PrettyTable(["文件", "定位", "违反规则", "错误说明"]) 63 table.add_rows(errs_info) 64 table.align["文件"] = "l" 65 table.align["定位"] = "l" 66 table.align["错误说明"] = "l" 67 info = table.get_string() 68 print( 69 "If you have any question, please access component static check rules:", 70 "https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/" 71 "subsystems/subsys-build-component-building-rules.md", 72 "or https://gitee.com/openharmony/build/tree/master/tools/component_tools/static_check/readme.md", 73 ) 74 print("There are(is) {} error(s):\n".format(len(errs_info))) 75 print(str(info)) 76 77 def csct_check_process(self): 78 pr_list = self.pr_list 79 self.__verbose_print( 80 self.log_verbose, 81 "\nCsct check begin!\tPull request list: {}.".format(pr_list), 82 ) 83 csct_prehandler = GiteeCsctPrehandler( 84 pr_list, "BUILD.gn", "bundle.json" 85 ) 86 87 _, gn_errs = CheckGnOnline(csct_prehandler.get_diff_dict("BUILD.gn")).output() 88 _, bundle_errs = BundleCheckOnline.check_diff( 89 csct_prehandler.get_diff_dict("bundle.json") 90 ) 91 92 errs_info = gn_errs + bundle_errs 93 if len(errs_info) == 0: 94 self.__verbose_print(self.log_verbose, "Result: without any errors.") 95 else: 96 self.__print_pretty(errs_info) 97 98 self.__verbose_print(self.log_verbose, "Csct check end!\n") 99 return errs_info 100 101 102def add_options(version): 103 parser = argparse.ArgumentParser( 104 description=f"Component Static Check Tool Online version {version}", 105 ) 106 parser.add_argument( 107 "-v", 108 "--verbose", 109 action="store_true", 110 dest="verbose", 111 default=False, 112 help="verbose mode", 113 ) 114 parser.add_argument( 115 metavar="pr_list", type=str, dest="pr_list", help="pull request url list" 116 ) 117 args = parser.parse_args() 118 return args 119 120 121def main(): 122 csct_online = CsctOnline() 123 args = add_options(csct_online.version) 124 csct_online.pr_list = args.pr_list 125 csct_online.log_verbose = args.verbose 126 errs_info = csct_online.csct_check_process() 127 128 129if __name__ == "__main__": 130 sys.exit(main()) 131