diff --git a/tools/components_deps/README.md b/tools/components_deps/README.md index 18c9a51c37e69df499b0e7671481f2d15ba5c19a..1142e657bf555f323b466e6db3812512be43f20e 100644 --- a/tools/components_deps/README.md +++ b/tools/components_deps/README.md @@ -17,22 +17,23 @@ config.json文件主要是关于rk3568系列,已测试产品包括rk3568、rk356 1. 获取BUILD.gn文件 1. 获取包含开源部件集的xml文件 1. 获取包含部件集定义的config.json文件 -1. python3及以后 +1. 获取BUILD.gn文件对应的name 命令介绍: 1. `-h`或`--help`命令查看帮助 ```shell - > python components_deps_analyzer.py --help - usage: components_deps_analyzer.py [-h] -p COMPONENTS_GN_PATH_LIST -c CONFIG_PATH -o OPEN_COMPONENT_XML_PATH [-r RESULT_JSON_NAME] + > python components_deps_analyzer.py --help + usage: components_deps_analyzer.py [-h] -p COMPONENTS_GN_PATH_LIST -g GN_NAME -c CONFIG_PATH -o OPEN_COMPONENT_XML_PATH [-r RESULT_JSON_NAME] analyze components deps. optional arguments: + -h, --help show this help message and exit -p COMPONENTS_GN_PATH_LIST, --components_gn_path_list COMPONENTS_GN_PATH_LIST path of pr BUILD.gn - -g GN_COMPONENT, --gn_component GN_COMPONENT - gn file corresponding component + -g GN_NAME, --gn_name GN_NAME + gn file corresponding name -c CONFIG_PATH, --config_path CONFIG_PATH path of config_file -o OPEN_COMPONENT_XML_PATH, --open_component_xml_path OPEN_COMPONENT_XML_PATH @@ -40,10 +41,11 @@ config.json文件主要是关于rk3568系列,已测试产品包括rk3568、rk356 -r RESULT_JSON_NAME, --result_json_name RESULT_JSON_NAME name of output_json + ``` 1. 使用示例 ```shell - python components_deps_analyzer.py -p BUILD.gn,pkgs/BUILD.gn -g ace_engine,cef -c config_path -o .\gn_xml\ohos.xml + python components_deps_analyzer.py -p BUILD.gn,pkgs/BUILD.gn -g third_party_curl,third_party_zlib -c config_path -o .\gn_xml\ohos.xml ``` ## 输出格式介绍(result.json) @@ -55,6 +57,7 @@ config.json文件主要是关于rk3568系列,已测试产品包括rk3568、rk356 "error": [ { "line": 行号, + "code": 行号对应代码, "rule": 触发规则, "detail": 详细说明 }, diff --git a/tools/components_deps/components_deps_analyzer.py b/tools/components_deps/components_deps_analyzer.py index ffd001ba6a961f4af84c0a31b32850f31d76f4ef..fc66b2b4d24c904575c2d28ce3a4a9bf71c827a6 100644 --- a/tools/components_deps/components_deps_analyzer.py +++ b/tools/components_deps/components_deps_analyzer.py @@ -25,13 +25,14 @@ class Analyzer: @classmethod def __get_open_components(cls, xml_path): open_components = list() + gn_name = list() with open(xml_path, 'r', encoding='utf-8') as r: xml_info = r.readlines() for line in xml_info: if "path=" in line: - tmp = re.findall('path="(.*?)"', line)[0] - open_components.append(tmp.split('/')[-1]) - return open_components + open_components.append(re.findall('path="(.*?)"', line)[0].split('/')[-1]) + gn_name.append(re.findall('name="(.*?)"', line)[0].split('/')[-1]) + return open_components, gn_name @classmethod def __deal_config_json(cls, config_json): @@ -55,8 +56,8 @@ class Analyzer: @classmethod def __get_line(cls, txt_list, key_words: str): - for i in range(len(txt_list)): - if key_words in txt_list[i]: + for i, txt in enumerate(txt_list): + if key_words in txt: return i + 1 return 0 @@ -75,8 +76,8 @@ class Analyzer: if dependent_close == True: if component in txt: dependent_close = False - for i in range(len(gn)): - dep_txt = re.findall('deps = \[(.*?)\]', gn[i]) + re.findall('deps += \[(.*?)\]', gn[i]) + for i, txt_line in enumerate(gn): + dep_txt = re.findall('deps = \[(.*?)\]', txt_line) + re.findall('deps += \[(.*?)\]', txt_line) dep_info = list() for info in dep_txt: if '/' in info: @@ -89,34 +90,36 @@ class Analyzer: if dependent_close == True and re.findall('deps =', txt): line = cls.__get_line(gn, 'deps =') error.append( - {"line": line, "rule": "depend close component", "detail": "可能依赖闭源部件,请检查deps中的内容"}) + {"line": line, "code": gn[line - 1].strip(), "rule": "depend close component", + "detail": "可能依赖闭源部件,请检查deps中的内容"}) for one_dep in deps: - error.append({"line": one_dep[1], "rule": "depend optional component", + error.append({"line": one_dep[1], "code": gn[one_dep[1] - 1].strip(), "rule": "depend optional component", "detail": "依赖开源部件中的非必选部件{},请检查deps中的内容".format(one_dep[0])}) return gn_path, error @classmethod - def analysis(cls, gn_path_list, gn_component, config_path: str, open_components_path, result_json_name: str): + def analysis(cls, gn_path_list, gn_name, config_path: str, open_components_path, result_json_name: str): if not os.path.exists(config_path): print("error: {} is inaccessible or not found".format(config_path)) return if not os.path.exists(open_components_path): print("error: {} is inaccessible or not found".format(open_components_path)) return - if len(gn_path_list) != len(gn_component): + if len(gn_path_list) != len(gn_name): print( - "error: The component corresponding to the gn file and the gn file path are not in one-to-one correspondence.") + "error: The component and the gn file path are not in one-to-one correspondence.") return required_components = cls.__get_required_components(config_path) - open_components = cls.__get_open_components(open_components_path) + open_components, gn_name_list = cls.__get_open_components(open_components_path) + gn_name2component = dict(zip(gn_name_list, open_components)) optional_components = list() for components in open_components: if components not in required_components: optional_components.append(components) result = list() - for i in range(len(gn_path_list)): + for i, _ in enumerate(gn_path_list): one_result = dict() - if gn_component[i] in required_components: + if gn_name[i] in gn_name_list and gn_name2component[gn_name[i]] in required_components: one_result["file_path"], one_result["error"] = cls.__judge_deps(gn_path_list[i], open_components, optional_components) else: @@ -132,8 +135,8 @@ def get_args(): description=f"analyze components deps.\n") parser.add_argument("-p", "--components_gn_path_list", required=True, type=str, help="path of pr BUILD.gn") - parser.add_argument("-g", "--gn_component", required=True, type=str, - help="gn file corresponding component") + parser.add_argument("-g", "--gn_name", required=True, type=str, + help="gn file corresponding name") parser.add_argument("-c", "--config_path", required=True, type=str, help="path of config_file") parser.add_argument("-o", "--open_component_xml_path", required=True, type=str, @@ -145,9 +148,9 @@ def get_args(): if __name__ == '__main__': args = get_args() - gn_path_list = args.components_gn_path_list.split(',') - gn_component = args.gn_component.split(',') + gn_path_list_name = args.components_gn_path_list.split(',') + gn_component_name = args.gn_name.split(',') config_path = args.config_path open_components_xml_path = args.open_component_xml_path - result_json_name = args.result_json_name - Analyzer.analysis(gn_path_list, gn_component, config_path, open_components_xml_path, result_json_name) \ No newline at end of file + result_json = args.result_json_name + Analyzer.analysis(gn_path_list_name, gn_component_name, config_path, open_components_xml_path, result_json) \ No newline at end of file