diff --git a/hb/__main__.py b/hb/__main__.py index 3fba68917d301717e9e802d816b4e4d4a440e901..f46fc58e873f143766a349222e00cada0597b6ed 100755 --- a/hb/__main__.py +++ b/hb/__main__.py @@ -23,10 +23,12 @@ sys.path.insert(0, os.path.abspath(os.path.join(__file__, os.pardir))) import argparse import importlib +import traceback from hb import VERSION from hb.common.utils import hb_warning from hb.common.utils import hb_error +from hb.common.utils import OHOSException def main(): @@ -77,11 +79,15 @@ def main(): try: status = args.command(args) except KeyboardInterrupt: - hb_warning('interrupted') + hb_warning('User Abort') status = -1 - except Exception as exception: + except OHOSException as exception: hb_error(exception.args[0]) status = -1 + except Exception as exception: + hb_error(traceback.format_exc()) + hb_error(f'Unhandled error: {exception}') + status = -1 return status diff --git a/hb/build/build_process.py b/hb/build/build_process.py index e7b70f8a1ffb3e298683d55fe814b5a03a774839..095b2a5cda6f02baa6f84121e3098d8bf6d0e2b1 100755 --- a/hb/build/build_process.py +++ b/hb/build/build_process.py @@ -25,6 +25,7 @@ from hb.common.utils import makedirs from hb.common.utils import remove_path from hb.common.utils import hb_info from hb.common.utils import hb_warning +from hb.common.utils import OHOSException from hb.common.config import Config from hb.cts.cts import CTS from hb.common.device import Device @@ -69,7 +70,7 @@ class Build(): self._target) return - raise Exception('Component {} not found'.format(component)) + raise OHOSException(f'Component {component} not found') @property def compiler(self): @@ -95,7 +96,7 @@ class Build(): if len(test_args) > 1: self.register_args('ohos_xts_test_args', self._test) else: - raise Exception('Error: wrong input of test') + raise OHOSException('Error: wrong input of test') def register_args(self, args_name, args_value, quota=True): if quota: diff --git a/hb/common/config.py b/hb/common/config.py index fe41496c989d7cedef4d13acf17415b339d2a1fd..8fd1f26a13ca488c14413667a8242edf35c5359a 100755 --- a/hb/common/config.py +++ b/hb/common/config.py @@ -24,6 +24,7 @@ from hb import CONFIG_STRUCT from hb.common.utils import read_json_file from hb.common.utils import dump_json_file from hb.common.utils import Singleton +from hb.common.utils import OHOSException class Config(metaclass=Singleton): @@ -43,8 +44,8 @@ class Config(metaclass=Singleton): @property def root_path(self): if self._root_path is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._root_path @@ -52,7 +53,7 @@ class Config(metaclass=Singleton): def root_path(self, value): self._root_path = os.path.abspath(value) if not os.path.isdir(self._root_path): - raise Exception('{} is not a valid path'.format(self._root_path)) + raise OHOSException(f'{self._root_path} is not a valid path') config_path = os.path.join(self._root_path, 'ohos_config.json') if not os.path.isfile(config_path): @@ -62,8 +63,8 @@ class Config(metaclass=Singleton): @property def board(self): if self._board is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._board @board.setter @@ -74,8 +75,8 @@ class Config(metaclass=Singleton): @property def kernel(self): if self._kernel is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._kernel @kernel.setter @@ -86,8 +87,8 @@ class Config(metaclass=Singleton): @property def product(self): if self._product is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._product @product.setter @@ -98,8 +99,8 @@ class Config(metaclass=Singleton): @property def product_path(self): if self._product_path is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._product_path @product_path.setter @@ -114,8 +115,8 @@ class Config(metaclass=Singleton): @property def device_path(self): if self._device_path is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') return self._device_path @device_path.setter @@ -131,7 +132,7 @@ class Config(metaclass=Singleton): def build_path(self): _build_path = os.path.join(self.root_path, 'build', 'lite') if not os.path.isdir(_build_path): - raise Exception(f'Invalid build path: {_build_path}') + raise OHOSException(f'Invalid build path: {_build_path}') return _build_path @property @@ -150,7 +151,7 @@ class Config(metaclass=Singleton): def vendor_path(self): _vendor_path = os.path.join(self.root_path, 'vendor') if not os.path.isdir(_vendor_path): - raise Exception(f'Invalid vendor path: {_vendor_path}') + raise OHOSException(f'Invalid vendor path: {_vendor_path}') return _vendor_path @property @@ -169,7 +170,7 @@ class Config(metaclass=Singleton): 'win-x86', 'bin') - raise Exception(f'unidentified platform: {platform_name}') + raise OHOSException(f'unidentified platform: {platform_name}') @property def gn_path(self): @@ -181,7 +182,7 @@ class Config(metaclass=Singleton): if env_gn_path is not None: return env_gn_path - raise Exception('gn not found, install it please') + raise OHOSException('gn not found, install it please') @property def ninja_path(self): @@ -193,7 +194,7 @@ class Config(metaclass=Singleton): if env_ninja_path is not None: return env_ninja_path - raise Exception('ninja not found, install it please') + raise OHOSException('ninja not found, install it please') @property def clang_path(self): @@ -214,7 +215,7 @@ class Config(metaclass=Singleton): if os.path.basename(env_clang_path) == 'llvm': return env_clang_path - raise Exception('clang not found, install it please') + raise OHOSException('clang not found, install it please') def config_create(self, config_path): dump_json_file(config_path, CONFIG_STRUCT) diff --git a/hb/common/device.py b/hb/common/device.py index c99cec977c3caf686b2acf41482fc1c6491bd16d..8213fff1b661581f3d4099c1954b85eb49399712 100755 --- a/hb/common/device.py +++ b/hb/common/device.py @@ -19,6 +19,7 @@ import os import re from hb.cts.menuconfig import Menuconfig +from hb.common.utils import OHOSException class Device(): @@ -44,7 +45,7 @@ class Device(): kernel_path if not len(kernel_path_dict): - raise Exception('no valid kernel found') + raise OHOSException('no valid kernel found') choices = [{'name': kernel} for kernel in kernel_path_dict.keys()] @@ -63,9 +64,8 @@ class Device(): kernel_version): return kernel_path - raise Exception('cannot find {}_{} in {}'.format(kernel_type, - kernel_version, - board_path)) + raise OHOSException(f'cannot find {kernel_type}_{kernel_version} ' + f'in {board_path}') @staticmethod def get_kernel_config(board_path): @@ -99,8 +99,8 @@ class Device(): kernel_list = re.findall(kernel_pattern, data) version_list = re.findall(version_pattern, data) if not len(kernel_list) or not len(version_list): - raise Exception('kernel_type or kernel_version ' - 'not found in {}'.format(config)) + raise OHOSException(f'kernel_type or kernel_version ' + f'not found in {config}') return kernel_list[0], version_list[0] @@ -108,7 +108,7 @@ class Device(): def check_path(path): if os.path.isdir(path) or os.path.isfile(path): return - raise Exception('invalid path: {}'.format(path)) + raise OHOSException(f'invalid path: {path}') @staticmethod def get_compiler(config_path): @@ -120,7 +120,6 @@ class Device(): data = config_file.read() compiler_list = re.findall(compiler_pattern, data) if not len(compiler_list): - raise Exception('board_toolchain_type is None' - ' in {}'.format(config)) + raise OHOSException(f'board_toolchain_type is None in {config}') return compiler_list[0] diff --git a/hb/common/product.py b/hb/common/product.py index 489c68cea45a1aea36f10067ab91ed19791fde7c..44b112a6d24913d787cccd8df0749164587ad3f8 100755 --- a/hb/common/product.py +++ b/hb/common/product.py @@ -19,6 +19,7 @@ import os from collections import defaultdict from hb.common.utils import read_json_file +from hb.common.utils import OHOSException from hb.common.config import Config from hb.cts.menuconfig import Menuconfig from hb.cts.common import Separator @@ -54,7 +55,7 @@ class Product(): @staticmethod def get_features(product_json): if not os.path.isfile(product_json): - raise Exception('{} not found'.format(product_json)) + raise OHOSException(f'{product_json} not found') features_list = [] subsystems = read_json_file(product_json).get('subsystems', []) @@ -69,7 +70,7 @@ class Product(): @staticmethod def get_components(product_json, subsystems): if not os.path.isfile(product_json): - raise Exception('{} not found'.format(product_json)) + raise OHOSException(f'{product_json} not found') components_dict = defaultdict(list) product_data = read_json_file(product_json) @@ -88,8 +89,7 @@ class Product(): if cur_company == company and cur_product == product_name: return product_path - raise Exception('product {}@{} not found'. - format(product_name, company)) + raise OHOSException(f'product {product_name}@{company} not found') @staticmethod def product_menuconfig(): @@ -104,7 +104,7 @@ class Product(): product_path_dict['{}@{}'.format(product, company)] = product_path if not len(product_path_dict): - raise Exception('no valid product found') + raise OHOSException('no valid product found') choices = [product if isinstance(product, Separator) else {'name': product.split('@')[0], diff --git a/hb/common/utils.py b/hb/common/utils.py index fbf191b519b145f3bffcfabc685406b0ff88e075..1775fbbf0abf93cd871be94b23bf9c7227f38cc8 100755 --- a/hb/common/utils.py +++ b/hb/common/utils.py @@ -46,14 +46,14 @@ def remove_path(path): # Read json file data def read_json_file(input_file): if not os.path.isfile(input_file): - raise OSError('{} not found'.format(input_file)) + raise OHOSException(f'{input_file} not found') with open(input_file, 'rb') as input_f: try: data = json.load(input_f) return data except json.JSONDecodeError: - raise Exception('{} parsing error!'.format(input_file)) + raise OHOSException(f'{input_file} parsing error!') def dump_json_file(dump_file, json_data): @@ -66,7 +66,7 @@ def dump_json_file(dump_file, json_data): def read_yaml_file(input_file): if not os.path.isfile(input_file): - raise OSError('{} not found'.format(input_file)) + raise OHOSException(f'{input_file} not found') with open(input_file, 'rt', encoding='utf-8') as yaml_file: try: @@ -74,15 +74,15 @@ def read_yaml_file(input_file): except yaml.YAMLError as exc: if hasattr(exc, 'problem_mark'): mark = exc.problem_mark - raise Exception(f'{input_file} load failed, error position:' - f' {mark.line + 1}:{mark.column + 1}') + raise OHOSException(f'{input_file} load failed, error line:' + f' {mark.line + 1}:{mark.column + 1}') def get_input(msg): try: user_input = input except NameError: - raise Exception('python2.x not supported') + raise OHOSException('python2.x not supported') return user_input(msg) @@ -123,7 +123,9 @@ def exec_command(cmd, log_path='out/build.log', **kwargs): hb_error('you can check build log in {}'.format(log_path)) if isinstance(cmd, list): cmd = ' '.join(cmd) - raise Exception("{} failed, return code is {}".format(cmd, ret_code)) + raise OHOSException(f'command: "{cmd}" failed\n' + f'return code: {ret_code}\n' + f'execution path: {os.getcwd()}') def get_failed_log(log_path): @@ -152,7 +154,9 @@ def check_output(cmd, **kwargs): ret = called_exception.output if isinstance(cmd, list): cmd = ' '.join(cmd) - raise Exception("{} failed, failed log is {}".format(cmd, ret)) + raise OHOSException(f'command: "{cmd}" failed\n' + f'return code: {ret}\n' + f'execution path: {os.getcwd()}') return ret @@ -162,12 +166,12 @@ def makedirs(path, exist_ok=True, with_rm=False): os.makedirs(path) except OSError: if not os.path.isdir(path): - raise Exception("{} makedirs failed".format(path)) + raise OHOSException(f"{path} makedirs failed") if with_rm: remove_path(path) return os.makedirs(path) if not exist_ok: - raise Exception("{} exists, makedirs failed".format(path)) + raise OHOSException(f"{path} exists, makedirs failed") def get_project_path(json_path): @@ -178,7 +182,8 @@ def get_project_path(json_path): def args_factory(args_dict): if not len(args_dict): - raise Exception('at least one k_v param is required in args_factory') + raise OHOSException('at least one k_v param is ' + 'required in args_factory') args_cls = namedtuple('Args', [key for key in args_dict.keys()]) args = args_cls(**args_dict) @@ -220,3 +225,7 @@ class Singleton(type): cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] + + +class OHOSException(Exception): + pass diff --git a/hb/cts/cts.py b/hb/cts/cts.py index c0fcc9feffd3a9e6a947f9faee8048f6a283fa3e..972df700d2c9011640a49c645a8027d56e9203e3 100755 --- a/hb/cts/cts.py +++ b/hb/cts/cts.py @@ -33,6 +33,7 @@ from hb.cts.common import check_path from hb.common.utils import get_project_path from hb.common.utils import hb_info from hb.common.utils import hb_warning +from hb.common.utils import OHOSException class CTS(): @@ -51,8 +52,8 @@ class CTS(): def _set_path(self): self._code_path = get_project_path(CONFIG_JSON) if self._code_path is None: - raise Exception('Please run command "hb set" to ' - 'init OHOS development environment') + raise OHOSException('Please run command "hb set" to ' + 'init OHOS development environment') self._components_path = os.path.join(self._code_path, 'build', @@ -113,7 +114,7 @@ class CTS(): def init(self, board=None): if board is None: if self.board is None: - raise Exception('no board selected') + raise OHOSException('no board selected') else: self.board = board @@ -485,4 +486,4 @@ class Component(): 'components': components_list } - return self.deps_real_dict \ No newline at end of file + return self.deps_real_dict diff --git a/hb/cts/list.py b/hb/cts/list.py index 32bc79dcee0c5e289f3425f0156ed29b65fe264d..b063913138504ffcd153ff99d4e34047f4325fa8 100755 --- a/hb/cts/list.py +++ b/hb/cts/list.py @@ -32,6 +32,7 @@ from prompt_toolkit.token import Token from hb.cts.common import Separator from hb.cts.common import if_mousedown from hb.cts.common import get_style +from hb.common.utils import OHOSException class InquirerControl(TokenListControl): @@ -113,7 +114,7 @@ class InquirerControl(TokenListControl): def question(message, **kwargs): if 'choices' not in kwargs: - raise Exception("You must choose one platform.") + raise OHOSException("You must choose one platform.") choices = kwargs.pop('choices', None) qmark = kwargs.pop('qmark', '?') diff --git a/hb/deps/deps.py b/hb/deps/deps.py index 7689df49634b99e0c54df410fc78dea43a782bbf..816e12b4a23edce6af66d8c70542a13910085b8f 100755 --- a/hb/deps/deps.py +++ b/hb/deps/deps.py @@ -19,6 +19,7 @@ from hb.deps.check_deps import check_deps from hb.deps.gen_deps import gen_deps +from hb.common.utils import OHOSException def add_options(parser): @@ -42,4 +43,4 @@ def exec_command(args): elif args.deps_type[0] == 'gen': return gen_deps(args.subsystems, args.products) - raise Exception('please select the operation for deps (check or gen)') + raise OHOSException('please select the operation for deps (check or gen)')