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 21import importlib 22import subprocess 23 24 25def get_python(): 26 hb_main = importlib.import_module("hb.__main__") 27 topdir = hb_main.find_top() 28 python_base_dir = os.path.join(topdir, 'prebuilts/python') 29 if os.path.exists(python_base_dir): 30 python_dir = hb_main.search(python_base_dir, 'python3') 31 return os.path.join(python_dir, 'python3') 32 else: 33 print("please execute preload_download.sh") 34 sys.exit() 35 36 37def check_output(cmd, **kwargs): 38 process = subprocess.Popen(cmd, 39 stdout=subprocess.PIPE, 40 stderr=subprocess.STDOUT, 41 universal_newlines=True, 42 **kwargs) 43 for line in iter(process.stdout.readline, ''): 44 sys.stdout.write(line) 45 sys.stdout.flush() 46 47 process.wait() 48 ret_code = process.returncode 49 50 return ret_code 51 52 53def set_root_path(path): 54 sys.path.insert(0, os.path.join(path, 'build/lite')) 55 module = importlib.import_module('hb_internal.set.set') 56 return module.set_root_path(root_path=path) 57 58 59def build(path, args_list): 60 python_executable = get_python() 61 cmd = [python_executable, 'build/lite/hb/__main__.py', 'build'] + args_list 62 return check_output(cmd, cwd=path) 63 64 65def main(): 66 root_path = os.path.dirname(os.path.abspath(__file__)) 67 ret_code = set_root_path(root_path) 68 if ret_code != 0: 69 return ret_code 70 return build(root_path, sys.argv[1:]) 71 72 73if __name__ == "__main__": 74 sys.exit(main()) 75