__main__.py 2.5 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

M
mamingshuai 已提交
19
import os
W
wenjun 已提交
20
import sys
M
mamingshuai 已提交
21 22 23
sys.path.insert(0, os.path.abspath(os.path.join(__file__,
                                                os.pardir,
                                                os.pardir)))
W
wenjun 已提交
24 25 26
import argparse
import importlib

27
from hb import VERSION
M
mamingshuai 已提交
28 29
from hb.common.utils import hb_warning
from hb.common.utils import hb_error
W
wenjun 已提交
30 31


M
mamingshuai 已提交
32 33 34
def main():
    parser = argparse.ArgumentParser(usage="hb",
                                     description='OHOS build system')
35 36 37 38
    parser.add_argument('-v', '--version',
                        action='version',
                        version=f'[OHOS INFO] hb version {VERSION}')

W
wenjun 已提交
39 40 41
    subparsers = parser.add_subparsers()
    parser_list = []
    parser_list.append({
M
mamingshuai 已提交
42
        'name': 'build',
W
wenjun 已提交
43 44 45 46
        'help': 'Build source code'
    })

    parser_list.append({
M
mamingshuai 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        'name': 'set',
        'help': 'OHOS build settings'
    })

    parser_list.append({
        'name': 'env',
        'help': 'Show OHOS build env'
    })

    parser_list.append({
        'name': 'clean',
        'help': 'Clean output'
    })

    parser_list.append({
        'name': 'deps',
        'help': 'OHOS components deps'
W
wenjun 已提交
64 65 66 67 68 69
    })

    for each in parser_list:
        module_parser = subparsers.add_parser(name=each.get('name'),
                                              help=each.get('help'))
        module = importlib.import_module('.{}'.format(each.get('name')),
M
mamingshuai 已提交
70
                                         'hb.{}'.format(each.get('name')))
W
wenjun 已提交
71
        module.add_options(module_parser)
M
mamingshuai 已提交
72 73
        module_parser.set_defaults(parser=module_parser,
                                   command=module.exec_command)
W
wenjun 已提交
74 75 76 77 78 79

    args = parser.parse_args()

    try:
        status = args.command(args)
    except KeyboardInterrupt:
M
mamingshuai 已提交
80
        hb_warning('interrupted')
W
wenjun 已提交
81
        status = -1
M
mamingshuai 已提交
82 83
    except Exception as exception:
        hb_error(exception.args[0])
W
wenjun 已提交
84 85 86 87 88 89 90
        status = -1

    return status


if __name__ == "__main__":
    sys.exit(main())