1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import argparse 17import json 18import os 19import stat 20 21 22def generate_replace_data(data_filter, ohos_src_dir): 23 if (not os.path.exists(os.path.join(ohos_src_dir, "unit")) or 24 not os.path.exists(os.path.join(ohos_src_dir, "misc"))): 25 return 26 27 data_filter["fileReplacements"] = dict() 28 data_filter["fileReplacements"]["directory"] = "$FILTERS" 29 data_filter["fileReplacements"]["replacements"] = [] 30 31 for unit_file in os.listdir(os.path.join(ohos_src_dir, "unit")): 32 data_filter["fileReplacements"]["replacements"].append("unit/{}".format(unit_file)) 33 34 for misc_file in os.listdir(os.path.join(ohos_src_dir, "misc")): 35 data_filter["fileReplacements"]["replacements"].append("misc/{}".format(misc_file)) 36 37 38def add_loacale_by_name(data_filter, key): 39 if len(key) == 1 and key[0] == "all": 40 return 41 data_filter["localeFilter"] = dict() 42 data_filter["localeFilter"]["filterType"] = "locale" 43 data_filter["localeFilter"]["includeChildren"] = False 44 data_filter["localeFilter"]["includelist"] = key 45 46 47def add_features_by_name(feature_filter, name, key): 48 if len(key) == 1 and key[0] == "all": 49 return 50 if len(key) == 0: 51 feature_filter[name] = "exclude" 52 return 53 feature_filter[name] = dict() 54 feature_filter[name]["whitelist"] = key 55 56 57def parse_config_file(data_filter, ohos_src_dir): 58 if not os.path.exists(os.path.join(ohos_src_dir, "config.json")): 59 return 60 61 with open(os.path.join(ohos_src_dir, "config.json"), 'r', encoding='utf-8') as f: 62 config = json.load(f) 63 64 add_loacale_by_name(data_filter, config["locale"]) 65 feature_filter = dict() 66 for feature in config.keys(): 67 if feature == "locale": 68 continue 69 add_features_by_name(feature_filter, feature, config[feature]) 70 if len(feature_filter.keys()) != 0: 71 data_filter["featureFilters"] = feature_filter 72 73 74def generate_json_file(ohos_src_dir, out_file): 75 if not os.path.exists(ohos_src_dir): 76 return 77 78 data_filter = dict() 79 data_filter["strategy"] = "subtractive" 80 generate_replace_data(data_filter, ohos_src_dir) 81 parse_config_file(data_filter, ohos_src_dir) 82 83 flags = os.O_WRONLY | os.O_CREAT 84 mode = stat.S_IWUSR | stat.S_IRUSR 85 with os.fdopen(os.open(out_file, flags, mode), 'w') as f: 86 json.dump(data_filter, f, indent=4) 87 88 89def add_content(data, src_content, prefix=''): 90 if len(prefix) == 0: 91 for content in src_content: 92 data = data + ' ' + content + '\n' 93 else: 94 data = data + ' ' + prefix + '{' + '\n' 95 for content in src_content: 96 data = data + ' ' + content + '\n' 97 data = data + ' }' + '\n' 98 return data 99 100 101def copy_content(src_file, dest_file, out_file): 102 if not os.path.exists(src_file) or not os.path.exists(dest_file): 103 return 104 105 with open(src_file, 'r', encoding='utf-8') as f: 106 src_content = f.read().splitlines() 107 108 with open(dest_file, 'r', encoding='utf-8') as f: 109 dest_content = f.read().splitlines(True) 110 111 data = '' 112 signal = 0 113 for index, line in enumerate(dest_content): 114 if (index == len(dest_content) - 1) and (signal != 3): 115 if signal == 0: 116 data = add_content(data, src_content, 'units') 117 signal = 1 118 if signal == 1: 119 data = add_content(data, src_content, 'unitsNarrow') 120 signal = 2 121 if signal == 2: 122 data = add_content(data, src_content, 'unitsShort') 123 data += line 124 elif ('unitsShort{' in line) or ('unitsShort:' in line): 125 if signal == 0: 126 data = add_content(data, src_content, 'units') 127 signal = 1 128 if signal == 1: 129 data = add_content(data, src_content, 'unitsNarrow') 130 data += line 131 if 'unitsShort{' in line: 132 data = add_content(data, src_content) 133 signal = 3 134 elif ('unitsNarrow{' in line) or ('unitsNarrow:' in line): 135 if signal == 0: 136 data = add_content(data, src_content, 'units') 137 data += line 138 if 'unitsNarrow{' in line: 139 data = add_content(data, src_content) 140 signal = 2 141 elif ('units{' in line) or ('units:' in line): 142 data += line 143 if 'units{' in line: 144 data = add_content(data, src_content) 145 signal = 1 146 else: 147 data += line 148 149 flags = os.O_WRONLY | os.O_CREAT 150 mode = stat.S_IWUSR | stat.S_IRUSR 151 with os.fdopen(os.open(out_file, flags, mode), 'w') as f: 152 f.write(data) 153 154 155def copy_file(src_file, dest_file): 156 if not os.path.exists(src_file): 157 return 158 159 with open(src_file, 'r', encoding='utf-8') as f: 160 data = f.read() 161 162 flags = os.O_WRONLY | os.O_CREAT 163 mode = stat.S_IWUSR | stat.S_IRUSR 164 with os.fdopen(os.open(dest_file, flags, mode), 'w') as f: 165 f.write(data) 166 167 168def add_content_misc(src_file, dest_file, out_file): 169 if not os.path.exists(src_file) or not os.path.exists(dest_file): 170 return 171 172 with open(src_file, 'r', encoding='utf-8') as f: 173 src_content = f.read().splitlines() 174 175 convert_units = [] 176 unit_quantities = [] 177 signal = 0 178 for content in src_content: 179 if "convertUnits:" in content: 180 signal = 1 181 continue 182 if "unitQuantities:" in content: 183 signal = 2 184 continue 185 if signal == 1: 186 convert_units.append(content) 187 if signal == 2: 188 unit_quantities.append(content) 189 190 with open(dest_file, 'r', encoding='utf-8') as f: 191 dest_content = f.read().splitlines(True) 192 193 data = '' 194 for line in dest_content: 195 data += line 196 if "convertUnits{" in line: 197 for content in convert_units: 198 data = "{}{}\n".format(data, content) 199 if "unitQuantities{" in line: 200 for content in unit_quantities: 201 data = "{}{}\n".format(data, content) 202 203 flags = os.O_WRONLY | os.O_CREAT 204 mode = stat.S_IWUSR | stat.S_IRUSR 205 with os.fdopen(os.open(out_file, flags, mode), 'w') as f: 206 f.write(data) 207 208if __name__ == '__main__': 209 parser = argparse.ArgumentParser() 210 parser.add_argument('--icu_src_dir', type=str) 211 parser.add_argument('--ohos_src_dir', type=str) 212 parser.add_argument('--out_dir', type=str) 213 args = parser.parse_args() 214 215 unit_path = 'unit' 216 misc_path = 'misc' 217 units_file = 'units.txt' 218 filter_file = 'data_filter.json' 219 out_dir = os.path.join(args.out_dir, 'out', 'temp') 220 os.makedirs(os.path.join(out_dir, unit_path), exist_ok=True) 221 os.makedirs(os.path.join(out_dir, misc_path), exist_ok=True) 222 223 if os.path.exists(os.path.join(args.ohos_src_dir, unit_path)): 224 for file in os.listdir(os.path.join(args.ohos_src_dir, unit_path)): 225 copy_content(os.path.join(args.ohos_src_dir, unit_path, file), 226 os.path.join(args.icu_src_dir, unit_path, file), 227 os.path.join(out_dir, unit_path, file)) 228 229 add_content_misc(os.path.join(args.ohos_src_dir, misc_path, units_file), 230 os.path.join(args.icu_src_dir, misc_path, units_file), 231 os.path.join(out_dir, misc_path, units_file)) 232 generate_json_file(args.ohos_src_dir, os.path.join(out_dir, filter_file)) 233