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 search(findir: str, target: str) -> str or bool:
26    for root, _, files in os.walk(findir):
27        if target in files:
28            return root
29    return False
30
31
32def find_top() -> str:
33    cur_dir = os.getcwd()
34    while cur_dir != "/":
35        build_config_file = os.path.join(
36            cur_dir, 'build/config/BUILDCONFIG.gn')
37        if os.path.exists(build_config_file):
38            return cur_dir
39        cur_dir = os.path.dirname(cur_dir)
40
41
42def get_python(python_relative_dir: str) -> str:
43    topdir = find_top()
44    if python_relative_dir is None:
45        python_relative_dir = 'prebuilts/python'
46    python_base_dir = os.path.join(topdir, python_relative_dir)
47
48    if os.path.exists(python_base_dir):
49        python_dir = search(python_base_dir, 'python3')
50        return os.path.join(python_dir, 'python3')
51    else:
52        print("please execute build/prebuilts_download.sh.",
53            "if you used '--python-dir', check whether the input path is valid.")
54        sys.exit()
55
56
57def check_output(cmd: str, **kwargs) -> str:
58    process = subprocess.Popen(cmd,
59                               stdout=subprocess.PIPE,
60                               stderr=subprocess.STDOUT,
61                               universal_newlines=True,
62                               **kwargs)
63    for line in iter(process.stdout.readline, ''):
64        sys.stdout.write(line)
65        sys.stdout.flush()
66
67    process.wait()
68    ret_code = process.returncode
69
70    return ret_code
71
72
73def build(path: str, args_list: list) -> str:
74    python_dir = None
75    if "--python-dir" in args_list:
76        index = args_list.index("--python-dir")
77        if index < len(args_list) - 1:
78            python_dir = args_list[index + 1]
79            del args_list[index: index + 2]
80        else:
81            print("-python-dir parmeter missing value.")
82            sys.exit()
83    python_executable = get_python(python_dir)
84    cmd = [python_executable, 'build/hb/main.py', 'build'] + args_list
85    return check_output(cmd, cwd=path)
86
87
88def main():
89    root_path = find_top()
90    return build(root_path, sys.argv[1:])
91
92
93if __name__ == "__main__":
94    sys.exit(main())
95