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