• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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        cur_dir = os.path.dirname(cur_dir)
36    raise Exception("Please call hb utilities inside source root directory")
37
38
39def get_hb_commands(config_file):
40    if not os.path.exists(config_file):
41        raise Exception('Error: {} not exist, couldnot get hb command set'.format(config_file))
42    with open(config_file, 'r') as file:
43        config = json.load(file)
44        return config
45
46
47def main():
48    try:
49        topdir = find_top()
50    except Exception as ex:
51        return print("hb_error: Please call hb utilities inside source root directory")
52    sys.path.insert(0, os.path.join(topdir, 'build/lite'))
53    parser = argparse.ArgumentParser(description='OHOS Build System '
54                                     f'version {VERSION}')
55    parser.add_argument('-v',
56                        '--version',
57                        action='version',
58                        version=f'[OHOS INFO] hb version {VERSION}')
59
60    subparsers = parser.add_subparsers()
61    parser_list = []
62
63    command_set = get_hb_commands(os.path.join(topdir, 'build/lite/hb_internal/hb_command_set.json'))
64    for key, val in command_set.items():
65        parser_list.append({'name': key, 'help': val})
66
67    for each in parser_list:
68        module_parser = subparsers.add_parser(name=each.get('name'),
69                                              help=each.get('help'))
70        module = importlib.import_module('hb_internal.{0}.{0}'.format(
71            each.get('name')))
72        module.add_options(module_parser)
73        module_parser.set_defaults(parser=module_parser,
74                                   command=module.exec_command)
75
76    args = parser.parse_known_args()
77
78    module = importlib.import_module('hb_internal.common.utils')
79    hb_error = getattr(module, 'hb_error')
80    hb_warning = getattr(module, 'hb_warning')
81    ohos_exception = getattr(module, 'OHOSException')
82    try:
83        if args[0].parser.prog == 'hb set' and 'root_path' in vars(args[0]):
84            # Root_path is topdir.
85            args[0].root_path = topdir
86        if "tool" in args[0].parser.prog:
87            status = args[0].command(args)
88        else:
89            status = args[0].command(args[0])
90    except KeyboardInterrupt:
91        hb_warning('User Abort')
92        status = -1
93    except ohos_exception as exception:
94        hb_error(exception.args[0])
95        status = -1
96    except Exception as exception:
97        if not hasattr(args[0], 'command'):
98            parser.print_help()
99        else:
100            hb_error(traceback.format_exc())
101            hb_error(f'Unhandled error: {exception}')
102        status = -1
103
104    return status
105
106
107if __name__ == "__main__":
108    sys.exit(main())
109