• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020 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
19
20def _init_global_config():
21    import sys
22    import os
23
24    # insert src path for loading xdevice modules
25    sys.framework_src_dir = os.path.abspath(os.path.dirname(
26        os.path.dirname(__file__)))
27    sys.path.insert(0, sys.framework_src_dir)
28    sys.framework_root_dir = os.path.abspath(os.path.dirname(
29        os.path.dirname(os.path.dirname(__file__))))
30
31    sys.xdevice_dir = os.path.abspath(os.path.join(
32        sys.framework_root_dir,
33        "..",
34        "xdevice",
35        "src"))
36    sys.path.insert(0, sys.xdevice_dir)
37
38    sys.xdevice_extension_dir = os.path.abspath(os.path.join(
39        sys.framework_root_dir,
40        "..",
41        "xdevice",
42        "extension",
43        "src"))
44    sys.path.insert(1, sys.xdevice_extension_dir)
45
46    sys.pytest_dir = os.path.abspath(os.path.join(
47        sys.framework_root_dir,
48        "aw",
49        "python"))
50    sys.path.insert(2, sys.pytest_dir)
51
52    sys.adapter_dir = os.path.abspath(os.path.join(
53        sys.framework_root_dir,
54        "adapter"
55        "aw",
56        "python"))
57    sys.path.insert(3, sys.adapter_dir)
58
59    sys.hmh_script = os.path.abspath(os.path.join(
60        sys.framework_root_dir,
61        "libs"))
62    sys.path.insert(4, sys.hmh_script)
63
64    sys.framework_res_dir = sys.framework_root_dir
65    sys.exec_dir = sys.framework_root_dir
66
67    from core.common import get_source_code_root_path
68    sys.source_code_root_path = get_source_code_root_path(
69        sys.framework_root_dir)
70
71    from xdevice import Variables
72    Variables.exec_dir = sys.framework_root_dir
73
74
75def _iter_module_plugins(packages):
76    import importlib
77    import pkgutil
78    for package in packages:
79        pkg_path = getattr(package, "__path__", "")
80        pkg_name = getattr(package, "__name__", "")
81        if not pkg_name or not pkg_path:
82            continue
83        _iter_modules = pkgutil.iter_modules(pkg_path, "%s%s" % (
84            pkg_name, "."))
85        for _, name, _ in _iter_modules:
86            importlib.import_module(name)
87
88
89def _load_internal_plugins():
90    import core.driver
91    import benchmark.report.benchmark_reporter
92    _iter_module_plugins([core.driver, benchmark.report.benchmark_reporter])
93
94    try:
95        import xdevice_extension._core.environment
96        _iter_module_plugins([xdevice_extension._core.environment])
97        import xdevice_extension._core.driver
98        _iter_module_plugins([xdevice_extension._core.driver])
99    except (ModuleNotFoundError, ImportError):
100        pass
101
102    try:
103        import script.report
104        _iter_module_plugins([script.report])
105    except (ModuleNotFoundError, ImportError):
106        pass
107
108_init_global_config()
109del _init_global_config
110
111_load_internal_plugins()
112del _load_internal_plugins
113