Esempio n. 1
0
 def test_fetch_agent_with_dependencies_unable_to_fetch(self, *mocks):
     """Test for fetch_agent method unable to fetch."""
     ctx_mock = ContextMock(
         connections=["public/id:{}".format(PublicIdMock.DEFAULT_VERSION)]
     )
     with self.assertRaises(ClickException):
         fetch_agent(ctx_mock, PublicIdMock())
Esempio n. 2
0
def fetch(click_context, public_id, alias, local, remote):
    """Fetch Agent from Registry."""
    ctx = cast(Context, click_context.obj)
    if remote:
        fetch_agent(ctx, public_id, alias)
    else:
        fetch_agent_locally(ctx, public_id, alias, is_mixed=not local)
Esempio n. 3
0
def fetch(click_context, public_id, alias, local):
    """Fetch Agent from Registry."""
    ctx = cast(Context, click_context.obj)
    if local:
        fetch_agent_locally(ctx, public_id, alias)
    else:
        fetch_agent(ctx, public_id, alias)
Esempio n. 4
0
def do_fetch(
    ctx: Context,
    public_id: PublicId,
    local: bool,
    remote: bool,
    alias: Optional[str] = None,
    target_dir: Optional[str] = None,
) -> None:
    """
    Run the Fetch command.

    :param ctx: the CLI context.
    :param public_id: the public id.
    :param local: whether to fetch from local
    :param remote whether to fetch from remote
    :param alias: the agent alias.
    :param target_dir: the target directory, in case fetching locally.
    :return: None
    """
    enforce(
        not (local and remote), "'local' and 'remote' options are mutually exclusive."
    )
    is_mixed = not local and not remote
    ctx.set_config("is_local", local and not remote)
    ctx.set_config("is_mixed", is_mixed)
    if remote:
        fetch_agent(ctx, public_id, alias=alias, target_dir=target_dir)
    elif local:
        fetch_agent_locally(ctx, public_id, alias=alias, target_dir=target_dir)
    else:
        fetch_mixed(ctx, public_id, alias=alias, target_dir=target_dir)
Esempio n. 5
0
    def load(
        cls,
        working_dir: str,
        public_id: PublicId,
        is_local: bool = False,
        registry_path: str = "packages",
        skip_consistency_check: bool = False,
    ) -> "Project":
        """
        Load project with given public_id to working_dir.

        :param working_dir: the working directory
        :param public_id: the public id
        :param is_local: whether to fetch from local or remote
        :param registry_path: the path to the registry locally
        :param skip_consistency_check: consistency checks flag
        """
        ctx = Context(cwd=working_dir, registry_path=registry_path)
        ctx.set_config("skip_consistency_check", skip_consistency_check)
        path = os.path.join(working_dir, public_id.author, public_id.name)
        target_dir = os.path.join(public_id.author, public_id.name)
        if is_local:
            fetch_agent_locally(ctx, public_id, target_dir=target_dir)
        else:
            fetch_agent(ctx, public_id, target_dir=target_dir)
        return cls(public_id, path)
Esempio n. 6
0
def fetch(click_context, public_id, alias, local):
    """Fetch Agent from Registry."""
    if local:
        ctx = cast(Context, click_context.obj)
        ctx.set_config("is_local", True)
        _fetch_agent_locally(click_context, public_id, alias)
    else:
        fetch_agent(click_context, public_id, alias)
Esempio n. 7
0
def fetch(click_context, public_id, alias, local, remote):
    """Fetch an agent from the registry."""
    ctx = cast(Context, click_context.obj)
    is_mixed = not local and not remote
    ctx.set_config("is_local", local and not remote)
    ctx.set_config("is_mixed", is_mixed)
    if remote:
        fetch_agent(ctx, public_id, alias)
    elif local:
        fetch_agent_locally(ctx, public_id, alias)
    else:
        fetch_mixed(ctx, public_id, alias)
Esempio n. 8
0
 def test_fetch_agent_positive(
     self, request_api_mock, extract_mock, download_file_mock, *mocks
 ):
     """Test for fetch_agent method positive result."""
     public_id_mock = PublicIdMock()
     fetch_agent(ContextMock(), public_id_mock, alias="alias")
     request_api_mock.assert_called_with(
         "GET",
         "/agents/{}/{}/{}".format(
             public_id_mock.author, public_id_mock.name, public_id_mock.version
         ),
     )
     download_file_mock.assert_called_once_with("url", "cwd")
     extract_mock.assert_called_once_with("filepath", "cwd")
Esempio n. 9
0
    def upgrade(self) -> bool:
        """
        Upgrade the project by fetching from remote registry.

        :return: True if the upgrade succeeded, False otherwise.
        """
        agent_config = self.ctx.agent_config
        agent_package_id = agent_config.package_id
        click.echo(
            f"Checking if there is a newer remote version of agent package '{agent_package_id.public_id}'..."
        )
        try:
            new_item = get_latest_version_available_in_registry(
                self.ctx,
                str(agent_package_id.package_type),
                agent_package_id.public_id.to_latest(),
                aea_version=self._current_aea_version,
            )
        except click.ClickException:
            click.echo("Package not found, continuing with normal upgrade.")
            return False

        if new_item.package_version <= agent_config.public_id.package_version:  # type: ignore
            click.echo(
                f"Latest version found is '{new_item.version}' which is smaller or equal than current version '{agent_config.public_id.package_version}'. Continuing..."
            )
            return False

        current_path = Path(self.ctx.cwd).absolute()
        user_wants_to_upgrade = self._ask_user_if_wants_to_upgrade(
            new_item, current_path)
        if not user_wants_to_upgrade:
            return False

        click.echo(f"Upgrading project to version '{new_item.version}'")

        try:
            delete_directory_contents(current_path)
        except OSError as e:  # pragma: nocover
            raise click.ClickException(
                f"Cannot remote path {current_path}. Error: {str(e)}.")

        fetch_agent(self.ctx, new_item, alias=self._TEMP_ALIAS)
        self.ctx.cwd = str(current_path)
        self._unpack_fetched_agent()
        return True
Esempio n. 10
0
 def test_fetch_agent_with_dependencies_positive(
     self, request_api_mock, add_item_mock, extract_mock, download_file_mock, *mocks
 ):
     """Test for fetch_agent method with dependencies positive result."""
     public_id_mock = PublicIdMock()
     ctx_mock = ContextMock(
         connections=["public/id:{}".format(PublicIdMock.DEFAULT_VERSION)]
     )
     fetch_agent(ctx_mock, public_id_mock)
     request_api_mock.assert_called_with(
         "GET",
         "/agents/{}/{}/{}".format(
             public_id_mock.author, public_id_mock.name, public_id_mock.version
         ),
     )
     download_file_mock.assert_called_once_with("url", "cwd")
     extract_mock.assert_called_once_with("filepath", "cwd")
     add_item_mock.assert_called()
Esempio n. 11
0
 def test_fetch_agent_with_dependencies_positive(
     self,
     request_api_mock,
     fetch_package_mock,
     extract_mock,
     download_file_mock,
 ):
     """Test for fetch_agent method with dependencies positive result."""
     public_id_mock = PublicIdMock()
     fetch_agent(ContextMock(), public_id_mock)
     request_api_mock.assert_called_with(
         "GET",
         "/agents/{}/{}/{}".format(public_id_mock.author,
                                   public_id_mock.name,
                                   public_id_mock.version),
     )
     download_file_mock.assert_called_once_with("url", "cwd")
     extract_mock.assert_called_once_with("filepath", "cwd/name")
     fetch_package_mock.assert_called()
Esempio n. 12
0
def fetch_mixed(
    ctx: Context,
    public_id: PublicId,
    alias: Optional[str] = None,
    target_dir: Optional[str] = None,
) -> None:
    """
    Fetch an agent in mixed mode.

    :param ctx: the Context.
    :param public_id: the public id.
    :param alias: the alias to the agent.
    :param target_dir: the target directory.
    :return: None
    """
    try:
        fetch_agent_locally(ctx, public_id, alias=alias, target_dir=target_dir)
    except click.ClickException as e:
        logger.debug(
            f"Fetch from local registry failed (reason={str(e)}), trying remote registry..."
        )
        fetch_agent(ctx, public_id, alias=alias, target_dir=target_dir)
Esempio n. 13
0
 def test_fetch_agent_with_dependencies_unable_to_fetch(self, *mocks):
     """Test for fetch_agent method positive result."""
     with self.assertRaises(ClickException):
         fetch_agent(ContextMock(), PublicIdMock())