• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20
21from _core.logger import platform_logger
22from _core.environment.manager_env import DeviceSelectionOption
23from _core.constants import DeviceLabelType
24from _core.constants import ConfigConst
25from _core.utils import is_config_str
26
27LOG = platform_logger("OptionUtil")
28
29
30def find_device_options(environment_config, options, test_source):
31    devices_option = []
32    index = 1
33    for device_dict in environment_config:
34        label = device_dict.get("label", "")
35        required_manager = device_dict.get("type", "device")
36        required_manager = \
37            required_manager if required_manager else "device"
38        if not label:
39            continue
40        device_option = DeviceSelectionOption(options, label, test_source)
41        device_dict.pop("type", None)
42        device_dict.pop("label", None)
43        device_option.required_manager = required_manager
44        device_option.extend_value = device_dict
45        device_option.source_file = \
46            test_source.config_file or test_source.source_string
47        if hasattr(device_option, "env_index"):
48            device_option.env_index = index
49        index += 1
50        devices_option.append(device_option)
51    return devices_option
52
53
54def calculate_device_options(device_options, environment_config,
55                             options, test_source):
56    # calculate difference
57    diff_value = len(environment_config) - len(device_options)
58    if device_options and diff_value == 0:
59        return device_options
60
61    else:
62        diff_value = diff_value if diff_value else 1
63        if str(test_source.source_file).endswith(".bin"):
64            device_option = DeviceSelectionOption(
65                options, DeviceLabelType.ipcamera, test_source)
66        else:
67            device_option = DeviceSelectionOption(
68                options, None, test_source)
69
70        device_option.source_file = \
71            test_source.source_file or test_source.source_string
72        device_option.required_manager = "device"
73        for device_dict in environment_config:
74            label = device_dict.get("label", None)
75            if label is None or label == '':
76                import copy
77                dst_option = copy.deepcopy(device_option)
78                dst_option.extend_value = device_dict
79                device_options.append(dst_option)
80        if not environment_config:
81            device_options.extend([device_option] * diff_value)
82        LOG.debug("Assign device options and it's length is %s"
83                  % len(device_options))
84    return device_options
85
86
87def get_device_options(options, test_source):
88    device_options = []
89    config_file = test_source.config_file
90    environment_config = []
91    from _core.testkit.json_parser import JsonParser
92    if test_source.source_string and is_config_str(
93            test_source.source_string):
94        json_config = JsonParser(test_source.source_string)
95        environment_config = json_config.get_environment()
96        device_options = find_device_options(
97            environment_config, options, test_source)
98    elif config_file and os.path.exists(config_file):
99        json_config = JsonParser(test_source.config_file)
100        environment_config = json_config.get_environment()
101        device_options = find_device_options(
102            environment_config, options, test_source)
103
104    device_options = calculate_device_options(
105        device_options, environment_config, options, test_source)
106
107    if ConfigConst.component_mapper in options.keys():
108        required_component = options.get(ConfigConst.component_mapper). \
109            get(test_source.module_name, None)
110        for device_option in device_options:
111            device_option.required_component = required_component
112    return device_options
113