• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Copyright 2022 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"""
18
19import os
20import re
21import json
22
23from hb_internal.common.utils import hb_info
24
25budle_json_files = []
26standard_part_roms = []
27part_info_list = []
28
29
30def part_size_compare(module_info_list, part_name, part_size):
31    for standard_part in standard_part_roms:
32        if standard_part['part_name'] == part_name and standard_part['part_size'] != 'None':
33            sta_size = re.findall(r"\d+", standard_part['part_size'])
34            rea_size = re.findall(r"\d+", part_size)
35            if int(sta_size[0]) >= int(rea_size[0]):
36                conform_str = ("part_name: " + part_name).ljust(55) + \
37                              ("actual_size: " + part_size).ljust(25) + \
38                              ("standard_size: " + standard_part['part_size']).ljust(25) + \
39                               " 'rom' conform to the rules"
40                hb_info(conform_str)
41                part_info_dict = {}
42                part_info_dict["part_name"] = part_name
43                part_info_dict["actual_size"] = part_size
44                part_info_dict["standard_size"] = standard_part['part_size']
45                part_info_dict["status_info"] = "'rom' conform to the rules"
46                part_info_dict["modules_info"] = module_info_list
47                part_info_list.append(part_info_dict)
48
49            elif int(sta_size[0]) < int(rea_size[0]):
50                out_of_standard_str = ("part_name: " + part_name).ljust(55) + \
51                                      ("actual_size: " + part_size).ljust(25) + \
52                                      ("standard_size: " + standard_part['part_size']).ljust(25) + \
53                                       " 'rom' out of standard"
54                hb_info(out_of_standard_str)
55                part_info_dict = {}
56                part_info_dict["part_name"] = part_name
57                part_info_dict["actual_size"] = part_size
58                part_info_dict["standard_size"] = standard_part['part_size']
59                part_info_dict["status_info"] = "'rom' out of standard"
60                part_info_dict["modules_info"] = module_info_list
61                part_info_list.append(part_info_dict)
62        else:
63            if standard_part['part_name'] == part_name and standard_part['part_size'] == 'None':
64                not_yet_standard_str = ("part_name: " + part_name).ljust(55) + \
65                                       ("actual_size: " + part_size).ljust(50) + \
66                                        "This part does not set standard 'rom' size".ljust(25)
67                hb_info(not_yet_standard_str)
68                part_info_dict = {}
69                part_info_dict["part_name"] = part_name
70                part_info_dict["actual_size"] = part_size
71                part_info_dict["standard_size"] = standard_part['part_size']
72                part_info_dict["status_info"] = "This part does not set standard 'rom' size"
73                part_info_dict["modules_info"] = module_info_list
74                part_info_list.append(part_info_dict)
75
76
77def collect_part_name(root_path):
78    install_parts = []
79    file_path = os.path.join(root_path, "packages/phone/system_install_parts.json")
80    if os.path.isfile(file_path):
81        with open(file_path, 'rb') as file:
82            file_json = json.load(file)
83            for part_info in file_json:
84                part_info_dict = {}
85                part_info_dict["part_name"] = part_info["part_name"]
86                part_info_dict["part_info_file"] = part_info["part_info_file"]
87                install_parts.append(part_info_dict)
88    return install_parts
89
90
91def colletct_modules_json_path(out_path, install_parts):
92    module_info_list = []
93    for part_info_dict in install_parts:
94        part_info_path = os.path.join(out_path, part_info_dict["part_info_file"])
95        if os.path.isfile(part_info_path):
96            with open(part_info_path, 'rb') as file:
97                file_json = json.load(file)
98                for module_info in file_json:
99                    if module_info["part_name"] == part_info_dict["part_name"]:
100                        module_json_path = {}
101                        module_json_path["module_info_path"] = module_info["module_info_file"]
102                        module_json_path["part_name"] = module_info["part_name"]
103                        module_info_list.append(module_json_path)
104    return module_info_list
105
106
107def sum_of_statistics(out_path, part_name, module_info_list):
108    part_so_size = 0
109    module_list = []
110    for module_info in module_info_list:
111        if part_name == module_info['part_name']:
112            module_info_path = os.path.join(out_path, module_info['module_info_path'])
113            if os.path.isfile(module_info_path):
114                with open(module_info_path, 'rb') as file:
115                    file_json = json.load(file)
116                    so_file_dir = os.path.join(out_path, file_json["source"])
117                    if so_file_dir.endswith(".so") and os.path.isfile(so_file_dir):
118                        module_info_dict = {}
119                        module_info_dict["module_name"] = file_json["label_name"]
120                        module_info_dict["source"] = file_json["source"]
121                        module_info_dict["dest"] = file_json["dest"]
122                        dest_num = len(file_json["dest"])
123                        so_file_size = os.path.getsize(so_file_dir) * dest_num
124                        part_so_size += so_file_size
125                        module_info_dict["module_size"] = f'{round(so_file_size / 1024, 2)}KB'
126                        module_list.append(module_info_dict)
127    return module_list, part_so_size
128
129
130def check_image_size(out_path):
131    image_list = []
132    image_path = os.path.join(out_path, 'packages/phone/images/')
133    if os.path.isdir(image_path):
134        for file in os.listdir(image_path):
135            if file.endswith(".img"):
136                image_dict = {}
137                img_path = os.path.join(image_path, file)
138                image_dict['img_size'] = f'{round(os.path.getsize(img_path) / 1024, 2)}KB'
139                image_dict['img_name'] = file
140                image_list.append(image_dict)
141    return image_list
142
143
144def actual_rom_statistics(out_path):
145    rom_statistics = {}
146    install_parts = collect_part_name(out_path)
147    module_info_list = colletct_modules_json_path(out_path, install_parts)
148    image_list = check_image_size(out_path)
149    rom_statistics["images_info"] = image_list
150
151    for part_info_dict in install_parts:
152        statistics_result = sum_of_statistics(out_path, part_info_dict["part_name"], module_info_list)
153        part_so_size = f'{round(statistics_result[1] / 1024, 2)}KB'
154        part_size_compare(statistics_result[0],
155                            part_info_dict["part_name"],
156                            part_so_size)
157    rom_statistics["parts_info"] = part_info_list
158    json_path = os.path.join(out_path, 'rom_statistics_table.json')
159    json_str = json.dumps(rom_statistics, indent=4)
160    with open(json_path, 'w') as json_file:
161        json_file.write(json_str)
162
163
164def read_bundle_json_file(file_path):
165    with open(file_path, 'rb') as file:
166        file_json = json.load(file)
167        standard_part_rom = {}
168        standard_part_rom["part_name"] = file_json["component"]["name"]
169        if 'rom' not in file_json["component"].keys() or file_json["component"]["rom"] == '':
170            standard_part_rom["part_size"] = 'None'
171        else:
172            standard_part_rom["part_size"] = file_json["component"]["rom"]
173        if standard_part_roms.count(standard_part_rom) == 0:
174            standard_part_roms.append(standard_part_rom)
175
176
177def collect_bundle_json_path(part_root_path):
178    for root, dirs, files in os.walk(part_root_path):
179        abs_path = os.path.abspath(root)
180        for file_name in files:
181            if file_name == 'bundle.json':
182                budle_json_files.append(os.path.join(abs_path, file_name))
183
184
185def read_subsystem_config(root_path):
186    part_json_paths = []
187    part_json_path = os.path.join(root_path, 'build/subsystem_config.json')
188    if os.path.isfile(part_json_path):
189        with open(part_json_path, 'r') as file:
190            file_json = json.load(file)
191            for part_info_valule in file_json.values():
192                for path_k, path_v in part_info_valule.items():
193                    if path_k == "path":
194                        part_json_paths.append(path_v)
195    return part_json_paths
196
197
198def read_ohos_config(root_path):
199    file_path = os.path.join(root_path, "ohos_config.json")
200    with open(file_path, 'r') as file:
201        file_json = json.load(file)
202        os_level = file_json["os_level"]
203        out_path = file_json["out_path"]
204        board = file_json["board"]
205        product = file_json["product"]
206    return (out_path, board, product, os_level)
207
208
209def output_part_rom_status(root_path):
210    ohos_config = read_ohos_config(root_path)
211    if ohos_config[3] == "mini":
212        return -1
213    elif ohos_config[3] == "small":
214        return -1
215    else:
216        part_paths = read_subsystem_config(root_path)
217        for part_path in part_paths:
218            part_root_path = os.path.join(root_path, part_path)
219            if os.path.isdir(part_root_path):
220                collect_bundle_json_path(part_root_path)
221        for json_file in budle_json_files:
222            if os.path.exists(json_file):
223                read_bundle_json_file(json_file)
224        actual_rom_statistics(ohos_config[0])
225    return 0
226