• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19
20def _init_global_config():
21    import sys
22    import os
23
24    # insert src path for loading xdevice modules
25    # 当前脚本运行的绝对路径 去掉最后两个路径
26    # 变量注释 framework_src_dir = OpenHarmony/test/developer_test
27    sys.framework_src_dir = os.path.abspath(os.path.dirname(
28        os.path.dirname(__file__)))
29
30    # 将目录存放到sys.path模块中,新添加的目录会优先于其他目录被import检查 0代表最高优先级
31    sys.path.insert(0, sys.framework_src_dir)
32
33    # 当前脚本运行的绝对路径 去掉最后两个路径
34    # 变量注释 framework_root_dir = OpenHarmony/test/developer_test
35    sys.framework_root_dir = os.path.abspath(os.path.dirname(
36        os.path.dirname(os.path.dirname(__file__))))
37
38    # 变量注释 sys.xdevice_dir = OpenHarmony/test/xdevice/src
39    sys.xdevice_dir = os.path.abspath(os.path.join(
40        sys.framework_root_dir,
41        "..",
42        "xdevice",
43        "src"))
44    sys.path.insert(0, sys.xdevice_dir)
45
46    # 变量注释 sys.xdevice_extension_dir = OpenHarmony/xdevice/plugins/ohos/src
47    sys.xdevice_extension_dir = os.path.abspath(os.path.join(
48        sys.framework_root_dir,
49        "..",
50        "xdevice",
51        "plugins",
52        "ohos",
53        "src"))
54    sys.path.insert(1, sys.xdevice_extension_dir)
55
56    # 变量注释 pytest_dir = OpenHarmony/test/developer_test/aw/python
57    sys.pytest_dir = os.path.abspath(os.path.join(
58        sys.framework_root_dir,
59        "aw",
60        "python"))
61    sys.path.insert(2, sys.pytest_dir)
62
63    # 变量注释 adapter_dir = OpenHarmony/test/developer_test/adapter/aw/python
64    sys.adapter_dir = os.path.abspath(os.path.join(
65        sys.framework_root_dir,
66        "adapter"
67        "aw",
68        "python"))
69    sys.path.insert(3, sys.adapter_dir)
70
71    # 变量注释 hmh_script = OpenHarmony/test/developer_test/libs
72    sys.hmh_script = os.path.abspath(os.path.join(
73        sys.framework_root_dir,
74        "libs"))
75    sys.path.insert(4, sys.hmh_script)
76
77    # 变量注释 framework_res_dir = OpenHarmony/test/developer_test
78    sys.framework_res_dir = sys.framework_root_dir
79
80    # 变量注释 exec_dir = OpenHarmony/test/developer_test
81    sys.exec_dir = sys.framework_root_dir
82
83    from core.common import get_source_code_root_path
84    sys.source_code_root_path = get_source_code_root_path(
85        sys.framework_root_dir)
86
87    # python的参数配置 设置脚本路径 调度python的xdevice
88    from xdevice import Variables
89    Variables.exec_dir = sys.framework_root_dir
90
91
92def _iter_module_plugins(packages):
93    import importlib
94    import pkgutil
95    for package in packages:
96
97        # 获取__path__对象属性的值,若不存在,默认为“”
98        pkg_path = getattr(package, "__path__", "")
99        pkg_name = getattr(package, "__name__", "")
100        if not pkg_name or not pkg_path:
101            continue
102        _iter_modules = pkgutil.iter_modules(pkg_path, "%s%s" % (
103            pkg_name, "."))
104        for _, name, _ in _iter_modules:
105            importlib.import_module(name)
106
107
108def _load_internal_plugins():
109    try:
110        import ohos.environment
111        _iter_module_plugins([ohos.environment])
112        import ohos.testkit
113        _iter_module_plugins([ohos.testkit])
114        import ohos.managers
115        _iter_module_plugins([ohos.managers])
116        import ohos.parser
117        _iter_module_plugins([ohos.parser])
118        import ohos.drivers
119        _iter_module_plugins([ohos.drivers])
120
121        import core.driver
122        import benchmark.report.benchmark_reporter
123        _iter_module_plugins([core.driver, benchmark.report.benchmark_reporter])
124
125    except ImportError:
126        pass
127
128    try:
129        import script.report
130        _iter_module_plugins([script.report])
131    except ImportError:
132        pass
133
134
135_init_global_config()
136del _init_global_config
137
138_load_internal_plugins()
139del _load_internal_plugins
140