1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2020 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 json 22import argparse 23import importlib 24import traceback 25 26VERSION = "0.4.6" 27 28 29def find_top(): 30 cur_dir = os.getcwd() 31 while cur_dir != "/": 32 hb_internal = os.path.join(cur_dir, 'build/lite/hb_internal') 33 if os.path.exists(hb_internal): 34 return cur_dir 35 36 cur_dir = os.path.dirname(cur_dir) 37 raise Exception("Please call hb utilities inside source root directory") 38 39 40def get_hb_commands(config_file): 41 if not os.path.exists(config_file): 42 raise Exception('Error: {} not exist, couldnot get hb command set'.format(config_file)) 43 with open(config_file, 'r') as file: 44 config = json.load(file) 45 return config 46 47 48def main(): 49 topdir = find_top() 50 sys.path.insert(0, os.path.join(topdir, 'build/lite')) 51 parser = argparse.ArgumentParser(description='OHOS Build System ' 52 f'version {VERSION}') 53 parser.add_argument('-v', 54 '--version', 55 action='version', 56 version=f'[OHOS INFO] hb version {VERSION}') 57 58 subparsers = parser.add_subparsers() 59 parser_list = [] 60 61 command_set = get_hb_commands(os.path.join(topdir, 'build/lite/hb_internal/hb_command_set.json')) 62 for key, val in command_set.items(): 63 parser_list.append({'name': key, 'help': val}) 64 65 for each in parser_list: 66 module_parser = subparsers.add_parser(name=each.get('name'), 67 help=each.get('help')) 68 module = importlib.import_module('hb_internal.{0}.{0}'.format( 69 each.get('name'))) 70 module.add_options(module_parser) 71 module_parser.set_defaults(parser=module_parser, 72 command=module.exec_command) 73 74 args = parser.parse_args() 75 76 module = importlib.import_module('hb_internal.common.utils') 77 hb_error = getattr(module, 'hb_error') 78 hb_warning = getattr(module, 'hb_warning') 79 OHOSException = getattr(module, 'OHOSException') 80 try: 81 if args.parser.prog == 'hb set' and 'root_path' in vars(args): 82 # Root_path is topdir. 83 args.root_path = topdir 84 status = args.command(args) 85 except KeyboardInterrupt: 86 hb_warning('User Abort') 87 status = -1 88 except OHOSException as exception: 89 hb_error(exception.args[0]) 90 status = -1 91 except Exception as exception: 92 if not hasattr(args, 'command'): 93 parser.print_help() 94 else: 95 hb_error(traceback.format_exc()) 96 hb_error(f'Unhandled error: {exception}') 97 status = -1 98 99 return status 100 101 102if __name__ == "__main__": 103 sys.exit(main()) 104