15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ciimport sys
175f9996aaSopenharmony_ciimport os
185f9996aaSopenharmony_ciimport argparse
195f9996aaSopenharmony_ciimport shutil
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_ci
225f9996aaSopenharmony_cidef _merge_txt_file(ohos_notice: str, a_notice: str, output_notice: str):
235f9996aaSopenharmony_ci    if not os.path.exists(ohos_notice):
245f9996aaSopenharmony_ci        print("Warning, can not find the ohos notice file: {}.".format(
255f9996aaSopenharmony_ci            ohos_notice))
265f9996aaSopenharmony_ci        return
275f9996aaSopenharmony_ci
285f9996aaSopenharmony_ci    if not os.path.exists(a_notice):
295f9996aaSopenharmony_ci        print("Warning, can not find the notice file: {}.".format(a_notice))
305f9996aaSopenharmony_ci        shutil.move(ohos_notice, a_notice)
315f9996aaSopenharmony_ci        return
325f9996aaSopenharmony_ci
335f9996aaSopenharmony_ci    with open(output_notice, 'a') as a_file:
345f9996aaSopenharmony_ci        with open(ohos_notice, 'r', errors='ignore') as _file:
355f9996aaSopenharmony_ci            data = _file.readlines()
365f9996aaSopenharmony_ci            del data[0]
375f9996aaSopenharmony_ci            for line in data:
385f9996aaSopenharmony_ci                a_file.write(line)
395f9996aaSopenharmony_ci        with open(a_notice, 'r', errors='ignore') as _file:
405f9996aaSopenharmony_ci            data = _file.readlines()
415f9996aaSopenharmony_ci            del data[0]
425f9996aaSopenharmony_ci            for line in data:
435f9996aaSopenharmony_ci                a_file.write(line)
445f9996aaSopenharmony_ci
455f9996aaSopenharmony_ci    if os.path.exists(ohos_notice):
465f9996aaSopenharmony_ci        os.remove(ohos_notice)
475f9996aaSopenharmony_ci
485f9996aaSopenharmony_ci
495f9996aaSopenharmony_cidef main() -> int:
505f9996aaSopenharmony_ci    """notice file merge."""
515f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
525f9996aaSopenharmony_ci    parser.add_argument('--ohos-notice', required=True)
535f9996aaSopenharmony_ci    parser.add_argument('--a-notice', required=True)
545f9996aaSopenharmony_ci    parser.add_argument('--output-notice', required=True)
555f9996aaSopenharmony_ci    args = parser.parse_args()
565f9996aaSopenharmony_ci
575f9996aaSopenharmony_ci    # merge NOTICE.txt file
585f9996aaSopenharmony_ci    _merge_txt_file(args.ohos_notice, args.a_notice, args.output_notice)
595f9996aaSopenharmony_ci
605f9996aaSopenharmony_ci    return 0
615f9996aaSopenharmony_ci
625f9996aaSopenharmony_ci
635f9996aaSopenharmony_ciif __name__ == '__main__':
645f9996aaSopenharmony_ci    sys.exit(main())
65