def test_get_available_device_mapping_raises_with_invalid_listing_2(self, logger): flasher = Flasher("asd") with self.assertRaises(GeneralFatalError) as cm: Common(logger).get_available_device_mapping([flasher], "as") self.assertEqual(cm.exception.return_code, EXIT_CODE_UNHANDLED_EXCEPTION) self.assertEqual(flasher.call_count, 1)
def test_get_available_device_mapping_raises_with_target_not_matching(self, logger, mock_sleep): flasher = Flasher([{"target_id": "asd"}]) with self.assertRaises(GeneralFatalError) as cm: Common(logger).get_available_device_mapping([flasher], "dsd") self.assertEqual(cm.exception.return_code, EXIT_CODE_COULD_NOT_MAP_DEVICE) self.assertEqual(flasher.call_count, 5)
def flash_multiple(self, build, platform_name, method='simple', target_ids_or_prefix='', no_reset=None): """ :param build: build :param platform_name: platform name :param method: method :param target_ids_or_prefix: target ids or prefix :param no_reset: with/without reset :return: """ device_mapping_table = Common( self.logger).get_available_device_mapping(self._flashers) if not platform_name: Flash._verify_platform_coherence(device_mapping_table) if isinstance(target_ids_or_prefix, list): aux_device_mapping_table = Flash._map_by_target_id( device_mapping_table=device_mapping_table, platform_name=platform_name, target_ids=target_ids_or_prefix) elif target_ids_or_prefix: aux_device_mapping_table = Flash._map_by_prefix( device_mapping_table=device_mapping_table, platform_name=platform_name, prefix=target_ids_or_prefix) else: aux_device_mapping_table = Flash._map_by_platform( device_mapping_table=device_mapping_table, platform_name=platform_name) if aux_device_mapping_table: device_mapping_table = aux_device_mapping_table if not device_mapping_table: raise FlashError( message="no devices to flash", return_code=EXIT_CODE_COULD_NOT_MAP_TARGET_ID_TO_DEVICE) self.logger.debug(device_mapping_table) self.logger.info('Going to flash following devices:') for item in device_mapping_table: self.logger.info(item['target_id']) for device in device_mapping_table: self.flash(build=build, target_id=device['target_id'], platform_name=None, device_mapping_table=device_mapping_table, method=method, no_reset=no_reset) return EXIT_CODE_SUCCESS
def _refine__device_mapping_table(self, device_mapping_table, target): """ get device mapping table if it's None. refine device_mapping table to be list :param device_mapping_table: individual devices mapping table """ if device_mapping_table: if isinstance(device_mapping_table, dict): device_mapping_table = [device_mapping_table] elif not isinstance(device_mapping_table, list): raise SystemError('device_mapping_table wasn\'t list or dictionary') else: device_mapping_table = Common(self.logger).get_available_device_mapping( self._flashers, target) self.logger.debug(device_mapping_table) return device_mapping_table
def erase(self, target_id=None, no_reset=None, method=None): """ Erase (mbed) device(s). :param target_id: target_id :param no_reset: erase with/without reset :param method: method for erase i.e. simple, pyocd or edbg """ if target_id is None: raise EraseError(message="target id is missing", return_code=EXIT_CODE_TARGET_ID_MISSING) self.logger.info("Starting erase for given target_id %s", target_id) self.logger.info("method used for reset: %s", method) available_devices = Common(self.logger).get_available_device_mapping( self.flashers, target_id) targets_to_erase = self.prepare_target_to_erase( target_id, available_devices) if len(targets_to_erase) <= 0: msg = "Could not map given target_id(s) to available devices" raise EraseError( message=msg, return_code=EXIT_CODE_COULD_NOT_MAP_TARGET_ID_TO_DEVICE) for item in targets_to_erase: if method == 'simple': erase_fnc = self._erase_board_simple elif method == 'pyocd': erase_fnc = self._erase_board_with_pyocd elif method == 'edbg': raise EraseError(message="egdb not supported", return_code=EXIT_CODE_IMPLEMENTATION_MISSING) else: raise EraseError( message="Selected method {} not supported".format(method), return_code=EXIT_CODE_MISUSE_CMD) erase_fnc(target=item, no_reset=no_reset) return EXIT_CODE_SUCCESS
def reset(self, target_id=None, method=None): """Reset (mbed) device :param target_id: target_id :param method: method for reset i.e. simple, pyocd or edbg """ if target_id is None: raise ResetError(message="Target ID is missing", return_code=EXIT_CODE_TARGET_ID_MISSING) self.logger.info("Starting reset for target_id %s", target_id) self.logger.info("Method for reset: %s", method) available_devices = Common(self.logger).get_available_device_mapping( self._flashers, target_id) targets_to_reset = self.prepare_target_to_reset( target_id, available_devices) if len(targets_to_reset) <= 0: msg = "Could not map given target_id(s) to available devices" print(msg) raise ResetError( message=msg, return_code=EXIT_CODE_COULD_NOT_MAP_TARGET_ID_TO_DEVICE) for item in targets_to_reset: if method == 'simple' and 'serial_port' in item: self.reset_board(item['serial_port']) elif method == 'pyocd': return self.try_pyocd_reset(item) elif method == 'edbg': raise ResetError(message="egdb not supported", return_code=EXIT_CODE_IMPLEMENTATION_MISSING) else: raise ResetError( message="Selected method {} not supported".format(method), return_code=EXIT_CODE_MISUSE_CMD) return EXIT_CODE_SUCCESS
def parse_id_to_devices(self, tid): """ :param tid: target id """ flasher = Flash() available = Common(self.logger).get_available_device_mapping( flasher.get_all_flashers(), tid) target_ids = [] available_target_ids = [] if not available: msg = "Could not find any connected device" print(msg) raise GeneralFatalError(message=msg, return_code=EXIT_CODE_DEVICES_MISSING) if 'all' in tid: for device in available: target_ids.append(device['target_id']) else: for item in tid: for device in available: available_target_ids.append(device['target_id']) if device['target_id'] == item or \ device['target_id'].startswith(item): if device['target_id'] not in target_ids: target_ids.append(device['target_id']) if not target_ids: print("Could not find given target_id from attached devices") print("Available target_ids:") print(available_target_ids) raise GeneralFatalError(message="Could not map device", return_code=EXIT_CODE_COULD_NOT_MAP_DEVICE) if len(target_ids) == 1: return target_ids[0] return target_ids
def subcmd_flash_handler(self, args): """ flash command handler """ if not args.tid: msg = "Target_id is missing" raise FlashError(message=msg, return_code=EXIT_CODE_TARGET_ID_MISSING) check_file(self.logger, args.target_filename or args.input) check_file(self.logger, args.input) check_file_exists(self.logger, args.input) check_file_extension(self.logger, args.target_filename or args.input) flasher = Flash() available = Common(self.logger).get_available_device_mapping( flasher.get_all_flashers(), args.tid) available_target_ids = [] retcode = EXIT_CODE_SUCCESS if args.platform_name: if args.platform_name not in flasher.get_supported_targets(): self.logger.error("Not supported platform: %s", args.platform_name) self.logger.error("Supported platforms: %s", flasher.get_supported_targets()) raise FlashError(message="Platform {} not supported".format( args.platform_name), return_code=EXIT_CODE_NOT_SUPPORTED_PLATFORM) if 'all' in args.tid: retcode = flasher.flash(build=args.input, target_id='all', platform_name=args.platform_name, target_filename=args.target_filename, method=args.method, no_reset=args.no_reset) if len(available) <= 0: msg = "Could not find any connected device" raise FlashError(message=msg, return_code=EXIT_CODE_DEVICES_MISSING) available_platforms, target_ids_to_flash = \ self.prepare_platforms_and_targets(available, args.tid, available_target_ids) if not target_ids_to_flash: self.logger.error( "Could not find given target_id from attached devices") self.logger.error("Available target_ids: %s", available_target_ids) raise FlashError(message="Could not map device", return_code=EXIT_CODE_COULD_NOT_MAP_DEVICE) elif len(available_platforms) > 1: if not args.platform_name: self.logger.error( "More than one platform detected for given target_id") self.logger.error( "Please specify the platform with -t <PLATFORM_NAME>") self.logger.error("Found platforms: %s", available_platforms) raise FlashError( message= "More than one platform detected for given target id", return_code=EXIT_CODE_PLATFORM_REQUIRED) else: retcode = flasher.flash(build=args.input, target_id=target_ids_to_flash, target_filename=args.target_filename, platform_name=available_platforms[0], method=args.method, no_reset=args.no_reset) return retcode
def test_get_available_device_mapping_returns_with_target_partly_matching(self, logger): flasher = Flasher([{"target_id": "asd"}]) results = Common(logger).get_available_device_mapping([flasher], "as") self.assertEqual(results, [{"target_id": "asd"}]) self.assertEqual(flasher.call_count, 1)
def test_get_available_device_mapping_returns_no_devices(self, logger): flasher = Flasher([]) results = Common(logger).get_available_device_mapping([flasher]) self.assertEqual(results, []) self.assertEqual(flasher.call_count, 1)
def test_get_available_device_mapping_returns_list_of_one(self, logger): flasher = Flasher([{"target_id": "asd"}]) results = Common(logger).get_available_device_mapping([flasher], ["asd"]) self.assertEqual(results, [{"target_id": "asd"}]) self.assertEqual(flasher.call_count, 1)
def test_get_available_device_mapping_returns_empty_with_invalid_listing( self, logger): flasher = Flasher([{"target": "asd"}]) results = Common(logger).get_available_device_mapping([flasher], "as") self.assertEqual(results, []) self.assertEqual(flasher.call_count, 1)
def get_available_device_mapping(self): """ Get available devices :return: list of devices """ return Common(self.logger).get_available_device_mapping(self._flashers)