def test_local_registry_update(): """Test local-registry-sync cli command.""" PACKAGES = [ PackageId(PackageType.CONNECTION, PublicId("fetchai", "local", "0.11.0")), PackageId(PackageType.AGENT, PublicId("fetchai", "my_first_aea", "0.10.0")), ] with TemporaryDirectory() as tmp_dir: for package_id in PACKAGES: package_dir = os.path.join( tmp_dir, package_id.public_id.author, str(package_id.package_type.to_plural()), package_id.public_id.name, ) os.makedirs(package_dir) fetch_package( str(package_id.package_type), public_id=package_id.public_id, cwd=tmp_dir, dest=package_dir, ) assert set(PACKAGES) == set([i[0] for i in enlist_packages(tmp_dir)]) runner = CliRunner() with cd(tmp_dir): # check intention to upgrade with patch( "aea.cli.local_registry_sync.replace_package" ) as replace_package_mock: result = runner.invoke( cli, ["-s", "local-registry-sync"], catch_exceptions=False ) assert result.exit_code == 0, result.stdout assert replace_package_mock.call_count == len(PACKAGES) # do actual upgrade result = runner.invoke( cli, ["-s", "local-registry-sync"], catch_exceptions=False ) assert result.exit_code == 0, result.stdout # check next update will do nothing with patch( "aea.cli.local_registry_sync.replace_package" ) as replace_package_mock: result = runner.invoke( cli, ["-s", "local-registry-sync"], catch_exceptions=False ) assert result.exit_code == 0, result.stdout assert replace_package_mock.call_count == 0 def sort_(packages): return sorted(packages, key=lambda x: str(x)) new_packages = [i[0] for i in enlist_packages(tmp_dir)] for new_package, old_package in zip(sort_(new_packages), sort_(PACKAGES)): assert new_package.public_id > old_package.public_id
def test_fetch_package_positive(self, extract_mock, download_file_mock, request_api_mock): """Test for fetch_package method positive result.""" obj_type = "connection" public_id = PublicId.from_str("author/name:0.1.0") cwd = "cwd" dest_path = os.path.join("dest", "path", "package_folder_name") fetch_package(obj_type, public_id, cwd, dest_path) request_api_mock.assert_called_with("GET", "/connections/author/name/0.1.0") download_file_mock.assert_called_once_with("url", "cwd") extract_mock.assert_called_once_with("filepath", os.path.join("dest", "path"))
def fetch_item_mixed( ctx: Context, item_type: str, item_public_id: PublicId, dest_path: str, ) -> Path: """ Find item, 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 dest_path: the path to the destination. :return: the path to the found package. """ try: package_path = find_item_locally_or_distributed( ctx, item_type, item_public_id, dest_path) except click.ClickException as e: logger.debug( f"Fetch from local registry failed (reason={str(e)}), trying remote registry..." ) # the following might raise exception, but we don't catch it this time package_path = fetch_package(item_type, public_id=item_public_id, cwd=ctx.cwd, dest=dest_path) return package_path
def replace_package(package_type: str, public_id: PublicId, package_dir: Union[Path, str]) -> None: """ Download, extract and replace exists package. :param package_type: str. :param public_id: pacakge bulic id to download :param: package_dir: target package dir :return: None """ with TemporaryDirectory() as tmp_dir: new_package_dir = os.path.join(tmp_dir, public_id.name) os.mkdir(new_package_dir) fetch_package(package_type, public_id=public_id, cwd=tmp_dir, dest=new_package_dir) shutil.rmtree(package_dir) shutil.move(new_package_dir, package_dir)
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 """ click.echo(f"Adding {item_type} '{item_public_id}'...") if is_item_present(ctx.cwd, ctx.agent_config, item_type, item_public_id): present_item_id = get_item_id_present(ctx.agent_config, item_type, item_public_id) raise click.ClickException( "A {} with id '{}' already exists. Aborting...".format( item_type, present_item_id)) dest_path = get_package_path(ctx.cwd, item_type, item_public_id) is_local = ctx.config.get("is_local") is_mixed = ctx.config.get("is_mixed") ctx.clean_paths.append(dest_path) if is_mixed: package_path = fetch_item_mixed(ctx, item_type, item_public_id, dest_path) elif is_local: package_path = find_item_locally_or_distributed( ctx, item_type, item_public_id, 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 ctx.config.get( "skip_consistency_check") and 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_config.public_id) click.echo(f"Successfully added {item_type} '{item_config.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)