def __init__(self, root, vendor): self.vendor = vendor self.file_path = hdf_utils.get_vendor_mk_path(root, vendor) if not os.path.exists(self.file_path): raise HdfToolException('file: %s not exist' % self.file_path, CommandErrorCode.TARGET_NOT_EXIST) self.contents = hdf_utils.read_file(self.file_path)
def run(self): self.action_type = self._get_action_type() if self.action_type in self.handlers: return self.handlers[self.action_type]() else: raise HdfToolException('unknown action_type: "%s" for "%s" cmd' % (self.action_type, self.cmd), CommandErrorCode.INTERFACE_ERROR)
def check_arg_raise_if_not_exist(self, arg): try: value = getattr(self.args, arg) if not value: raise AttributeError() except AttributeError: raise HdfToolException('argument "--%s" is required for "%s" cmd' % (arg, self.cmd), CommandErrorCode.INTERFACE_ERROR)
def _add_driver_handler(self): self.check_arg_raise_if_not_exist("vendor_name") self.check_arg_raise_if_not_exist("module_name") self.check_arg_raise_if_not_exist("driver_name") self.check_arg_raise_if_not_exist("board_name") root, vendor, module, driver, board = self.get_args() drv_converter = hdf_utils.WordsConverter(self.args.driver_name) drv_config = HdfDriverConfigFile(root, board, module, driver) module_k = \ hdf_utils.get_module_kconfig_path(root, vendor, module) if not os.path.exists(module_k): raise HdfToolException('module "%s Kconfig" not exist' % module, CommandErrorCode.TARGET_NOT_EXIST) module_mk = \ hdf_utils.get_module_mk_path(root, vendor, module) if not os.path.exists(module_mk): raise HdfToolException('module "%s Makefile" not exist' % module, CommandErrorCode.TARGET_NOT_EXIST) drv_src_dir = hdf_utils.get_drv_src_dir(root, vendor, module, driver) if os.path.exists(drv_src_dir): raise HdfToolException('driver "%s" already exist' % driver, CommandErrorCode.TARGET_ALREADY_EXIST) os.makedirs(drv_src_dir) drv_include_dir = \ hdf_utils.get_drv_include_dir(root, vendor, module, driver) if not os.path.exists(drv_include_dir): os.makedirs(drv_include_dir) data_model = { 'driver_lower_case': drv_converter.lower_case(), 'driver_upper_camel_case': drv_converter.upper_camel_case(), 'driver_lower_camel_case': drv_converter.lower_camel_case(), 'driver_upper_case': drv_converter.upper_case() } self._file_gen_lite('hdf_driver.c.template', drv_src_dir, '%s_driver.c' % driver, data_model) self._file_gen_lite('hdf_driver.h.template', drv_include_dir, '%s_driver.h' % driver, data_model) k_path = hdf_utils.get_module_kconfig_path(root, vendor, module) module_config = HdfModuleKconfigFile(root, module, k_path) config = module_config.add_driver(driver) module_mk = HdfModuleMkFile(root, vendor, module) module_mk.add_driver(driver) drv_config.create_driver() return json.dumps(config)
def add_host(self, module): manager_range = self._find_range(self.hdf_manager_pattern) if not manager_range: raise HdfToolException( 'file: %s format wrong, no hdf_manager node' ' found' % self.file_path, CommandErrorCode.FILE_FORMAT_WRONG) host_section = self._create_host(module) last_brace = hdf_utils.SectionRange(manager_range.end_pos, manager_range.end_pos) hdf_utils.add_after_and_save(self.contents, self.file_path, last_brace, host_section)
def __init__(self, words_str): self.words_str = words_str if len(self.words_str) == 0: raise HdfToolException('empty words') self.words_list = self.split() self.upper = '_'.join([w.upper() for w in self.words_list]) self.lower = '_'.join([w.lower() for w in self.words_list]) self.upper_camel = ''.join([w.capitalize() for w in self.words_list]) if len(self.upper_camel) == 1: self.lower_camel = self.upper_camel[0].lower() else: self.lower_camel = '%s%s' % (self.upper_camel[0].lower(), self.upper_camel[1:])
def _set_vendor_new_name_handler(self): self.check_arg_raise_if_not_exist("vendor_name") self.check_arg_raise_if_not_exist("new_vendor_name") root_dir = self.args.root_dir old_name = self.args.vendor_name new_name = self.args.new_vendor_name src_vendor = hdf_utils.get_vendor_dir(root_dir, old_name) if not os.path.exists(src_vendor): raise HdfToolException('vendor: "%s" not exist' % old_name, CommandErrorCode.TARGET_NOT_EXIST) dst_vendor = hdf_utils.get_vendor_dir(root_dir, new_name) if os.path.exists(dst_vendor): raise HdfToolException('vendor: "%s" already exist' % new_name, CommandErrorCode.TARGET_ALREADY_EXIST) need_update_current_vendor = False hdf_lite = HdfLiteMkFile(root_dir) if hdf_lite.get_current_vendor() == old_name: need_update_current_vendor = True os.rename(src_vendor, dst_vendor) if need_update_current_vendor: hdf_lite.set_vendor(new_name) HdfVendorKconfigFile(root_dir, new_name).rename_vendor() HdfVendorMkFile(root_dir, new_name).rename_vendor()
def _add_module_handler(self): self.check_arg_raise_if_not_exist("vendor_name") self.check_arg_raise_if_not_exist("module_name") root, vendor, module, _, _ = self.get_args() converter = hdf_utils.WordsConverter(self.args.module_name) hdf = hdf_utils.get_vendor_hdf_dir(root, vendor) if not os.path.exists(hdf): raise HdfToolException('vendor "%s" not exist' % vendor, CommandErrorCode.TARGET_NOT_EXIST) drv_root_dir = hdf_utils.get_drv_root_dir(root, vendor, module) if os.path.exists(drv_root_dir): raise HdfToolException('module "%s" already exist' % module, CommandErrorCode.TARGET_ALREADY_EXIST) os.makedirs(drv_root_dir) hdi_dir = os.path.join(hdf, module, 'hdi') if not os.path.exists(hdi_dir): os.makedirs(hdi_dir) data_model = { "module_upper_case": converter.upper_case(), "module_lower_case": converter.lower_case() } self._file_gen_lite('hdf_module_kconfig.template', drv_root_dir, 'Kconfig', data_model) self._file_gen_lite('hdf_module_mk.template', drv_root_dir, 'Makefile', data_model) vendor_k = HdfVendorKconfigFile(root, vendor) vendor_k.add_module([module, 'driver', 'Kconfig']) vendor_mk = HdfVendorMkFile(root, vendor) vendor_mk.add_module(module) config_item = { 'name': module, 'config_item': 'DRIVERS_HDF_%s' % converter.upper_case(), 'depends_on_item': 'DRIVERS_HDF', 'enabled': False } return json.dumps(config_item)
def add_driver(self, driver): drv_section = self._create_driver_item(driver) old_range = hdf_utils.find_section(self.contents, drv_section) if old_range: hdf_utils.replace_and_save(self.contents, self.file_path, old_range, drv_section) return tail_pattern = r'include\s+\$\(HDF_DRIVER\)' replacement = '%sinclude $(HDF_DRIVER)' % drv_section.to_str() new_content, count = re.subn(tail_pattern, replacement, self.contents) if count != 1: raise HdfToolException( 'Makefile: %s has more than one include' ' $(HDF_DRIVER)' % self.file_path, CommandErrorCode.FILE_FORMAT_WRONG) hdf_utils.write_file(self.file_path, new_content)
def _get_driver_file_handler(self): self.check_arg_raise_if_not_exist("root_dir") self.check_arg_raise_if_not_exist("vendor_name") self.check_arg_raise_if_not_exist("module_name") self.check_arg_raise_if_not_exist("driver_name") root = os.path.realpath(self.args.root_dir) _, vendor, module, driver, _ = self.get_args() drv_dir = hdf_utils.get_drv_dir(root, vendor, module, driver) if not os.path.exists(drv_dir): raise HdfToolException('driver directory: %s not exist' % drv_dir, CommandErrorCode.TARGET_NOT_EXIST) for root_path, dirs, files in os.walk(drv_dir): for file in files: if file.endswith('.c'): return os.path.realpath(os.path.join(root_path, file)) return ''
def _add_vendor_handler(self): self.check_arg_raise_if_not_exist("vendor_name") root, vendor, _, _, board = self.get_args() target_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) if os.path.exists(target_dir): raise HdfToolException("%s already exists" % target_dir, CommandErrorCode.TARGET_ALREADY_EXIST) os.makedirs(target_dir) self._file_gen_lite('hdf_vendor_kconfig.template', target_dir, 'Kconfig', {}) board_parent_path = HdfToolSettings().get_board_parent_path(board) if not board_parent_path: board_parent_path = 'vendor/hisi/hi35xx' data_model = {"board_parent_path": board_parent_path} self._file_gen_lite('hdf_vendor_mk.template', target_dir, 'hdf_vendor.mk', data_model)
def __init__(self): self.file_path = get_hdf_tool_settings_path() self.settings = {} if not os.path.exists(self.file_path): return with open(self.file_path) as file: try: self.settings = json.load(file) except ValueError as exc: raise HdfToolException( 'file: %s format wrong, %s' % (self.file_path, str(exc)), CommandErrorCode.FILE_FORMAT_WRONG) self.supported_boards_key = 'supported_boards' self.drivers_path_key = 'drivers_path_relative_to_vendor' self.dot_configs_key = 'dot_configs' self.board_path_key = 'board_parent_path' self.dot_config_path_key = 'dot_config_path'
def _get_driver_list_handler(self): self.check_arg_raise_if_not_exist("root_dir") self.check_arg_raise_if_not_exist("vendor_name") root, vendor, _, _, _ = self.get_args() hdf_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) if not os.path.exists(hdf_dir): raise HdfToolException('vendor "%s" not exist' % vendor, CommandErrorCode.TARGET_NOT_EXIST) modules = os.listdir(hdf_dir) vendor_k = HdfVendorKconfigFile(root, vendor) module_items = vendor_k.get_module_and_config_path() drivers = {} for item in module_items: module, k_path = item if module in modules: models = \ HdfModuleKconfigFile(root, module, k_path).get_models() drivers[module] = models return json.dumps(drivers)
def __init__(self, root, board, module, driver, only_path=False): self.root = root self.board = board self.module = module self.driver = driver bpp = HdfToolSettings().get_board_parent_path(self.board) board_path = os.path.join(self.root, bpp, self.board) if not os.path.exists(board_path): raise HdfToolException('board: %s not exist' % board_path, CommandErrorCode.TARGET_NOT_EXIST) self.config_dir = os.path.join(board_path, 'config') self.drv_dir = os.path.join(self.config_dir, self.module) self.drv_config_path = os.path.join(self.drv_dir, '%s_config.hcs' % self.driver) if only_path: return manager_hcs_path = os.path.join(self.config_dir, 'device_info', 'device_info.hcs') self.manager_hcs = HdfManagerConfigFile(manager_hcs_path) hdf_hcs_path = os.path.join(self.config_dir, 'hdf.hcs') self.hdf_hcs = HdfHcsFile(hdf_hcs_path)
def check_path_raise_if_not_exist(full_path): if not os.path.exists(full_path): raise HdfToolException('%s not exist' % full_path, CommandErrorCode.TARGET_NOT_EXIST)
def create_dirs(dir_path): if dir_path and not os.path.exists(dir_path): try: os.makedirs(dir_path) except Exception: raise HdfToolException('create dirs fail: %s' % dir_path)
def run(self): if self.server_type in self.servers: self.servers[self.server_type](self.read_obj, self.write_obj).run() else: raise HdfToolException('unknown type: %s' % self.server_type)
def exit(self, status=0, message=None): if status: raise HdfToolException(message, CommandErrorCode.INTERFACE_ERROR) exit(status)
def run(self, cmd, args): if cmd in self.commands: return self.commands[cmd](args).run() else: raise HdfToolException('unknown cmd: "%s"' % cmd, CommandErrorCode.INTERFACE_ERROR)
def set_vendor(self, vendor_name): if not self._is_vendor_valid(vendor_name): raise HdfToolException('vendor: "%s" not exist' % vendor_name, CommandErrorCode.TARGET_NOT_EXIST) self._set_var_value(self.hdf_vendor_var_name, vendor_name)