parse_parts_config.py 3.6 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env python3
# coding=utf-8
#
# Copyright (c) 2020 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.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
import json

from core.utils import get_build_output_path
M
mamingshuai 已提交
23
from core.common import is_open_source_product
M
mamingshuai 已提交
24 25 26 27 28 29 30 31 32 33 34
from core.config.config_manager import UserConfigManager


##############################################################################
##############################################################################

class ParsePartsConfig(object):
    def __init__(self, productform):
        self.productform = productform
        self.subsystem_infos, self.part_infos = self.get_infos_data()

M
mamingshuai 已提交
35
    def get_config_file_path(self):
M
mamingshuai 已提交
36 37 38 39 40 41
        manager = UserConfigManager()
        testcase_dir = manager.get_test_cases_dir()

        if testcase_dir == "":
            if sys.source_code_root_path != "":
                config_filepath = os.path.join(
M
mamingshuai 已提交
42
                    get_build_output_path(self.productform),
M
mamingshuai 已提交
43 44 45 46 47 48 49 50 51 52 53 54
                    "build_configs",
                    "infos_for_testfwk.json")
            else:
                config_filepath = ""
        else:
            config_filepath = os.path.join(
                testcase_dir,
                "infos_for_testfwk.json")
        return config_filepath

    def get_infos_data(self):
        config_filepath = self.get_config_file_path()
L
lvgang 已提交
55

M
mamingshuai 已提交
56 57 58 59 60 61 62 63 64 65 66
        if not os.path.exists(config_filepath):
            print("Error: %s is not exist." % config_filepath)
            return None, None

        data_dic = None
        with open(config_filepath, 'r') as file_handle:
            data_dic = json.load(file_handle)
            if not data_dic:
                print("Error: json file load error.")
                return None, None

M
mamingshuai 已提交
67 68 69 70
        if is_open_source_product(self.productform):
            product_data_dic = data_dic.get("phone", None)
        else:
            product_data_dic = data_dic.get(self.productform, None)
71

M
mamingshuai 已提交
72 73 74
        if product_data_dic is None:
            print("Error: product_data_dic is None.")
            return None, None
75

M
mamingshuai 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        subsystem_infos = product_data_dic.get("subsystem_infos", None)
        part_infos = product_data_dic.get("part_infos", None)
        return subsystem_infos, part_infos

    def get_subsystem_infos(self):
        return self.subsystem_infos

    def get_part_infos(self):
        return self.part_infos

    def get_subsystem_name_list(self):
        subsystem_name_list = []
        if self.subsystem_infos:
            for item in self.subsystem_infos:
                subsystem_name_list.append(item)
        return subsystem_name_list

    def get_part_list(self, subsystemlist, partlist):
        if len(partlist) != 0:
            return partlist
        if self.subsystem_infos is None:
            return subsystemlist

        part_name_list = []
        if len(subsystemlist) != 0:
            for item in subsystemlist:
                parts = self.subsystem_infos.get(item, [])
                part_name_list.extend(parts)
        return part_name_list


##############################################################################
##############################################################################