15f9996aaSopenharmony_ci#!/usr/bin/env python3 25f9996aaSopenharmony_ci# -*- coding: utf-8 -*- 35f9996aaSopenharmony_ci 45f9996aaSopenharmony_ci# 55f9996aaSopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd. 65f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 75f9996aaSopenharmony_ci# you may not use this file except in compliance with the License. 85f9996aaSopenharmony_ci# You may obtain a copy of the License at 95f9996aaSopenharmony_ci# 105f9996aaSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 115f9996aaSopenharmony_ci# 125f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 135f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 145f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155f9996aaSopenharmony_ci# See the License for the specific language governing permissions and 165f9996aaSopenharmony_ci# limitations under the License. 175f9996aaSopenharmony_ci# 185f9996aaSopenharmony_ci 195f9996aaSopenharmony_ciimport os 205f9996aaSopenharmony_ciimport sys 215f9996aaSopenharmony_ciimport importlib 225f9996aaSopenharmony_ciimport subprocess 235f9996aaSopenharmony_ci 245f9996aaSopenharmony_ci 255f9996aaSopenharmony_cidef search(findir: str, target: str) -> str or bool: 265f9996aaSopenharmony_ci for root, _, files in os.walk(findir): 275f9996aaSopenharmony_ci if target in files: 285f9996aaSopenharmony_ci return root 295f9996aaSopenharmony_ci return False 305f9996aaSopenharmony_ci 315f9996aaSopenharmony_ci 325f9996aaSopenharmony_cidef find_top() -> str: 335f9996aaSopenharmony_ci cur_dir = os.getcwd() 345f9996aaSopenharmony_ci while cur_dir != "/": 355f9996aaSopenharmony_ci build_config_file = os.path.join( 365f9996aaSopenharmony_ci cur_dir, 'build/config/BUILDCONFIG.gn') 375f9996aaSopenharmony_ci if os.path.exists(build_config_file): 385f9996aaSopenharmony_ci return cur_dir 395f9996aaSopenharmony_ci cur_dir = os.path.dirname(cur_dir) 405f9996aaSopenharmony_ci 415f9996aaSopenharmony_ci 425f9996aaSopenharmony_cidef get_python(python_relative_dir: str) -> str: 435f9996aaSopenharmony_ci topdir = find_top() 445f9996aaSopenharmony_ci if python_relative_dir is None: 455f9996aaSopenharmony_ci python_relative_dir = 'prebuilts/python' 465f9996aaSopenharmony_ci python_base_dir = os.path.join(topdir, python_relative_dir) 475f9996aaSopenharmony_ci 485f9996aaSopenharmony_ci if os.path.exists(python_base_dir): 495f9996aaSopenharmony_ci python_dir = search(python_base_dir, 'python3') 505f9996aaSopenharmony_ci return os.path.join(python_dir, 'python3') 515f9996aaSopenharmony_ci else: 525f9996aaSopenharmony_ci print("please execute build/prebuilts_download.sh.", 535f9996aaSopenharmony_ci "if you used '--python-dir', check whether the input path is valid.") 545f9996aaSopenharmony_ci sys.exit() 555f9996aaSopenharmony_ci 565f9996aaSopenharmony_ci 575f9996aaSopenharmony_cidef check_output(cmd: str, **kwargs) -> str: 585f9996aaSopenharmony_ci process = subprocess.Popen(cmd, 595f9996aaSopenharmony_ci stdout=subprocess.PIPE, 605f9996aaSopenharmony_ci stderr=subprocess.STDOUT, 615f9996aaSopenharmony_ci universal_newlines=True, 625f9996aaSopenharmony_ci **kwargs) 635f9996aaSopenharmony_ci for line in iter(process.stdout.readline, ''): 645f9996aaSopenharmony_ci sys.stdout.write(line) 655f9996aaSopenharmony_ci sys.stdout.flush() 665f9996aaSopenharmony_ci 675f9996aaSopenharmony_ci process.wait() 685f9996aaSopenharmony_ci ret_code = process.returncode 695f9996aaSopenharmony_ci 705f9996aaSopenharmony_ci return ret_code 715f9996aaSopenharmony_ci 725f9996aaSopenharmony_ci 735f9996aaSopenharmony_cidef build(path: str, args_list: list) -> str: 745f9996aaSopenharmony_ci python_dir = None 755f9996aaSopenharmony_ci if "--python-dir" in args_list: 765f9996aaSopenharmony_ci index = args_list.index("--python-dir") 775f9996aaSopenharmony_ci if index < len(args_list) - 1: 785f9996aaSopenharmony_ci python_dir = args_list[index + 1] 795f9996aaSopenharmony_ci del args_list[index: index + 2] 805f9996aaSopenharmony_ci else: 815f9996aaSopenharmony_ci print("-python-dir parmeter missing value.") 825f9996aaSopenharmony_ci sys.exit() 835f9996aaSopenharmony_ci python_executable = get_python(python_dir) 845f9996aaSopenharmony_ci cmd = [python_executable, 'build/hb/main.py', 'build'] + args_list 855f9996aaSopenharmony_ci return check_output(cmd, cwd=path) 865f9996aaSopenharmony_ci 875f9996aaSopenharmony_ci 885f9996aaSopenharmony_cidef main(): 895f9996aaSopenharmony_ci root_path = find_top() 905f9996aaSopenharmony_ci return build(root_path, sys.argv[1:]) 915f9996aaSopenharmony_ci 925f9996aaSopenharmony_ci 935f9996aaSopenharmony_ciif __name__ == "__main__": 945f9996aaSopenharmony_ci sys.exit(main()) 95