提交 65064a2d 编写于 作者: G gzyang

add OHOSException class in hb

上级 7f22287e
......@@ -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
......
......@@ -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:
......
......@@ -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)
......
......@@ -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]
......@@ -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],
......
......@@ -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
......@@ -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
......@@ -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', '?')
......
......@@ -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)')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册