1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 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 json 18import os 19import time 20import stat 21import utils 22 23 24def _get_args(): 25 parser = argparse.ArgumentParser(add_help=True) 26 parser.add_argument( 27 "-p", 28 "--input_path", 29 default=r"./", 30 type=str, 31 help="Path of source code", 32 ) 33 parser.add_argument( 34 "-rp", 35 "--root_path", 36 default=r"./", 37 type=str, 38 help="Path of root", 39 ) 40 args = parser.parse_args() 41 return args 42 43 44def _judge_type(element, deps_list: list): 45 if isinstance(element, dict): 46 for k, v in element.items(): 47 _judge_type(v, deps_list) 48 elif isinstance(element, list): 49 for v in element: 50 _judge_type(v, deps_list) 51 elif isinstance(element, str): 52 deps_list.append(element) 53 54 55def _inner_kits_name(inner_kits_list, deps_list): 56 if inner_kits_list: 57 for k in inner_kits_list: 58 deps_list.append(k['name']) 59 60 61def _output_build_gn(deps_list, output_path): 62 file_name = os.path.join(output_path, 'BUILD.gn') 63 flags = os.O_WRONLY | os.O_CREAT 64 modes = stat.S_IWUSR | stat.S_IRUSR 65 with os.fdopen(os.open(file_name, flags, modes), 'w') as f: 66 f.write('import("//build/ohos_var.gni")\n') 67 f.write('\n') 68 f.write('group("default") {\n') 69 f.write(' deps = [\n') 70 for i in deps_list: 71 f.write(f" \"{i}\",\n") 72 f.write(' ]\n') 73 f.write('}\n') 74 75 76def _get_bundle_path(source_code_path): 77 bundle_paths = dict() 78 for root, dirs, files in os.walk(source_code_path): 79 for file in files: 80 if file.endswith("bundle.json"): 81 bundle_paths.update({os.path.join(root, file): root}) 82 return bundle_paths 83 84 85def _get_src_part_name(src_bundle_paths): 86 _name = '' 87 _path = '' 88 _bundle_path = '' 89 for src_bundle_path, v_path in src_bundle_paths.items(): 90 src_bundle_json = utils.get_json(src_bundle_path) 91 part_name = src_bundle_json['component']['name'] 92 if part_name.endswith('lite'): 93 pass 94 else: 95 _name = part_name 96 _bundle_path = src_bundle_path 97 _path = v_path 98 return _bundle_path, _path 99 100 101def main(): 102 args = _get_args() 103 source_code_path = args.input_path 104 deps_list = list() 105 bundle_paths = _get_bundle_path(source_code_path) 106 _bundle_path, dir_path = _get_src_part_name(bundle_paths) 107 bundle_json = utils.get_json(_bundle_path) 108 build_data = bundle_json["component"]["build"] 109 for ele in build_data.keys(): 110 if ele not in ['inner_kits', 'test', 'inner_api']: 111 _judge_type(build_data[ele], deps_list) 112 elif ele in ['inner_kits', 'inner_api']: 113 inner_kits_list = build_data[ele] 114 _inner_kits_name(inner_kits_list, deps_list) 115 output_path = os.path.join(args.root_path, 'out') 116 _output_build_gn(deps_list, output_path) 117 118 119if __name__ == '__main__': 120 main() 121