def _check_is_item_in_local_registry(public_id, item_type_plural, registry_path): try: try_get_item_source_path(registry_path, public_id.author, item_type_plural, public_id.name) except click.ClickException as e: raise click.ClickException( "Dependency is missing. {} " "Please push it first and then retry.".format(e))
def test_get_item_source_path_positive(self, exists_mock, join_mock): """Test for get_item_source_path positive result.""" result = try_get_item_source_path("cwd", AUTHOR, "skills", "skill-name") expected_result = "some-path" self.assertEqual(result, expected_result) join_mock.assert_called_once_with("cwd", AUTHOR, "skills", "skill-name") exists_mock.assert_called_once_with("some-path") result = try_get_item_source_path("cwd", None, "skills", "skill-name") self.assertEqual(result, expected_result)
def _check_is_item_in_local_registry(public_id: PublicId, item_type_plural: str, registry_path: str) -> None: try: try_get_item_source_path(registry_path, public_id.author, item_type_plural, public_id.name) except click.ClickException as e: raise click.ClickException( f"Dependency is missing. {str(e)}\nPlease push it first and then retry." )
def _save_item_locally(ctx: Context, item_type: str, item_id: PublicId) -> None: """ Save item to local packages. :param item_type: str type of item (connection/protocol/skill). :param item_id: the public id of the item. :return: None """ item_type_plural = item_type + "s" source_path = try_get_item_source_path( ctx.cwd, None, item_type_plural, item_id.name ) target_path = try_get_item_target_path( ctx.agent_config.registry_path, ctx.agent_config.author, item_type_plural, item_id.name, ) _check_package_public_id(source_path, item_type, item_id) copytree(source_path, target_path) click.echo( '{} "{}" successfully saved in packages folder.'.format( item_type.title(), item_id ) )
def fetch_agent_locally( ctx: Context, public_id: PublicId, alias: Optional[str] = None, target_dir: Optional[str] = None, ) -> None: """ Fetch Agent from local packages. :param ctx: a Context object. :param public_id: public ID of agent to be fetched. :param alias: an optional alias. :param target_dir: the target directory to which the agent is fetched. :return: None """ packages_path = ( DEFAULT_REGISTRY_NAME if ctx.registry_path is None else ctx.registry_path ) source_path = try_get_item_source_path( packages_path, public_id.author, AGENTS, public_id.name ) enforce( ctx.config.get("is_local") is True or ctx.config.get("is_mixed") is True, "Please use `ctx.set_config('is_local', True)` or `ctx.set_config('is_mixed', True)` to fetch agent and all components locally.", ) try_to_load_agent_config(ctx, agent_src_path=source_path) if not _is_version_correct(ctx, public_id): raise click.ClickException( "Wrong agent version in public ID: specified {}, found {}.".format( public_id.version, ctx.agent_config.version ) ) folder_name = target_dir or (public_id.name if alias is None else alias) target_path = os.path.join(ctx.cwd, folder_name) if os.path.exists(target_path): path = Path(target_path) raise click.ClickException( f'Item "{path.name}" already exists in target folder "{path.parent}".' ) if target_dir is not None: os.makedirs(target_path) # pragma: nocover ctx.clean_paths.append(target_path) copy_tree(source_path, target_path) ctx.cwd = target_path try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias ctx.agent_loader.dump( ctx.agent_config, open_file(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w"), ) _fetch_agent_deps(ctx) click.echo("Agent {} successfully fetched.".format(public_id.name))
def _fetch_agent_locally( click_context, public_id: PublicId, alias: Optional[str] = None ) -> None: """ Fetch Agent from local packages. :param click_context: click context object. :param public_id: public ID of agent to be fetched. :param click_context: the click context. :param alias: an optional alias. :return: None """ packages_path = os.path.basename(DEFAULT_REGISTRY_PATH) source_path = try_get_item_source_path( packages_path, public_id.author, "agents", public_id.name ) ctx = cast(Context, click_context.obj) try_to_load_agent_config(ctx, agent_src_path=source_path) if not _is_version_correct(ctx, public_id): raise click.ClickException( "Wrong agent version in public ID: specified {}, found {}.".format( public_id.version, ctx.agent_config.version ) ) folder_name = public_id.name if alias is None else alias target_path = os.path.join(ctx.cwd, folder_name) if os.path.exists(target_path): raise click.ClickException( 'Item "{}" already exists in target folder.'.format(public_id.name) ) ctx.clean_paths.append(target_path) copy_tree(source_path, target_path) ctx.cwd = target_path try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w") ) # add dependencies for item_type in ("skill", "connection", "contract", "protocol"): item_type_plural = "{}s".format(item_type) required_items = getattr(ctx.agent_config, item_type_plural) for item_id in required_items: try: _add_item(click_context, item_type, item_id) except click.ClickException as e: raise click.ClickException( "Failed to add {} dependency {}: {}".format( item_type, item_id, str(e) ) ) click.echo("Agent {} successfully fetched.".format(public_id.name))
def fetch_agent_locally( ctx: Context, public_id: PublicId, alias: Optional[str] = None, target_dir: Optional[str] = None, is_mixed: bool = True, ) -> None: """ Fetch Agent from local packages. :param ctx: a Context object. :param public_id: public ID of agent to be fetched. :param alias: an optional alias. :param target_dir: the target directory to which the agent is fetched. :param is_mixed: flag to enable mixed mode (try first local, then remote). :return: None """ packages_path = (DEFAULT_REGISTRY_PATH if ctx.registry_path is None else ctx.registry_path) source_path = try_get_item_source_path(packages_path, public_id.author, "agents", public_id.name) try_to_load_agent_config(ctx, agent_src_path=source_path) if not _is_version_correct(ctx, public_id): raise click.ClickException( "Wrong agent version in public ID: specified {}, found {}.".format( public_id.version, ctx.agent_config.version)) folder_name = target_dir or (public_id.name if alias is None else alias) target_path = os.path.join(ctx.cwd, folder_name) if os.path.exists(target_path): raise click.ClickException( 'Item "{}" already exists in target folder.'.format( public_id.name)) if target_dir is not None: os.makedirs(target_path) # pragma: nocover ctx.clean_paths.append(target_path) copy_tree(source_path, target_path) ctx.cwd = target_path try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w")) # add dependencies _fetch_agent_deps(ctx, is_mixed) click.echo("Agent {} successfully fetched.".format(public_id.name))
def _save_item_locally(ctx: Context, item_type: str, item_id: PublicId) -> None: """ Save item to local packages. :param item_type: str type of item (connection/protocol/skill). :param item_id: the public id of the item. :return: None """ item_type_plural = item_type + "s" try: # try non vendor first source_path = try_get_item_source_path(ctx.cwd, None, item_type_plural, item_id.name) except ClickException: # failed on user's packages # try vendors source_path = try_get_item_source_path( os.path.join(ctx.cwd, "vendor"), item_id.author, item_type_plural, item_id.name, ) check_package_public_id(source_path, item_type, item_id) target_path = try_get_item_target_path( ctx.agent_config.registry_path, ctx.agent_config.author, item_type_plural, item_id.name, ) copytree(source_path, target_path) click.echo( f'{item_type.title()} "{item_id}" successfully saved in packages folder.' )
def fetch_agent_locally(ctx: Context, public_id: PublicId, alias: Optional[str] = None) -> None: """ Fetch Agent from local packages. :param ctx: a Context object. :param public_id: public ID of agent to be fetched. :param alias: an optional alias. :return: None """ packages_path = os.path.basename(DEFAULT_REGISTRY_PATH) source_path = try_get_item_source_path(packages_path, public_id.author, "agents", public_id.name) try_to_load_agent_config(ctx, agent_src_path=source_path) if not _is_version_correct(ctx, public_id): raise click.ClickException( "Wrong agent version in public ID: specified {}, found {}.".format( public_id.version, ctx.agent_config.version)) folder_name = public_id.name if alias is None else alias target_path = os.path.join(ctx.cwd, folder_name) if os.path.exists(target_path): raise click.ClickException( 'Item "{}" already exists in target folder.'.format( public_id.name)) ctx.clean_paths.append(target_path) copy_tree(source_path, target_path) ctx.cwd = target_path try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w")) # add dependencies _fetch_agent_deps(ctx) click.echo("Agent {} successfully fetched.".format(public_id.name))
def test_get_item_source_path_not_exists(self, exists_mock, join_mock): """Test for get_item_source_path item already exists.""" with self.assertRaises(ClickException): try_get_item_source_path("cwd", AUTHOR, "skills", "skill-name")