1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2021 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17""" 18import os 19import json 20import argparse 21from hb.resources.config import Config 22 23""" 24@Desc: 25 This script is used to generate full components 26 example for creating new products 27 28@GUID: 29basic: 30 cmd1: cd ${repo_root} 31 cmd2: python3 ./build/tools/component_tools/full_components_generator.py 32 base_product.json will be created in ./productdefine/common/base 33 34advanced: 35 cmd: python3 full_components_generator.py -h 36 Get more information 37 38@Date 2022/01/14 39""" 40 41 42def find_files(path: str, name: str): 43 ret, files, folders = [], [], [path] 44 for folder in folders: 45 for file in os.listdir(folder): 46 abs_file = os.path.join(folder, file) 47 if str(file) == name: 48 files.append(abs_file) 49 if os.path.isdir(abs_file): 50 folders.append(abs_file) 51 52 for file in files: 53 if len(file) > 0 and os.path.exists(file): 54 ret.append(file) 55 return ret 56 57 58def read_component_from_ohos_build(file: str): 59 ret = {"subsystem": "", "components": []} 60 with open(file, "rb") as f: 61 data = json.load(f) 62 ret["subsystem"] = data.get("subsystem") 63 for k, _ in data.get("parts").items(): 64 ret.get("components").append(k) 65 return ret 66 67 68def read_component_from_bundle_json(file: str): 69 ret = {"subsystem": "", "components": []} 70 with open(file, "rb") as f: 71 data = json.load(f) 72 ret["subsystem"] = data.get("component").get("subsystem") 73 ret.get("components").append(data.get("component").get("name")) 74 return ret 75 76 77def find_component_in_path(subsys: str, path: str): 78 ret = set() 79 if not os.path.exists(path): 80 return [] 81 files_ohos_build = find_files(path, "ohos.build") 82 files_bundle_json = find_files(path, "bundle.json") 83 for ohos_build in files_ohos_build: 84 data = read_component_from_ohos_build(ohos_build) 85 if data.get("subsystem") == subsys: 86 ret = ret.union(set(data.get("components"))) 87 88 for bundle_json in files_bundle_json: 89 data = read_component_from_bundle_json(bundle_json) 90 if data.get("subsystem") == subsys: 91 ret = ret.union(set(data.get("components"))) 92 return ret 93 94 95def update_components(subsys_file: str): 96 ret = {"subsystems": []} 97 with open(subsys_file, "rb") as f: 98 data = json.load(f) 99 for subsys, v in data.items(): 100 components_path = v.get("path") 101 parts = find_component_in_path(subsys, components_path) 102 components = [] 103 for part in parts: 104 components.append({"component": part, "features": []}) 105 ret.get("subsystems").append( 106 {"subsystem": subsys, "components": components}) 107 return ret 108 109 110def main(): 111 conf = Config() 112 subsystem_json_overlay_path = os.path.join(conf.product_path, 'subsystem_config_overlay.json') 113 parser = argparse.ArgumentParser() 114 parser.add_argument('--subsys', type=str, default="./build/subsystem_config.json", 115 help='subsystem config file location, default=//build/subsystem_config.json') 116 parser.add_argument('--subsys_overlay', type=str, default=subsystem_json_overlay_path, 117 help='subsystem config overlay file location, default={}'.format(subsystem_json_overlay_path)) 118 parser.add_argument('--out', type=str, default="./productdefine/common/base/base_product.json", 119 help='base_config output path default //productdefine/common/base') 120 args = parser.parse_args() 121 # Only for version 3.0 config 122 # 'device_name' has been replaced by 'board' in 3.0 123 # device info has been merged into product in 3.0 124 # 'target cpu' need to be arm instead of arm64 due to adaption work has not been done 125 ret = { 126 "product_name": "ohos-arm64", 127 "version": "3.0", 128 "type": "standard", 129 "ohos_version": "OpenHarmony 3.x", 130 "board": "arm64", 131 "kernel_type": "", 132 "kernel_version": "", 133 "device_name": "arm64", 134 "device_company": "openharmony", 135 "target_os": "ohos", 136 "target_cpu": "arm", 137 "subsystems": [] 138 } 139 data = update_components(args.subsys) 140 ret["subsystems"] = data.get("subsystems") 141 if os.path.isfile(subsystem_json_overlay_path): 142 overlay_data = update_components(args.subsys_overlay) 143 ret["subsystems"].update(overlay_data.get("subsystems")) 144 with open(args.out, "w") as f: 145 f.write(json.dumps(ret, indent=2)) 146 print("file has generated in path: {}".format(args.out)) 147 148 149if __name__ == '__main__': 150 main() 151