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 sys
17import os
18
19sys.path.append(os.path.dirname(
20    os.path.dirname(os.path.dirname(os.path.dirname(
21        os.path.abspath(__file__))))))
22from scripts.util.file_utils import read_json_file  # noqa: E402
23
24
25def __get_relative_install_dir(categories):
26    is_platforsdk = False
27    is_chipsetsdk = False
28    for cat in categories:
29        if cat == "ndk":
30            return "ndk"
31        if cat.startswith("platformsdk"):
32            is_platforsdk = True
33        elif cat.startswith("chipsetsdk"):
34            is_chipsetsdk = True
35        elif cat.startswith("passthrough"):
36            return "chipsetsdk"
37    if is_platforsdk and is_chipsetsdk:
38        return "chipset-pub-sdk"
39    elif is_platforsdk:
40        return "platformsdk"
41    elif is_chipsetsdk:
42        return "chipset-sdk"
43    return ""
44
45
46def load_categorized_libraries(file_name):
47    res = read_json_file(file_name)
48
49    for key, val in res.items():
50        val["relative_install_dir"] = __get_relative_install_dir(val["categories"])
51    return res
52
53
54#
55# module_info is an json object including label and dest
56# update_module_info will update dest according to label category
57#
58def update_module_info(module_info, categorized_libraries):
59    if "dest" not in module_info:
60        return
61
62    label = module_info["label"]
63    pos = label.find("(")
64    if pos > 0:
65        label = label[:label.find("(")]
66    if label not in categorized_libraries:
67        return
68
69    dest = []
70    for item in module_info["dest"]:
71        pos = item.rfind("/")
72        if pos < 0:
73            dest.append(item)
74            continue
75        lib_name = item[pos + 1:]
76        dir_name = item[:pos]
77
78        if dir_name.endswith("chipset-sdk") or dir_name.endswith("platformsdk") \
79        or dir_name.endswith("chipset-pub-sdk") or dir_name.endswith("ndk") or dir_name.endswith("chipsetsdk"):
80            dir_name = dir_name[:dir_name.rfind("/")]
81        dest.append("{}/{}/{}".format(dir_name, categorized_libraries[label]["relative_install_dir"], lib_name))
82    module_info["dest"] = dest
83
84
85if __name__ == '__main__':
86    categories = load_categorized_libraries(os.path.join(
87          os.path.dirname(os.path.abspath(__file__)), "categorized-libraries.json"))
88    module_info = {
89        "dest": ["system/lib/chipset-pub-sdk/libaccesstoken_sdk.z.so"],
90        "label": "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk"
91    }
92    update_module_info(module_info, categories)
93    print(module_info)
94