1#!/usr/bin/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 41def find_files(path, name): 42 ret, files, folders = [], [], [path] 43 for folder in folders: 44 for file in os.listdir(folder): 45 abs_file = os.path.join(folder, file) 46 if str(file) == name: 47 files.append(abs_file) 48 if os.path.isdir(abs_file): 49 folders.append(abs_file) 50 51 for file in files: 52 if len(file) > 0 and os.path.exists(file): 53 ret.append(file) 54 return ret 55 56 57def read_component_from_ohos_build(file): 58 ret = {"subsystem": "", "components": []} 59 with open(file, "rb") as f: 60 data = json.load(f) 61 ret["subsystem"] = data.get("subsystem") 62 for k, _ in data.get("parts").items(): 63 ret.get("components").append(k) 64 return ret 65 66 67def read_component_from_bundle_json(file): 68 ret = {"subsystem": "", "components": []} 69 with open(file, "rb") as f: 70 data = json.load(f) 71 ret["subsystem"] = data.get("component").get("subsystem") 72 ret.get("components").append(data.get("component").get("name")) 73 return ret 74 75 76def find_component_in_path(subsys, path): 77 ret = set() 78 if not os.path.exists(path): 79 return [] 80 files_ohos_build = find_files(path, "ohos.build") 81 files_bundle_json = find_files(path, "bundle.json") 82 for ohos_build in files_ohos_build: 83 data = read_component_from_ohos_build(ohos_build) 84 if data.get("subsystem") == subsys: 85 ret = ret.union(set(data.get("components"))) 86 87 for bundle_json in files_bundle_json: 88 data = read_component_from_bundle_json(bundle_json) 89 if data.get("subsystem") == subsys: 90 ret = ret.union(set(data.get("components"))) 91 return ret 92 93 94def update_components(subsys_file): 95 ret = {"subsystems": []} 96 with open(subsys_file, "rb") as f: 97 data = json.load(f) 98 for subsys, v in data.items(): 99 components_path = v.get("path") 100 parts = find_component_in_path(subsys, components_path) 101 components = [] 102 for part in parts: 103 components.append({"component": part, "features": []}) 104 ret.get("subsystems").append( 105 {"subsystem": subsys, "components": components}) 106 return ret 107 108 109def main(): 110 conf = Config() 111 subsystem_json_overlay_path = conf.product_path + '/subsystem_config_overlay.json' 112 parser = argparse.ArgumentParser() 113 parser.add_argument('--subsys', type=str, default="./build/subsystem_config.json", 114 help='subsystem config file location, default=//build/subsystem_config.json') 115 parser.add_argument('--subsys_overlay', type=str, default=subsystem_json_overlay_path, 116 help='subsystem config overlay file location, default={}'.format(subsystem_json_overlay_path)) 117 parser.add_argument('--out', type=str, default="./productdefine/common/base/base_product.json", 118 help='base_config output path default //productdefine/common/base') 119 args = parser.parse_args() 120 # Only for version 3.0 config 121 # 'device_name' has been replaced by 'board' in 3.0 122 # device info has been merged into product in 3.0 123 # 'target cpu' need to be arm instead of arm64 due to adaption work has not been done 124 ret = { 125 "product_name": "ohos-arm64", 126 "version": "3.0", 127 "type": "standard", 128 "ohos_version": "OpenHarmony 3.x", 129 "board": "arm64", 130 "kernel_type": "", 131 "kernel_version": "", 132 "device_name": "arm64", 133 "device_company": "openharmony", 134 "target_os": "ohos", 135 "target_cpu": "arm", 136 "subsystems": [] 137 } 138 data = update_components(args.subsys) 139 ret["subsystems"] = data.get("subsystems") 140 if os.path.isfile(subsystem_json_overlay_path): 141 overlay_data = update_components(args.subsys_overlay) 142 ret["subsystems"].update(overlay_data.get("subsystems")) 143 with open(args.out, "w") as f: 144 f.write(json.dumps(ret, indent=2)) 145 print("file has generated in path: {}".format(args.out)) 146 147 148if __name__ == '__main__': 149 main() 150