Skip to content

Commit

Permalink
Merge pull request #1911 from fetchai/develop
Browse files Browse the repository at this point in the history
Release v0.7.2
  • Loading branch information
DavidMinarsch authored Nov 9, 2020
2 parents 4c764b4 + 58e4dba commit ad0562d
Show file tree
Hide file tree
Showing 85 changed files with 1,280 additions and 376 deletions.
9 changes: 9 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release History

## 0.7.2 (2020-11-09)

- Fixes some AW2 AEAs
- Improves generic buyer AEA
- Fixes a few backwards incompatibilities on CLI (upgrade, add, fetch) introduced in 0.7.1
- Fixes geo locations in some tests
- Multiple docs updates based on user feedback
- Multiple additional tests and test stability fixes

## 0.7.1 (2020-11-05)

- Adds two AEAs for Agent World 2
Expand Down
2 changes: 1 addition & 1 deletion aea/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
__title__ = "aea"
__description__ = "Autonomous Economic Agent framework"
__url__ = "https://github.com/fetchai/agents-aea.git"
__version__ = "0.7.1"
__version__ = "0.7.2"
__author__ = "Fetch.AI Limited"
__license__ = "Apache-2.0"
__copyright__ = "2019 Fetch.AI Limited"
85 changes: 65 additions & 20 deletions aea/cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
# ------------------------------------------------------------------------------

"""Implementation of the 'aea add' subcommand."""

from pathlib import Path
from typing import cast

import click

from aea.cli.registry.add import fetch_package
from aea.cli.utils.click_utils import PublicIdParameter
from aea.cli.utils.click_utils import PublicIdParameter, registry_flag
from aea.cli.utils.config import load_item_config
from aea.cli.utils.context import Context
from aea.cli.utils.decorators import check_aea_project, clean_after, pass_ctx
Expand All @@ -43,14 +43,14 @@


@click.group()
@click.option("--local", is_flag=True, help="For adding from local folder.")
@registry_flag()
@click.pass_context
@check_aea_project
def add(click_context, local):
def add(click_context, local, remote):
"""Add a resource to the agent."""
ctx = cast(Context, click_context.obj)
if local:
ctx.set_config("is_local", True)
ctx.set_config("is_local", local and not remote)
ctx.set_config("is_mixed", not local and not remote)


@add.command()
Expand Down Expand Up @@ -95,13 +95,7 @@ def add_item(ctx: Context, item_type: str, item_public_id: PublicId) -> None:
: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
)
)
click.echo(f"Adding {item_type} '{item_public_id}'...")
if is_item_present(ctx, item_type, item_public_id):
present_item_id = get_item_id_present(ctx, item_type, item_public_id)
raise click.ClickException(
Expand All @@ -112,16 +106,16 @@ def add_item(ctx: Context, item_type: str, item_public_id: PublicId) -> None:

dest_path = get_package_path(ctx, 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)

is_distributed = is_distributed_item(item_public_id)
if is_local and 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)
elif is_local and not is_distributed:
source_path, _ = find_item_locally(ctx, item_type, item_public_id)
package_path = copy_package_directory(source_path, 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
Expand Down Expand Up @@ -167,3 +161,54 @@ def _add_item_deps(ctx: Context, item_type: str, item_config) -> None:
for skill_public_id in item_config.skills:
if skill_public_id not in ctx.agent_config.skills:
add_item(ctx, "skill", skill_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 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:
click.echo("Fetch from local registry failed, 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
60 changes: 24 additions & 36 deletions aea/cli/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@
def fetch(click_context, public_id, alias, local, remote):
"""Fetch Agent from 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_agent_locally(ctx, public_id, alias, is_mixed=not local)
fetch_mixed(ctx, public_id, alias)


def _is_version_correct(ctx: Context, agent_public_id: PublicId) -> bool:
Expand All @@ -76,7 +81,6 @@ def fetch_agent_locally(
public_id: PublicId,
alias: Optional[str] = None,
target_dir: Optional[str] = None,
is_mixed: bool = True,
) -> None:
"""
Fetch Agent from local packages.
Expand All @@ -85,7 +89,6 @@ def fetch_agent_locally(
: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 = (
Expand Down Expand Up @@ -125,58 +128,43 @@ def fetch_agent_locally(
)

# add dependencies
_fetch_agent_deps(ctx, is_mixed)
_fetch_agent_deps(ctx)
click.echo("Agent {} successfully fetched.".format(public_id.name))


def _fetch_agent_deps(ctx: Context, is_mixed: bool = True) -> None:
def _fetch_agent_deps(ctx: Context) -> None:
"""
Fetch agent dependencies.
:param ctx: context object.
:param is_mixed: flag to enable mixed mode (try first local, then remote).
:return: None
:raises: ClickException re-raises if occurs in add_item call.
"""
ctx.set_config("is_local", True)
ctx.set_config("is_mixed", is_mixed)
for item_type in ("protocol", "contract", "connection", "skill"):
item_type_plural = "{}s".format(item_type)
required_items = getattr(ctx.agent_config, item_type_plural)
for item_id in required_items:
fetch_local_or_mixed(ctx, item_type, item_id)
add_item(ctx, item_type, item_id)


def fetch_local_or_mixed(ctx: Context, item_type: str, item_id: PublicId) -> None:
def fetch_mixed(
ctx: Context,
public_id: PublicId,
alias: Optional[str] = None,
target_dir: Optional[str] = None,
) -> None:
"""
Fetch item, either local or mixed, depending on the parameters/context configuration.
Fetch an agent in mixed mode.
It will first try to fetch from local registry; in case of failure,
if the 'is_mixed' flag is set, it will try to fetch from remote registry.
Context expects 'is_local' and 'is_mixed' to be set.
:param ctx: the CLI context.
:param item_type: the type of the package.
:param item_id: the public id of the item.
: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
"""

def _raise(item_type_: str, item_id_: PublicId, exception):
"""Temporary function to raise exception (used below twice)."""
raise click.ClickException(
f"Failed to add {item_type_} dependency {item_id_}: {str(exception)}"
)

try:
add_item(ctx, item_type, item_id)
except click.ClickException as e:
if not ctx.config.get("is_mixed", False):
_raise(item_type, item_id, e)
ctx.set_config("is_local", False)
try:
add_item(ctx, item_type, item_id)
except click.ClickException as e:
_raise(item_type, item_id, e)
ctx.set_config("is_local", True)
fetch_agent_locally(ctx, public_id, alias=alias, target_dir=target_dir)
except click.ClickException:
click.echo("Fetch from local registry failed, trying on remote registry...")
fetch_agent(ctx, public_id, alias=alias, target_dir=target_dir)
2 changes: 1 addition & 1 deletion aea/cli/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def publish(click_context, local, remote): # pylint: disable=unused-argument
if remote:
publish_agent(ctx)
else:
_save_agent_locally(ctx, is_mixed=not local)
_save_agent_locally(ctx, is_mixed=not local and not remote)


def _validate_config(ctx: Context) -> None:
Expand Down
8 changes: 3 additions & 5 deletions aea/cli/registry/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def fetch_agent(
:return: None
"""
author, name, version = public_id.author, public_id.name, public_id.version
api_path = "/agents/{}/{}/{}".format(author, name, version)
api_path = f"/agents/{author}/{name}/{version}"
resp = request_api("GET", api_path)
file_url = resp["file"]

Expand Down Expand Up @@ -88,9 +88,7 @@ def fetch_agent(
add_item(ctx, item_type, item_public_id)
except Exception as e:
raise click.ClickException(
'Unable to fetch dependency for agent "{}", aborting. {}'.format(
name, e
)
f'Unable to fetch dependency for agent "{name}", aborting. {e}'
)
click.echo("Dependencies successfully fetched.")
click.echo("Agent {} successfully fetched to {}.".format(name, aea_folder))
click.echo(f"Agent {name} successfully fetched to {aea_folder}.")
Loading

0 comments on commit ad0562d

Please sign in to comment.