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