15f9996aaSopenharmony_ci#!/usr/bin/env python3
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2023 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_ci
175f9996aaSopenharmony_ciimport argparse
185f9996aaSopenharmony_ciimport os
195f9996aaSopenharmony_ciimport sys
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_ci
225f9996aaSopenharmony_cidef get_source_name(module_type: str, name: str, prefix_override: str, suffix: str):
235f9996aaSopenharmony_ci    """generate source file name by type"""
245f9996aaSopenharmony_ci    if (module_type == 'lib' or module_type == 'lib64') and not prefix_override:
255f9996aaSopenharmony_ci        if not name.startswith('lib'):
265f9996aaSopenharmony_ci            name = 'lib' + name
275f9996aaSopenharmony_ci    alias = ''
285f9996aaSopenharmony_ci    if suffix:
295f9996aaSopenharmony_ci        name = name + suffix
305f9996aaSopenharmony_ci    if module_type == 'none':
315f9996aaSopenharmony_ci        name = ''
325f9996aaSopenharmony_ci    return name, alias
335f9996aaSopenharmony_ci
345f9996aaSopenharmony_ci
355f9996aaSopenharmony_cidef gen_install_dests(system_base_dir: str, source_file_name: str, install_images: list, module_install_dir: str, relative_install_dir: str, module_type: str):
365f9996aaSopenharmony_ci    """generate module install dir by user config"""
375f9996aaSopenharmony_ci    dest = ''
385f9996aaSopenharmony_ci    _install_dir = ''
395f9996aaSopenharmony_ci    for image in install_images:
405f9996aaSopenharmony_ci        if image != 'system':
415f9996aaSopenharmony_ci            continue
425f9996aaSopenharmony_ci        if module_install_dir != '':
435f9996aaSopenharmony_ci            _install_dir = os.path.join(system_base_dir, module_install_dir)
445f9996aaSopenharmony_ci        elif relative_install_dir != '':
455f9996aaSopenharmony_ci            _install_dir = os.path.join(
465f9996aaSopenharmony_ci                system_base_dir, module_type, relative_install_dir)
475f9996aaSopenharmony_ci        else:
485f9996aaSopenharmony_ci            _install_dir = os.path.join(system_base_dir, module_type)
495f9996aaSopenharmony_ci    dest = os.path.join(_install_dir, source_file_name)
505f9996aaSopenharmony_ci    return dest
515f9996aaSopenharmony_ci
525f9996aaSopenharmony_ci
535f9996aaSopenharmony_cidef main():
545f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
555f9996aaSopenharmony_ci    parser.add_argument('--system-base-dir', required=True,
565f9996aaSopenharmony_ci                        help='system base dir')
575f9996aaSopenharmony_ci    parser.add_argument('--install-images', nargs='+', help='install images')
585f9996aaSopenharmony_ci    parser.add_argument('--module-install-dir', required=False,
595f9996aaSopenharmony_ci                        default='', help='module install dir')
605f9996aaSopenharmony_ci    parser.add_argument('--relative-install-dir', required=False,
615f9996aaSopenharmony_ci                        default='', help='relative install dir')
625f9996aaSopenharmony_ci    parser.add_argument('--type', required=True, help='module type')
635f9996aaSopenharmony_ci    parser.add_argument('--install-name', required=True,
645f9996aaSopenharmony_ci                        help='module install name')
655f9996aaSopenharmony_ci    parser.add_argument('--prefix-override', dest='prefix_override',
665f9996aaSopenharmony_ci                        action='store_true', help='prefix override')
675f9996aaSopenharmony_ci    parser.set_defaults(prefix_override=False)
685f9996aaSopenharmony_ci    parser.add_argument('--suffix', required=False, default='', help='suffix')
695f9996aaSopenharmony_ci    parser.add_argument('--allowed-lib-list', help='')
705f9996aaSopenharmony_ci    args = parser.parse_args()
715f9996aaSopenharmony_ci
725f9996aaSopenharmony_ci    source_file_name = ''
735f9996aaSopenharmony_ci    source_file_name, alt_source_file_name = get_source_name(
745f9996aaSopenharmony_ci        args.type, args.install_name, args.prefix_override, args.suffix)
755f9996aaSopenharmony_ci    install_dest = ''
765f9996aaSopenharmony_ci    if args.install_images:
775f9996aaSopenharmony_ci        install_dest = gen_install_dests(args.system_base_dir, source_file_name, args.install_images,
785f9996aaSopenharmony_ci                                         args.module_install_dir, args.relative_install_dir, args.type)
795f9996aaSopenharmony_ci    with open(args.allowed_lib_list, 'r') as f:
805f9996aaSopenharmony_ci        lines = f.readlines()
815f9996aaSopenharmony_ci    lines = [line.strip()[1:] for line in lines]
825f9996aaSopenharmony_ci    if install_dest in lines:
835f9996aaSopenharmony_ci        return 0
845f9996aaSopenharmony_ci    else:
855f9996aaSopenharmony_ci        print(f"{install_dest} not in allowed_so_list")
865f9996aaSopenharmony_ci        return 1
875f9996aaSopenharmony_ci
885f9996aaSopenharmony_ci
895f9996aaSopenharmony_ciif __name__ == '__main__':
905f9996aaSopenharmony_ci    sys.exit(main())
91