1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2022 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 20import sys 21import tempfile 22from dataclasses import dataclass 23 24__all__ = ["Variables"] 25 26SRC_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 27MODULES_DIR = os.path.abspath(os.path.dirname(__file__)) 28TOP_DIR = os.path.abspath( 29 os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) 30sys.path.insert(0, SRC_DIR) 31sys.path.insert(1, MODULES_DIR) 32sys.path.insert(2, TOP_DIR) 33 34 35@dataclass 36class ReportVariables: 37 report_dir = "" 38 log_dir = "" 39 log_format = "" 40 log_level = "" 41 log_handler = "" 42 pub_key_file = None 43 pub_key_string = "" 44 45 46@dataclass 47class Variables: 48 modules_dir = "" 49 top_dir = "" 50 res_dir = "" 51 exec_dir = "" 52 temp_dir = "" 53 report_vars = ReportVariables() 54 task_name = "" 55 source_code_rootpath = "" 56 57 58def _init_global_config(): 59 import logging 60 61 from _core.common import get_source_code_rootpath 62 63 Variables.modules_dir = MODULES_DIR 64 Variables.top_dir = TOP_DIR 65 Variables.res_dir = os.path.abspath(os.path.join( 66 MODULES_DIR, "_core", "resource")) 67 68 # create xdevice temp folder 69 Variables.temp_dir = os.path.join(tempfile.gettempdir(), 70 "xdevice_data") 71 if not os.path.exists(Variables.temp_dir): 72 os.makedirs(Variables.temp_dir) 73 74 # set report variables 75 Variables.report_vars.log_dir = "log" 76 Variables.report_vars.report_dir = "reports" 77 Variables.report_vars.log_format = \ 78 "[%(asctime)s] [%(thread)d] [%(name)s] [%(levelname)s] %(message)s" 79 Variables.report_vars.log_level = logging.INFO 80 Variables.report_vars.log_handler = "console, file" 81 82 # set execution directory 83 if not Variables.exec_dir: 84 current_exec_dir = os.path.abspath(os.getcwd()) 85 try: 86 common_path = os.path.commonpath([Variables.top_dir, 87 current_exec_dir]) 88 if os.path.normcase(common_path) == os.path.normcase( 89 Variables.top_dir): 90 Variables.exec_dir = common_path 91 else: 92 Variables.exec_dir = current_exec_dir 93 except (ValueError, AttributeError) as _: 94 Variables.exec_dir = current_exec_dir 95 Variables.source_code_rootpath = get_source_code_rootpath( 96 Variables.top_dir) 97 _init_logger() 98 99 100def _init_logger(): 101 import time 102 from _core.constants import LogType 103 from _core.logger import Log 104 from _core.plugin import Plugin 105 from _core.plugin import get_plugin 106 107 tool_logger_plugin = get_plugin(Plugin.LOG, LogType.tool) 108 if tool_logger_plugin: 109 return 110 111 @Plugin(type=Plugin.LOG, id=LogType.tool, enabled=True) 112 class ToolLog(Log): 113 @classmethod 114 def get_plugin_type(cls): 115 return Plugin.LOG 116 117 @classmethod 118 def get_plugin_id(cls): 119 return LogType.tool 120 121 tool_log_file = None 122 if Variables.exec_dir and os.path.normcase( 123 Variables.exec_dir) == os.path.normcase(Variables.top_dir): 124 host_log_path = os.path.join(Variables.exec_dir, 125 Variables.report_vars.report_dir, 126 Variables.report_vars.log_dir) 127 os.makedirs(host_log_path, exist_ok=True) 128 time_str = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()) 129 tool_file_name = "platform_log_{}.log".format(time_str) 130 tool_log_file = os.path.join(host_log_path, tool_file_name) 131 132 tool_logger_plugin = get_plugin(Plugin.LOG, LogType.tool)[0] or ToolLog() 133 tool_logger_plugin.__initial__(Variables.report_vars.log_handler, 134 tool_log_file, 135 Variables.report_vars.log_level, 136 Variables.report_vars.log_format) 137 138 139def _iter_module_plugins(packages): 140 import importlib 141 import pkgutil 142 for package in packages: 143 pkg_path = getattr(package, "__path__", "") 144 pkg_name = getattr(package, "__name__", "") 145 if not pkg_name or not pkg_path: 146 continue 147 148 _iter_modules = pkgutil.iter_modules(pkg_path, "%s%s" % ( 149 pkg_name, ".")) 150 for _, name, _ in _iter_modules: 151 importlib.import_module(name) 152 153 154def _load_internal_plugins(): 155 import _core.driver 156 import _core.testkit 157 import _core.environment 158 import _core.executor 159 _iter_module_plugins([_core.driver, _core.testkit, _core.environment, 160 _core.executor]) 161 162 163_init_global_config() 164_load_internal_plugins() 165 166del _init_global_config 167del _init_logger 168del _load_internal_plugins 169del _iter_module_plugins 170