• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21
22"""
23@Desc:
24  This script is used to generate full components
25  example for creating new products
26
27@GUID:
28basic:
29  cmd1: cd ${repo_root}
30  cmd2: python3 ./build/tools/component_tools/full_components_generator.py
31  base_product.json will be created in ./productdefine/common/base
32
33advanced:
34  cmd: python3 full_components_generator.py -h
35  Get more information
36
37@Date 2022/01/14
38"""
39
40def find_files(path, name):
41    ret, files, folders = [], [], [path]
42    for folder in folders:
43        for file in os.listdir(folder):
44            abs_file = os.path.join(folder, file)
45            if str(file) == name:
46                files.append(abs_file)
47            if os.path.isdir(abs_file):
48                folders.append(abs_file)
49
50    for file in files:
51        if len(file) > 0 and os.path.exists(file):
52            ret.append(file)
53    return ret
54
55
56def read_component_from_ohos_build(file):
57    ret = {"subsystem": "", "components": []}
58    with open(file, "rb") as f:
59        data = json.load(f)
60        ret["subsystem"] =  data.get("subsystem")
61        for k, _ in data.get("parts").items():
62            ret.get("components").append(k)
63    return ret
64
65
66def read_component_from_bundle_json(file):
67    ret = {"subsystem": "", "components": []}
68    with open(file, "rb") as f:
69        data = json.load(f)
70        ret["subsystem"] = data.get("component").get("subsystem")
71        ret.get("components").append(data.get("component").get("name"))
72    return ret
73
74
75def find_component_in_path(subsys, path):
76    ret = set()
77    if not os.path.exists(path):
78        return []
79    files_ohos_build = find_files(path, "ohos.build")
80    files_bundle_json = find_files(path, "bundle.json")
81    for ohos_build in files_ohos_build:
82        data = read_component_from_ohos_build(ohos_build)
83        if data.get("subsystem") == subsys:
84            ret = ret.union(set(data.get("components")))
85
86    for bundle_json in files_bundle_json:
87        data = read_component_from_bundle_json(bundle_json)
88        if data.get("subsystem") == subsys:
89            ret = ret.union(set(data.get("components")))
90    return ret
91
92
93def update_components(subsys_file):
94    ret = {"subsystems": []}
95    with open(subsys_file, "rb") as f:
96        data = json.load(f)
97        for subsys, v in data.items():
98            components_path = v.get("path")
99            parts = find_component_in_path(subsys, components_path)
100            components = []
101            for part in parts:
102                components.append({"component": part, "features": []})
103            ret.get("subsystems").append(
104                {"subsystem": subsys, "components": components})
105    return ret
106
107
108def main():
109    parser = argparse.ArgumentParser()
110    parser.add_argument('--subsys', type=str, default="./build/subsystem_config.json",
111                        help='subsystem config file location, default=//build/subsystem_config.json')
112    parser.add_argument('--out', type=str, default="./productdefine/common/base/base_product.json",
113                        help='base_config output path default //productdefine/common/base')
114    args = parser.parse_args()
115    # Only for version 3.0 config
116    # 'device_name' has been replaced by 'board' in 3.0
117    # device info has been merged into product in 3.0
118    # 'target cpu' need to be arm instead of arm64 due to adaption work has not been done
119    ret = {
120        "product_name": "ohos-arm64",
121        "version": "3.0",
122        "type": "standard",
123        "ohos_version": "OpenHarmony 3.x",
124        "board": "arm64",
125        "kernel_type": "",
126        "kernel_version": "",
127        "device_name": "arm64",
128        "device_company": "openharmony",
129        "target_os": "ohos",
130        "target_cpu": "arm",
131        "subsystems": []
132    }
133    data = update_components(args.subsys)
134    ret["subsystems"] = data.get("subsystems")
135    with open(args.out, "w") as f:
136        f.write(json.dumps(ret, indent=2))
137    print("file has generated in path: {}".format(args.out))
138
139
140if __name__ == '__main__':
141    main()
142