def test_raises_when_file_path_is_invalid(self, mock_stdout, logger): with self.assertRaises(FlashError) as cm: check_is_file_flashable(logger, "") self.assertEqual(cm.exception.return_code, EXIT_CODE_FILE_MISSING) with self.assertRaises(FlashError) as cm: check_is_file_flashable(logger, None) self.assertEqual(cm.exception.return_code, EXIT_CODE_FILE_MISSING)
def test_allows_extensions(self, mock_stdout, mock_isfile, logger): for ext in ["bin", "hex", "act", "cfg", "BIN", "HEX", "ACT", "CFG"]: self.assertEqual(check_is_file_flashable(logger, "test.{}".format(ext)), None)
def test_raises_when_file_has_bad_extension(self, mock_stdout, mock_isfile, logger): with self.assertRaises(FlashError) as cm: check_is_file_flashable(logger, "test.heh") self.assertEqual(cm.exception.return_code, EXIT_CODE_DAPLINK_USER_ERROR)
def test_allows_existing_file(self, mock_stdout, logger): file_path = os.path.join("test", "helloworld.bin") self.assertEqual(check_is_file_flashable(logger, file_path), None)
def test_raises_when_file_does_not_exist(self, mock_stdout, logger): with self.assertRaises(FlashError) as cm: check_is_file_flashable(logger, "should/not/exist") self.assertEqual(cm.exception.return_code, EXIT_CODE_FILE_MISSING)
def test_allows_good_file_path(self, mock_stdout, mock_isfile, logger): self.assertEqual(check_is_file_flashable(logger, "./test.bin"), None)
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_is_file_flashable(self.logger, 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, 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, platform_name=available_platforms[0], method=args.method, no_reset=args.no_reset) return retcode
def flash(self, build, target_id=None, platform_name=None, device_mapping_table=None, method='simple', no_reset=None): """Flash (mbed) device :param build: Build -object or string (file-path) :param target_id: target_id :param platform_name: platform_name, to flash multiple devices of same type :param device_mapping_table: individual devices mapping table :param method: method for flashing i.e. simple, pyocd or edbg :param no_reset: whether to reset the board after flash """ k64f_target_id_length = 48 if target_id is None and platform_name is None: raise SyntaxError("target_id or target_name is required") check_is_file_flashable(self.logger, build) if (isinstance(target_id, list) or target_id.lower() == 'all' or (len(target_id) < k64f_target_id_length and device_mapping_table is None)): if isinstance(target_id, str) and target_id.lower() == 'all': target_id = '' return self.flash_multiple( build=build, platform_name=platform_name, method=method, target_ids_or_prefix=target_id, no_reset=no_reset) device_mapping_table = self._refine__device_mapping_table( device_mapping_table, target_id) try: if target_id: target_mbed = self.__find_by_target_id(target_id, device_mapping_table) else: target_mbed = self.__find_by_platform_name(platform_name, device_mapping_table) except KeyError: msg = "Could not find required device" self.logger.exception(msg) raise FlashError(message=msg, return_code=EXIT_CODE_COULD_NOT_MAP_TARGET_ID_TO_DEVICE) platform_name = self._get_platform_name(platform_name, target_mbed) self.logger.debug("Flashing: %s", target_mbed["target_id"]) flasher = self.__get_flasher(platform_name, target_mbed) retcode = Flash._do_flash(flasher=flasher, build=build, target=target_mbed, method=method, no_reset=no_reset) if retcode == EXIT_CODE_SUCCESS: self.logger.info("%s flash success", target_mbed["target_id"]) else: self.logger.warning("%s flash fails with code %d", target_mbed["target_id"], retcode) return retcode