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 21 22 23VERSION = "0.4.6" 24# execv execution fragment 25EXECV_FRAGMENT = """ 26import sys 27import importlib 28 29sys.path.append(sys.argv.pop()) 30entry = importlib.import_module("__entry__") 31sys.exit(entry.main()) 32""" 33 34 35def find_top(): 36 cur_dir = os.getcwd() 37 while cur_dir != "/": 38 hb_internal = os.path.join(cur_dir, 'build/lite/hb_internal') 39 if os.path.exists(hb_internal): 40 return cur_dir 41 cur_dir = os.path.dirname(cur_dir) 42 raise Exception("Please call hb utilities inside source root directory") 43 44 45def search(findir, target): 46 for root, dirs, files in os.walk(findir): 47 if target in files: 48 return root 49 50 51def main(): 52 try: 53 topdir = find_top() 54 except Exception as ex: 55 return print("hb_error: Please call hb utilities inside source root directory") 56 python_base_dir = os.path.join(topdir, 'prebuilts/python') 57 if os.path.exists(python_base_dir): 58 python_dir = search(python_base_dir, 'python3') 59 python_executable = os.path.join(python_dir, 'python3') 60 lite_dir = os.path.join(topdir, 'build/lite') 61 hb_dir = search(lite_dir, '__entry__.py') 62 param_list = ["python3", "-c", EXECV_FRAGMENT] 63 for arg in sys.argv[1:]: 64 param_list.append(arg) 65 param_list.append(hb_dir) 66 os.environ['PATH'] = python_dir + ":" + os.getenv('PATH') 67 os.execv(python_executable, param_list) 68 else: 69 print("please execute build/prebuilts_download.sh") 70 71 72if __name__ == "__main__": 73 sys.exit(main()) 74