_init_global_config.py 4.3 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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.
#


def _init_global_config():
    import sys
    import os

    # insert src path for loading xdevice modules
C
cuishuangxi 已提交
25
    # 当前脚本运行的绝对路径 去掉最后两个路径
Z
zhongzhi 已提交
26
    # 变量注释 framework_src_dir = OpenHarmony/test/developertest
M
mamingshuai 已提交
27 28
    sys.framework_src_dir = os.path.abspath(os.path.dirname(
        os.path.dirname(__file__)))
C
cuishuangxi 已提交
29 30

    # 将目录存放到sys.path模块中,新添加的目录会优先于其他目录被import检查 0代表最高优先级
W
wenjun 已提交
31
    sys.path.insert(0, sys.framework_src_dir)
C
cuishuangxi 已提交
32 33

    # 当前脚本运行的绝对路径 去掉最后两个路径
Z
zhongzhi 已提交
34
    # 变量注释 framework_root_dir = OpenHarmony/test/developertest
M
mamingshuai 已提交
35 36
    sys.framework_root_dir = os.path.abspath(os.path.dirname(
        os.path.dirname(os.path.dirname(__file__))))
W
wenjun 已提交
37

Z
zhongzhi 已提交
38
    # 变量注释 sys.xdevice_dir = OpenHarmony/test/xdevice/src
W
wenjun 已提交
39 40 41 42 43 44 45
    sys.xdevice_dir = os.path.abspath(os.path.join(
        sys.framework_root_dir,
        "..",
        "xdevice",
        "src"))
    sys.path.insert(0, sys.xdevice_dir)

Z
zhongzhi 已提交
46
    # 变量注释 sys.xdevice_extension_dir = OpenHarmony/xdevice/extension/src
M
mamingshuai 已提交
47 48 49 50 51 52 53 54
    sys.xdevice_extension_dir = os.path.abspath(os.path.join(
        sys.framework_root_dir,
        "..",
        "xdevice",
        "extension",
        "src"))
    sys.path.insert(1, sys.xdevice_extension_dir)

Z
zhongzhi 已提交
55
    # 变量注释 pytest_dir = OpenHarmony/test/developertest/aw/python
M
mamingshuai 已提交
56 57 58 59 60 61
    sys.pytest_dir = os.path.abspath(os.path.join(
        sys.framework_root_dir,
        "aw",
        "python"))
    sys.path.insert(2, sys.pytest_dir)

Z
zhongzhi 已提交
62
    # 变量注释 adapter_dir = OpenHarmony/test/developertest/adapter/aw/python
M
mamingshuai 已提交
63 64 65 66 67 68 69
    sys.adapter_dir = os.path.abspath(os.path.join(
        sys.framework_root_dir,
        "adapter"
        "aw",
        "python"))
    sys.path.insert(3, sys.adapter_dir)

Z
zhongzhi 已提交
70
    # 变量注释 hmh_script = OpenHarmony/test/developertest/libs
M
mamingshuai 已提交
71 72
    sys.hmh_script = os.path.abspath(os.path.join(
        sys.framework_root_dir,
N
NicoYam 已提交
73
        "libs"))
M
mamingshuai 已提交
74 75
    sys.path.insert(4, sys.hmh_script)

Z
zhongzhi 已提交
76
    # 变量注释 framework_res_dir = OpenHarmony/test/developertest
M
mamingshuai 已提交
77
    sys.framework_res_dir = sys.framework_root_dir
C
cuishuangxi 已提交
78

Z
zhongzhi 已提交
79
    # 变量注释 exec_dir = OpenHarmony/test/developertest
W
wenjun 已提交
80 81 82 83 84 85
    sys.exec_dir = sys.framework_root_dir

    from core.common import get_source_code_root_path
    sys.source_code_root_path = get_source_code_root_path(
        sys.framework_root_dir)

C
cuishuangxi 已提交
86
    # python的参数配置 设置脚本路径 调度python的xdevice
M
mamingshuai 已提交
87 88 89 90 91 92 93 94
    from xdevice import Variables
    Variables.exec_dir = sys.framework_root_dir


def _iter_module_plugins(packages):
    import importlib
    import pkgutil
    for package in packages:
C
cuishuangxi 已提交
95 96

        # 获取__path__对象属性的值,若不存在,默认为“”
M
mamingshuai 已提交
97 98 99 100 101 102 103 104 105 106 107 108
        pkg_path = getattr(package, "__path__", "")
        pkg_name = getattr(package, "__name__", "")
        if not pkg_name or not pkg_path:
            continue
        _iter_modules = pkgutil.iter_modules(pkg_path, "%s%s" % (
            pkg_name, "."))
        for _, name, _ in _iter_modules:
            importlib.import_module(name)


def _load_internal_plugins():
    import core.driver
N
NicoYam 已提交
109 110
    import benchmark.report.benchmark_reporter
    _iter_module_plugins([core.driver, benchmark.report.benchmark_reporter])
M
mamingshuai 已提交
111 112 113 114

    try:
        import xdevice_extension._core.environment
        _iter_module_plugins([xdevice_extension._core.environment])
R
renxiang 已提交
115 116
        import xdevice_extension._core.driver
        _iter_module_plugins([xdevice_extension._core.driver])
M
mamingshuai 已提交
117 118 119
    except (ModuleNotFoundError, ImportError):
        pass

M
mamingshuai 已提交
120 121 122 123 124
    try:
        import script.report
        _iter_module_plugins([script.report])
    except (ModuleNotFoundError, ImportError):
        pass
M
mamingshuai 已提交
125

W
wenjun 已提交
126 127
_init_global_config()
del _init_global_config
M
mamingshuai 已提交
128 129 130

_load_internal_plugins()
del _load_internal_plugins