1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 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
20import sys
21import importlib
22import importlib.util
23
24
25def is_in_ohos_dir():
26    cur_dir = os.getcwd()
27    while cur_dir != "/":
28        global_var = os.path.join(
29            cur_dir, 'build', 'hb', 'resources', 'global_var.py')
30        if os.path.exists(global_var):
31            return True, cur_dir
32        cur_dir = os.path.dirname(cur_dir)
33    return False, ''
34
35
36def main():
37    in_ohos_dir, ohos_root_path = is_in_ohos_dir()
38    if in_ohos_dir :
39        entry_path = os.path.join(os.path.abspath(os.path.relpath(
40            ohos_root_path, os.path.curdir)), 'build', 'hb', 'main.py')
41        spec = importlib.util.spec_from_file_location('main', entry_path)
42        api = importlib.util.module_from_spec(spec)
43        spec.loader.exec_module(api)
44        main_api = api.Main()
45        main_api.main()
46    else:
47        raise Exception(
48            "[OHOS_ERROR]: Please call hb utilities inside ohos source directory")
49
50
51if __name__ == "__main__":
52    sys.exit(main())
53