def test_find_item_locally_bad_config(self, *mocks): """Test find_item_locally for bad config result.""" public_id = PublicIdMock.from_str("fetchai/echo:0.11.0") with self.assertRaises(ClickException) as cm: find_item_locally(ContextMock(), "skill", public_id) self.assertIn("configuration file not valid", cm.exception.message)
def test_find_item_locally_cant_find(self, from_conftype_mock, *mocks): """Test find_item_locally for can't find result.""" public_id = PublicIdMock.from_str("fetchai/echo:0.11.0") with self.assertRaises(ClickException) as cm: find_item_locally(ContextMock(), "skill", public_id) self.assertEqual( cm.exception.message, "Cannot find skill with author and version specified." )
def get_latest_version_available_in_registry( ctx: Context, item_type: str, item_public_id: PublicId) -> PublicId: """ Get latest avalable package version public id. :param ctx: Context object. :param item_type: the item type. :param item_public_id: the item public id. :return: PublicId """ is_local = ctx.config.get("is_local") try: if is_local: _, item_config = find_item_locally(ctx, item_type, item_public_id) latest_item_public_id = item_config.public_id else: package_meta = get_package_meta(item_type, item_public_id) latest_item_public_id = PublicId.from_str( package_meta["public_id"]) except Exception: # pylint: disable=broad-except raise click.ClickException( f"Package {item_public_id} details can not be fetched from the registry!" ) return latest_item_public_id
def get_latest_public_id_mixed( ctx: Context, item_type: str, item_public_id: PublicId, aea_version: Optional[str] = None, ) -> PublicId: """ Get latest public id of the message, mixed mode. That is, give priority to local registry, and fall back to remote registry in case of failure. :param ctx: the CLI context. :param item_type: the item type. :param item_public_id: the item public id. :param aea_version: the AEA version constraint, or None :return: the path to the found package. """ try: _, item_config = find_item_locally(ctx, item_type, item_public_id) latest_item_public_id = item_config.public_id except click.ClickException: logger.debug( "Get latest public id from local registry failed, trying remote registry..." ) # the following might raise exception, but we don't catch it this time package_meta = get_package_meta( item_type, item_public_id, aea_version=aea_version ) latest_item_public_id = PublicId.from_str(cast(str, package_meta["public_id"])) return latest_item_public_id
def add_item(ctx: Context, item_type: str, item_public_id: PublicId) -> None: """ Add an item. :param ctx: Context object. :param item_type: the item type. :param item_public_id: the item public id. :return: None """ agent_name = cast(str, ctx.agent_config.agent_name) click.echo("Adding {} '{}' to the agent '{}'...".format( item_type, item_public_id, agent_name)) if is_item_present(ctx, item_type, item_public_id): raise click.ClickException( "A {} with id '{}/{}' already exists. Aborting...".format( item_type, item_public_id.author, item_public_id.name)) dest_path = get_package_path(ctx, item_type, item_public_id) is_local = ctx.config.get("is_local") ctx.clean_paths.append(dest_path) if item_public_id in [DEFAULT_CONNECTION, *LOCAL_PROTOCOLS, DEFAULT_SKILL]: source_path = find_item_in_distribution(ctx, item_type, item_public_id) package_path = copy_package_directory(source_path, dest_path) elif is_local: source_path = find_item_locally(ctx, item_type, item_public_id) package_path = copy_package_directory(source_path, dest_path) else: package_path = fetch_package(item_type, public_id=item_public_id, cwd=ctx.cwd, dest=dest_path) item_config = load_item_config(item_type, package_path) if not is_fingerprint_correct(package_path, item_config): # pragma: no cover raise click.ClickException( "Failed to add an item with incorrect fingerprint.") _add_item_deps(ctx, item_type, item_config) register_item(ctx, item_type, item_public_id)
def find_item_locally_or_distributed(ctx: Context, item_type: str, item_public_id: PublicId, dest_path: str) -> Path: """ Unify find item locally both in case it is distributed or not. :param ctx: the CLI context. :param item_type: the item type. :param item_public_id: the item public id. :param dest_path: the path to the destination. :return: the path to the found package. """ is_distributed = is_distributed_item(item_public_id) if is_distributed: # pragma: nocover source_path = find_item_in_distribution(ctx, item_type, item_public_id) package_path = copy_package_directory(source_path, dest_path) else: source_path, _ = find_item_locally(ctx, item_type, item_public_id) package_path = copy_package_directory(source_path, dest_path) return package_path
def get_latest_version_available_in_registry( ctx: Context, item_type: str, item_public_id: PublicId, aea_version: Optional[str] = None, ) -> PublicId: """ Get latest available package version public id. Optionally consider AEA version through the `aea_version` parameter. :param ctx: Context object. :param item_type: the item type. :param item_public_id: the item public id. :param aea_version: the AEA version (e.g. "0.1.0") or None. :return: the latest public id. """ is_local = ctx.config.get("is_local") is_mixed = ctx.config.get("is_mixed") try: if is_mixed: latest_item_public_id = get_latest_public_id_mixed( ctx, item_type, item_public_id, aea_version ) elif is_local: _, item_config = find_item_locally(ctx, item_type, item_public_id) latest_item_public_id = item_config.public_id else: package_meta = get_package_meta(item_type, item_public_id, aea_version) latest_item_public_id = PublicId.from_str( cast(str, package_meta["public_id"]) ) except Exception: # pylint: disable=broad-except raise click.ClickException( f"Package {item_public_id} details can not be fetched from the registry!" ) return latest_item_public_id
def _add_item(click_context, item_type, item_public_id) -> None: """ Add an item. :param click_context: the click context. :param item_type: the item type. :param item_public_id: the item public id. :return: None """ ctx = cast(Context, click_context.obj) agent_name = cast(str, ctx.agent_config.agent_name) item_type_plural = item_type + "s" supported_items = getattr(ctx.agent_config, item_type_plural) is_local = ctx.config.get("is_local") click.echo( "Adding {} '{}' to the agent '{}'...".format( item_type, item_public_id, agent_name ) ) # check if we already have an item with the same name logger.debug( "{} already supported by the agent: {}".format( item_type_plural.capitalize(), supported_items ) ) if _is_item_present(item_type, item_public_id, ctx): raise click.ClickException( "A {} with id '{}/{}' already exists. Aborting...".format( item_type, item_public_id.author, item_public_id.name ) ) # find and add protocol dest_path = get_package_dest_path( ctx, item_public_id.author, item_type_plural, item_public_id.name ) ctx.clean_paths.append(dest_path) if item_public_id in [DEFAULT_CONNECTION, DEFAULT_PROTOCOL, DEFAULT_SKILL]: source_path = find_item_in_distribution(ctx, item_type, item_public_id) package_path = copy_package_directory( ctx, source_path, item_type, item_public_id.name, item_public_id.author, dest_path, ) elif is_local: source_path = find_item_locally(ctx, item_type, item_public_id) package_path = copy_package_directory( ctx, source_path, item_type, item_public_id.name, item_public_id.author, dest_path, ) else: package_path = fetch_package( item_type, public_id=item_public_id, cwd=ctx.cwd, dest=dest_path ) configuration_file_name = _get_default_configuration_file_name_from_type(item_type) configuration_path = package_path / configuration_file_name configuration_loader = ConfigLoader.from_configuration_type(PackageType(item_type)) item_configuration = configuration_loader.load(configuration_path.open()) _validate_fingerprint(package_path, item_configuration) if item_type in {"connection", "skill"}: _add_protocols(click_context, item_configuration.protocols) if item_type == "skill": for contract_public_id in item_configuration.contracts: if contract_public_id not in ctx.agent_config.contracts: _add_item(click_context, "contract", contract_public_id) # add the item to the configurations. logger.debug( "Registering the {} into {}".format(item_type, DEFAULT_AEA_CONFIG_FILE) ) supported_items.add(item_public_id) ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w") )