From aa1a98db0cdd9d15fde0ae006a3004cd178e0ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=89=91?= Date: Tue, 5 Nov 2024 20:07:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=AF=91=E8=84=9A=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王剑 --- ohos_icu4c/build_data/ohos_data.py | 43 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/ohos_icu4c/build_data/ohos_data.py b/ohos_icu4c/build_data/ohos_data.py index 8e655139..d03cb22e 100644 --- a/ohos_icu4c/build_data/ohos_data.py +++ b/ohos_icu4c/build_data/ohos_data.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- # Copyright (c) 2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,16 +15,17 @@ import argparse import os +import stat def add_content(data, src_content, prefix=''): if len(prefix) == 0: for content in src_content: - data = data + ' ' + content + '\n' + data = data + ' ' + content + '\n' else: data = data + ' ' + prefix + '{' + '\n' for content in src_content: - data = data + ' ' + content + '\n' + data = data + ' ' + content + '\n' data = data + ' }' + '\n' return data @@ -33,15 +36,13 @@ def copy_content(src_file, dest_file, out_file): src_content = f.read().splitlines() else: src_content = [] - + with open(dest_file, 'r', encoding='utf-8') as f: dest_content = f.read().splitlines(True) data = '' signal = 0 - for index in range(len(dest_content)): - line = dest_content[index] - + for index, line in enumerate(dest_content): if (index == len(dest_content) - 1) and (signal != 3): if signal == 0: data = add_content(data, src_content, 'units') @@ -77,7 +78,9 @@ def copy_content(src_file, dest_file, out_file): else: data += line - with open(out_file, 'w', encoding='utf-8') as f: + flags = os.O_WRONLY | os.O_CREAT + mode = stat.S_IWUSR | stat.S_IRUSR + with os.fdopen(os.open(out_file, flags, mode), 'w') as f: f.write(data) @@ -85,24 +88,28 @@ def copy_file(src_file, dest_file): with open(src_file, 'r', encoding='utf-8') as f: data = f.read() - with open(dest_file, 'w', encoding='utf-8') as f: + flags = os.O_WRONLY | os.O_CREAT + mode = stat.S_IWUSR | stat.S_IRUSR + with os.fdopen(os.open(dest_file, flags, mode), 'w') as f: f.write(data) -if __name__=='__main__': +if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--icu_src_dir', type=str) parser.add_argument('--ohos_src_dir', type=str) parser.add_argument('--out_dir', type=str) args = parser.parse_args() - - out_dir = args.out_dir + '/out/temp/' - unit_path = '/unit/' - filter_file = '/data_filter.json' - os.makedirs(out_dir + unit_path, exist_ok=True) - for file in os.listdir(args.ohos_src_dir + unit_path): - copy_content(args.ohos_src_dir + unit_path + file, - args.icu_src_dir + unit_path + file, out_dir + unit_path + file) + unit_path = 'unit' + filter_file = 'data_filter.json' + out_dir = os.path.join(args.out_dir, 'out', 'temp') + os.makedirs(os.path.join(out_dir, unit_path), exist_ok=True) - copy_file(args.ohos_src_dir + filter_file, out_dir + filter_file) + if os.path.exists(os.path.join(args.ohos_src_dir, unit_path)): + for file in os.listdir(os.path.join(args.ohos_src_dir, unit_path)): + copy_content(os.path.join(args.ohos_src_dir, unit_path, file), + os.path.join(args.icu_src_dir, unit_path, file), + os.path.join(out_dir, unit_path, file)) + + copy_file(os.path.join(args.ohos_src_dir, filter_file), os.path.join(out_dir, filter_file))