Exemplo n.º 1
0
 def __init__(self, cwd: str = "."):
     """Init the context."""
     self.config = dict()  # type: Dict
     self.agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
     self.skill_loader = ConfigLoader("skill-config_schema.json",
                                      SkillConfig)
     self.connection_loader = ConfigLoader("connection-config_schema.json",
                                           ConnectionConfig)
     self.protocol_loader = ConfigLoader("protocol-config_schema.json",
                                         ProtocolConfig)
     self.cwd = cwd
Exemplo n.º 2
0
    def _add_protocol(self, directory: str, protocol_name: str):
        """
        Add a protocol.

        :param directory: the agent's resources directory.
        :param protocol_name: the name of the protocol to be added.
        :return: None
        """
        # get the serializer
        serialization_spec = importlib.util.spec_from_file_location("serialization",
                                                                    os.path.join(directory, "protocols", protocol_name, "serialization.py"))
        serialization_module = importlib.util.module_from_spec(serialization_spec)
        serialization_spec.loader.exec_module(serialization_module)  # type: ignore
        classes = inspect.getmembers(serialization_module, inspect.isclass)
        serializer_classes = list(filter(lambda x: re.match("\\w+Serializer", x[0]), classes))
        serializer_class = serializer_classes[0][1]

        logger.debug("Found serializer class {serializer_class} for protocol {protocol_name}"
                     .format(serializer_class=serializer_class, protocol_name=protocol_name))
        serializer = serializer_class()

        config_loader = ConfigLoader("protocol-config_schema.json", ProtocolConfig)
        protocol_config = config_loader.load(open(Path(directory, "protocols", protocol_name, DEFAULT_PROTOCOL_CONFIG_FILE)))

        # instantiate the protocol manager.
        protocol = Protocol(protocol_name, serializer, protocol_config)
        self.register((protocol_name, None), protocol)
Exemplo n.º 3
0
def _verify_ledger_apis_access() -> None:
    """Verify access to ledger apis."""
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = open(str(path), mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, value in aea_conf.ledger_apis.read_all():
        if identifier not in SUPPORTED_LEDGER_APIS:
            ValueError("Unsupported identifier in ledger apis.")

    fetchai_ledger_api_config = aea_conf.ledger_apis.read(FETCHAI)
    if fetchai_ledger_api_config is None:
        logger.debug("No fetchai ledger api config specified.")
    else:
        fetchai_ledger_api_config = cast(LedgerAPIConfig,
                                         fetchai_ledger_api_config)
        _try_to_instantiate_fetchai_ledger_api(fetchai_ledger_api_config.addr,
                                               fetchai_ledger_api_config.port)

    ethereum_ledger_config = aea_conf.ledger_apis.read(ETHEREUM)
    if ethereum_ledger_config is None:
        logger.debug("No ethereum ledger api config specified.")
    else:
        ethereum_ledger_config = cast(LedgerAPIConfig, ethereum_ledger_config)
        _try_to_instantiate_ethereum_ledger_api(ethereum_ledger_config.addr,
                                                ethereum_ledger_config.port)
Exemplo n.º 4
0
    def from_dir(cls, directory: str) -> "Protocol":
        """
        Load a protocol from a directory.

        :param directory: the skill directory.
        :param agent_context: the agent's context
        :return: the Protocol object.
        :raises Exception: if the parsing failed.
        """
        # check if there is the config file. If not, then return None.
        protocol_loader = ConfigLoader("protocol-config_schema.json",
                                       ProtocolConfig)
        protocol_config = protocol_loader.load(
            open(os.path.join(directory, DEFAULT_PROTOCOL_CONFIG_FILE)))
        protocol_module = load_module("protocols",
                                      Path(directory, "serialization.py"))
        add_agent_component_module_to_sys_modules("protocol",
                                                  protocol_config.name,
                                                  protocol_config.author,
                                                  protocol_module)
        classes = inspect.getmembers(protocol_module, inspect.isclass)
        serializer_classes = list(
            filter(lambda x: re.match("\\w+Serializer", x[0]), classes))
        assert len(
            serializer_classes) == 1, "Not exactly one serializer detected."
        serializer_class = serializer_classes[0][1]

        protocol_id = PublicId(protocol_config.author, protocol_config.name,
                               protocol_config.version)
        protocol = Protocol(protocol_id, serializer_class(), protocol_config)
        return protocol
Exemplo n.º 5
0
def verify_or_create_private_keys(ctx: Context) -> None:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = path.open(mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, _value in aea_conf.private_key_paths.read_all():
        if identifier not in registry.supported_crypto_ids:
            ValueError("Unsupported identifier in private key paths.")

    for identifier, private_key_path in IDENTIFIER_TO_KEY_FILES.items():
        config_private_key_path = aea_conf.private_key_paths.read(identifier)
        if config_private_key_path is None:
            create_private_key(identifier)
            aea_conf.private_key_paths.update(identifier, private_key_path)
        else:
            try:
                _try_validate_private_key_path(identifier, private_key_path)
            except FileNotFoundError:  # pragma: no cover
                raise click.ClickException(
                    "File {} for private key {} not found.".format(
                        repr(private_key_path), identifier,
                    )
                )

    # update aea config
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    fp = path.open(mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    ctx.agent_config = aea_conf
Exemplo n.º 6
0
def _load_protocol_specification_from_string(
    specification_content: str, ) -> ProtocolSpecification:
    """Load a protocol specification from string."""
    file = StringIO(initial_value=specification_content)
    config_loader = ConfigLoader("protocol-specification_schema.json",
                                 ProtocolSpecification)
    protocol_spec = config_loader.load_protocol_specification(file)
    return protocol_spec
Exemplo n.º 7
0
    def test_compare_latest_generator_output_with_test_protocol(self):
        """Test that the "t_protocol" test protocol matches with what the latest generator generates based on the specification."""
        # check protoc is installed
        res = shutil.which("protoc")
        if res is None:
            pytest.skip(
                "Please install protocol buffer first! See the following link: https://developers.google.com/protocol-buffers/"
            )

        # Specification
        protocol_name = "t_protocol"
        path_to_specification = os.path.join(self.cwd, "tests", "data",
                                             "sample_specification.yaml")
        path_to_generated_protocol = self.t
        path_to_original_protocol = os.path.join(self.cwd, "tests", "data",
                                                 "generator", protocol_name)
        path_to_package = "tests.data.generator."

        # Load the config
        config_loader = ConfigLoader("protocol-specification_schema.json",
                                     ProtocolSpecification)
        protocol_specification = config_loader.load_protocol_specification(
            open(path_to_specification))

        # Generate the protocol
        protocol_generator = ProtocolGenerator(
            protocol_specification,
            path_to_generated_protocol,
            path_to_protocol_package=path_to_package,
        )
        protocol_generator.generate()

        # Apply black
        try:
            subp = subprocess.Popen(  # nosec
                [
                    sys.executable,
                    "-m",
                    "black",
                    os.path.join(path_to_generated_protocol, protocol_name),
                    "--quiet",
                ])
            subp.wait(10.0)
        finally:
            poll = subp.poll()
            if poll is None:  # pragma: no cover
                subp.terminate()
                subp.wait(5)

        # compare __init__.py
        init_file_generated = Path(self.t, protocol_name, "__init__.py")
        init_file_original = Path(
            path_to_original_protocol,
            "__init__.py",
        )
        assert filecmp.cmp(init_file_generated, init_file_original)
Exemplo n.º 8
0
    def from_dir(cls, directory: str, agent_context: AgentContext) -> 'Skill':
        """
        Load a skill from a directory.

        :param directory: the skill
        :param agent_context: the agent's context
        :return: the Skill object.
        :raises Exception: if the parsing failed.
        """
        # check if there is the config file. If not, then return None.
        skill_loader = ConfigLoader("skill-config_schema.json", SkillConfig)
        skill_config = skill_loader.load(open(os.path.join(directory, DEFAULT_SKILL_CONFIG_FILE)))
        skills_spec = importlib.util.spec_from_file_location(skill_config.name, os.path.join(directory, "__init__.py"))
        skill_module = importlib.util.module_from_spec(skills_spec)
        sys.modules[skill_config.name + "_skill"] = skill_module
        loader_contents = [path.name for path in Path(directory).iterdir()]
        skills_packages = list(filter(lambda x: not x.startswith("__"), loader_contents))  # type: ignore
        logger.debug("Processing the following skill package: {}".format(skills_packages))

        skill_context = SkillContext(agent_context)

        handlers_by_id = skill_config.handlers.read_all()
        if len(handlers_by_id) > 0:
            handlers_configurations = list(dict(handlers_by_id).values())
            handlers = Handler.parse_module(os.path.join(directory, "handlers.py"), handlers_configurations, skill_context)
        else:
            handlers = []

        behaviours_by_id = skill_config.behaviours.read_all()
        if len(behaviours_by_id) > 0:
            behaviours_configurations = list(dict(behaviours_by_id).values())
            behaviours = Behaviour.parse_module(os.path.join(directory, "behaviours.py"), behaviours_configurations, skill_context)
        else:
            behaviours = []

        tasks_by_id = skill_config.tasks.read_all()
        if len(tasks_by_id) > 0:
            tasks_configurations = list(dict(tasks_by_id).values())
            tasks = Task.parse_module(os.path.join(directory, "tasks.py"), tasks_configurations, skill_context)
        else:
            tasks = []

        shared_classes_by_id = skill_config.shared_classes.read_all()
        if len(shared_classes_by_id) > 0:
            shared_classes_configurations = list(dict(shared_classes_by_id).values())
            shared_classes_instances = SharedClass.parse_module(directory, shared_classes_configurations, skill_context)
        else:
            shared_classes_instances = []

        skill = Skill(skill_config, skill_context, handlers, behaviours, tasks, shared_classes_instances)
        skill_context._skill = skill

        return skill
Exemplo n.º 9
0
def load_protocol_specification(specification_path: str) -> ProtocolSpecification:
    """
    Load a protocol specification.

    :param specification_path: path to the protocol specification yaml file.
    :return: A ProtocolSpecification object
    """
    config_loader = ConfigLoader(
        "protocol-specification_schema.json", ProtocolSpecification
    )
    protocol_spec = config_loader.load_protocol_specification(open(specification_path))
    return protocol_spec
Exemplo n.º 10
0
def _verify_or_create_private_keys(aea_project_path: Path) -> None:
    """Verify or create private keys."""
    path_to_configuration = aea_project_path / DEFAULT_AEA_CONFIG_FILE
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp_read = path_to_configuration.open(mode="r", encoding="utf-8")
    agent_configuration = agent_loader.load(fp_read)

    for identifier, _value in agent_configuration.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    fetchai_private_key_path = agent_configuration.private_key_paths.read(FETCHAI)
    if fetchai_private_key_path is None:
        _create_fetchai_private_key(
            private_key_file=str(aea_project_path / FETCHAI_PRIVATE_KEY_FILE)
        )
        agent_configuration.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_fet_private_key_path(
                str(aea_project_path / fetchai_private_key_path), exit_on_error=False
            )
        except FileNotFoundError:  # pragma: no cover
            logger.error(
                "File {} for private key {} not found.".format(
                    repr(fetchai_private_key_path), FETCHAI,
                )
            )
            raise

    ethereum_private_key_path = agent_configuration.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_path is None:
        _create_ethereum_private_key(
            private_key_file=str(aea_project_path / ETHEREUM_PRIVATE_KEY_FILE)
        )
        agent_configuration.private_key_paths.update(
            ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE
        )
    else:
        try:
            _try_validate_ethereum_private_key_path(
                str(aea_project_path / ethereum_private_key_path), exit_on_error=False
            )
        except FileNotFoundError:  # pragma: no cover
            logger.error(
                "File {} for private key {} not found.".format(
                    repr(ethereum_private_key_path), ETHEREUM,
                )
            )
            raise

    fp_write = path_to_configuration.open(mode="w", encoding="utf-8")
    agent_loader.dump(agent_configuration, fp_write)
Exemplo n.º 11
0
    def from_dir(cls, directory: str, agent_context: AgentContext) -> "Skill":
        """
        Load a skill from a directory.

        :param directory: the skill directory.
        :param agent_context: the agent's context
        :return: the Skill object.
        :raises Exception: if the parsing failed.
        """
        # check if there is the config file. If not, then return None.
        skill_loader = ConfigLoader("skill-config_schema.json", SkillConfig)
        skill_config = skill_loader.load(
            open(os.path.join(directory, DEFAULT_SKILL_CONFIG_FILE)))
        skill_module = load_agent_component_package("skill", skill_config.name,
                                                    skill_config.author,
                                                    Path(directory))
        add_agent_component_module_to_sys_modules("skill", skill_config.name,
                                                  skill_config.author,
                                                  skill_module)
        loader_contents = [path.name for path in Path(directory).iterdir()]
        skills_packages = list(
            filter(lambda x: not x.startswith("__"),
                   loader_contents))  # type: ignore
        logger.debug("Processing the following skill package: {}".format(
            skills_packages))

        skill_context = SkillContext(agent_context)
        # set the logger of the skill context.
        logger_name = "aea.{}.skills.{}.{}".format(agent_context.agent_name,
                                                   skill_config.author,
                                                   skill_config.name)
        skill_context._logger = logging.getLogger(logger_name)

        handlers_by_id = dict(skill_config.handlers.read_all())
        handlers = Handler.parse_module(os.path.join(directory, "handlers.py"),
                                        handlers_by_id, skill_context)

        behaviours_by_id = dict(skill_config.behaviours.read_all())
        behaviours = Behaviour.parse_module(
            os.path.join(directory, "behaviours.py"),
            behaviours_by_id,
            skill_context,
        )

        models_by_id = dict(skill_config.models.read_all())
        model_instances = Model.parse_module(directory, models_by_id,
                                             skill_context)

        skill = Skill(skill_config, skill_context, handlers, behaviours,
                      model_instances)
        skill_context._skill = skill

        return skill
Exemplo n.º 12
0
    def _add_protocol(
        self,
        protocol_directory: Path,
        allowed_protocols: Optional[Set[PublicId]] = None,
    ):
        """
        Add a protocol. If the protocol is not allowed, it is ignored.

        :param protocol_directory: the directory of the protocol to be added.
        :param allowed_protocols: an optional set of allowed protocols.
                                  If None, every protocol is allowed.
        :return: None
        """
        protocol_name = protocol_directory.name
        config_loader = ConfigLoader("protocol-config_schema.json",
                                     ProtocolConfig)
        protocol_config = config_loader.load(
            open(protocol_directory / DEFAULT_PROTOCOL_CONFIG_FILE))
        protocol_public_id = PublicId(protocol_config.author,
                                      protocol_config.name,
                                      protocol_config.version)
        if (allowed_protocols is not None
                and protocol_public_id not in allowed_protocols):
            logger.debug(
                "Ignoring protocol {}, not declared in the configuration file."
                .format(protocol_public_id))
            return

        # get the serializer
        serialization_spec = importlib.util.spec_from_file_location(
            "serialization", protocol_directory / "serialization.py")
        serialization_module = importlib.util.module_from_spec(
            serialization_spec)
        serialization_spec.loader.exec_module(
            serialization_module)  # type: ignore
        classes = inspect.getmembers(serialization_module, inspect.isclass)
        serializer_classes = list(
            filter(lambda x: re.match("\\w+Serializer", x[0]), classes))
        serializer_class = serializer_classes[0][1]

        logger.debug(
            "Found serializer class {serializer_class} for protocol {protocol_name}"
            .format(serializer_class=serializer_class,
                    protocol_name=protocol_name))
        serializer = serializer_class()

        # instantiate the protocol
        protocol = Protocol(protocol_config.public_id, serializer,
                            protocol_config)
        self.register(protocol_public_id, protocol)
Exemplo n.º 13
0
def _verify_or_create_private_keys(ctx: Context) -> None:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = path.open(mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, _value in aea_conf.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    fetchai_private_key_path = aea_conf.private_key_paths.read(FETCHAI)
    if fetchai_private_key_path is None:
        _create_fetchai_private_key()
        aea_conf.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_fet_private_key_path(fetchai_private_key_path)
        except FileNotFoundError:  # pragma: no cover
            logger.error("File {} for private key {} not found.".format(
                repr(fetchai_private_key_path),
                FETCHAI,
            ))
            sys.exit(1)

    ethereum_private_key_path = aea_conf.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_path is None:
        _create_ethereum_private_key()
        aea_conf.private_key_paths.update(ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_ethereum_private_key_path(ethereum_private_key_path)
        except FileNotFoundError:  # pragma: no cover
            logger.error("File {} for private key {} not found.".format(
                repr(ethereum_private_key_path),
                ETHEREUM,
            ))
            sys.exit(1)

    # update aea config
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    fp = path.open(mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    ctx.agent_config = aea_conf
Exemplo n.º 14
0
def verify_or_create_private_keys(
    aea_project_path: Path,
    exit_on_error: bool = True,
) -> AgentConfig:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path_to_aea_config = aea_project_path / DEFAULT_AEA_CONFIG_FILE
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = path_to_aea_config.open(mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, _value in aea_conf.private_key_paths.read_all():
        if identifier not in crypto_registry.supported_ids:  # pragma: nocover
            ValueError("Unsupported identifier in private key paths.")

    for identifier, private_key_path in IDENTIFIER_TO_KEY_FILES.items():
        config_private_key_path = aea_conf.private_key_paths.read(identifier)
        if config_private_key_path is None:
            if identifier == aea_conf.default_ledger:  # pragma: nocover
                create_private_key(
                    identifier,
                    private_key_file=str(aea_project_path / private_key_path),
                )
                aea_conf.private_key_paths.update(identifier, private_key_path)
        else:
            try:
                try_validate_private_key_path(
                    identifier,
                    str(aea_project_path / private_key_path),
                    exit_on_error=exit_on_error,
                )
            except FileNotFoundError:  # pragma: no cover
                raise ValueError(
                    "File {} for private key {} not found.".format(
                        repr(private_key_path),
                        identifier,
                    ))

    # update aea config
    fp = path_to_aea_config.open(mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    return aea_conf
Exemplo n.º 15
0
    def from_dir(cls, directory: str, agent_context: AgentContext) -> Optional['Skill']:
        """
        Load a skill from a directory.

        :param directory: the skill
        :param agent_context: the agent's context
        :return: the Skill object. None if the parsing failed.
        """
        # check if there is the config file. If not, then return None.
        skill_loader = ConfigLoader("skill-config_schema.json", SkillConfig)
        skill_config = skill_loader.load(open(os.path.join(directory, DEFAULT_SKILL_CONFIG_FILE)))
        if skill_config is None:
            return None

        skills_spec = importlib.util.spec_from_file_location(skill_config.name, os.path.join(directory, "__init__.py"))
        if skills_spec is None:
            logger.warning("No skill found.")
            return None

        skill_module = importlib.util.module_from_spec(skills_spec)
        sys.modules[skill_config.name + "_skill"] = skill_module
        skills_packages = list(filter(lambda x: not x.startswith("__"), skills_spec.loader.contents()))  # type: ignore
        logger.debug("Processing the following skill package: {}".format(skills_packages))

        skill_context = SkillContext(agent_context)

        handlers_configurations = list(dict(skill_config.handlers.read_all()).values())
        handlers = Handler.parse_module(os.path.join(directory, "handlers.py"), handlers_configurations, skill_context)
        behaviours_configurations = list(dict(skill_config.behaviours.read_all()).values())
        behaviours = Behaviour.parse_module(os.path.join(directory, "behaviours.py"), behaviours_configurations, skill_context)
        tasks_configurations = list(dict(skill_config.tasks.read_all()).values())
        tasks = Task.parse_module(os.path.join(directory, "tasks.py"), tasks_configurations, skill_context)
        shared_classes_configurations = list(dict(skill_config.shared_classes.read_all()).values())
        shared_classes_instances = SharedClass.parse_module(directory, shared_classes_configurations, skill_context)

        skill = Skill(skill_config, skill_context, handlers, behaviours, tasks, shared_classes_instances)
        skill_context._skill = skill

        return skill
Exemplo n.º 16
0
def _verify_or_create_private_keys(ctx: Context) -> None:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = open(str(path), mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, value in aea_conf.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    default_private_key_config = aea_conf.private_key_paths.read(DEFAULT)
    if default_private_key_config is None:
        _create_default_private_key()
        default_private_key_config = PrivateKeyPathConfig(
            DEFAULT, DEFAULT_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(default_private_key_config.ledger,
                                          default_private_key_config)
    else:
        default_private_key_config = cast(PrivateKeyPathConfig,
                                          default_private_key_config)
        try:
            _try_validate_private_key_pem_path(default_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(default_private_key_config.path),
                default_private_key_config.ledger))
            sys.exit(1)

    fetchai_private_key_config = aea_conf.private_key_paths.read(FETCHAI)
    if fetchai_private_key_config is None:
        _create_fetchai_private_key()
        fetchai_private_key_config = PrivateKeyPathConfig(
            FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(fetchai_private_key_config.ledger,
                                          fetchai_private_key_config)
    else:
        fetchai_private_key_config = cast(PrivateKeyPathConfig,
                                          fetchai_private_key_config)
        try:
            _try_validate_fet_private_key_path(fetchai_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(fetchai_private_key_config.path),
                fetchai_private_key_config.ledger))
            sys.exit(1)

    ethereum_private_key_config = aea_conf.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_config is None:
        _create_ethereum_private_key()
        ethereum_private_key_config = PrivateKeyPathConfig(
            ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(ethereum_private_key_config.ledger,
                                          ethereum_private_key_config)
    else:
        ethereum_private_key_config = cast(PrivateKeyPathConfig,
                                           ethereum_private_key_config)
        try:
            _try_validate_ethereum_private_key_path(
                ethereum_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(ethereum_private_key_config.path),
                ethereum_private_key_config.ledger))
            sys.exit(1)

    # update aea config
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    fp = open(str(path), mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    ctx.agent_config = aea_conf
Exemplo n.º 17
0
def _generate_item(ctx: Context, item_type, specification_path):
    """Generate an item based on a specification and add it to the configuration file and agent."""
    # # check protocol buffer compiler is installed
    # res = shutil.which("protoc")
    # if res is None:
    #     print(
    #         "Please install protocol buffer first! See the following link: https://developers.google.com/protocol-buffers/"
    #     )
    #     sys.exit(1)

    # Get existing items
    existing_id_list = getattr(ctx.agent_config, "{}s".format(item_type))
    existing_item_list = [public_id.name for public_id in existing_id_list]

    item_type_plural = item_type + "s"

    # Load item specification yaml file
    try:
        config_loader = ConfigLoader(
            "protocol-specification_schema.json", ProtocolSpecification
        )
        protocol_spec = config_loader.load_protocol_specification(
            open(specification_path)
        )
    except Exception as e:
        logger.exception(e)
        sys.exit(1)

    protocol_directory_path = os.path.join(
        ctx.cwd, item_type_plural, protocol_spec.name
    )

    # Check if we already have an item with the same name in the agent config
    logger.debug(
        "{} already supported by the agent: {}".format(
            item_type_plural, existing_item_list
        )
    )
    if protocol_spec.name in existing_item_list:
        logger.error(
            "A {} with name '{}' already exists. Aborting...".format(
                item_type, protocol_spec.name
            )
        )
        sys.exit(1)
    # Check if we already have a directory with the same name in the resource directory (e.g. protocols) of the agent's directory
    if os.path.exists(protocol_directory_path):
        logger.error(
            "A directory with name '{}' already exists. Aborting...".format(
                protocol_spec.name
            )
        )
        sys.exit(1)

    try:
        agent_name = ctx.agent_config.agent_name
        click.echo(
            "Generating {} '{}' and adding it to the agent '{}'...".format(
                item_type, protocol_spec.name, agent_name
            )
        )

        output_path = os.path.join(ctx.cwd, item_type_plural)
        protocol_generator = ProtocolGenerator(protocol_spec, output_path)
        protocol_generator.generate()

        # Add the item to the configurations
        logger.debug(
            "Registering the {} into {}".format(item_type, DEFAULT_AEA_CONFIG_FILE)
        )
        existing_id_list.add(PublicId("fetchai", protocol_spec.name, DEFAULT_VERSION))
        ctx.agent_loader.dump(
            ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w")
        )
    except FileExistsError:
        logger.error(
            "A {} with this name already exists. Please choose a different name and try again.".format(
                item_type
            )
        )
        sys.exit(1)
    except Exception as e:
        logger.exception(e)
        shutil.rmtree(
            os.path.join(item_type_plural, protocol_spec.name), ignore_errors=True
        )
        sys.exit(1)
Exemplo n.º 18
0
def _generate_item(click_context, item_type, specification_path):
    """Generate an item based on a specification and add it to the configuration file and agent."""
    # check protocol buffer compiler is installed
    ctx = cast(Context, click_context.obj)
    res = shutil.which("protoc")
    if res is None:
        raise click.ClickException(
            "Please install protocol buffer first! See the following link: https://developers.google.com/protocol-buffers/"
        )

    # check black code formatter is installed
    res = shutil.which("black")
    if res is None:
        raise click.ClickException(
            "Please install black code formater first! See the following link: https://black.readthedocs.io/en/stable/installation_and_usage.html"
        )

    # Get existing items
    existing_id_list = getattr(ctx.agent_config, "{}s".format(item_type))
    existing_item_list = [public_id.name for public_id in existing_id_list]

    item_type_plural = item_type + "s"

    # Load item specification yaml file
    try:
        config_loader = ConfigLoader("protocol-specification_schema.json",
                                     ProtocolSpecification)
        protocol_spec = config_loader.load_protocol_specification(
            open(specification_path))
    except Exception as e:
        raise click.ClickException(str(e))

    protocol_directory_path = os.path.join(ctx.cwd, item_type_plural,
                                           protocol_spec.name)

    # Check if we already have an item with the same name in the agent config
    logger.debug("{} already supported by the agent: {}".format(
        item_type_plural, existing_item_list))
    if protocol_spec.name in existing_item_list:
        raise click.ClickException(
            "A {} with name '{}' already exists. Aborting...".format(
                item_type, protocol_spec.name))
    # Check if we already have a directory with the same name in the resource directory (e.g. protocols) of the agent's directory
    if os.path.exists(protocol_directory_path):
        raise click.ClickException(
            "A directory with name '{}' already exists. Aborting...".format(
                protocol_spec.name))

    ctx.clean_paths.append(protocol_directory_path)
    try:
        agent_name = ctx.agent_config.agent_name
        click.echo(
            "Generating {} '{}' and adding it to the agent '{}'...".format(
                item_type, protocol_spec.name, agent_name))

        output_path = os.path.join(ctx.cwd, item_type_plural)
        protocol_generator = ProtocolGenerator(protocol_spec, output_path)
        protocol_generator.generate()

        # Add the item to the configurations
        logger.debug("Registering the {} into {}".format(
            item_type, DEFAULT_AEA_CONFIG_FILE))
        existing_id_list.add(
            PublicId("fetchai", protocol_spec.name, protocol_spec.version))
        ctx.agent_loader.dump(
            ctx.agent_config,
            open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w"))
    except FileExistsError:
        raise click.ClickException(
            "A {} with this name already exists. Please choose a different name and try again."
            .format(item_type))
    except ProtocolSpecificationParseError as e:
        raise click.ClickException(
            "The following error happened while parsing the protocol specification: "
            + str(e))
    except Exception as e:
        raise click.ClickException(
            "There was an error while generating the protocol. The protocol is NOT generated. Exception: "
            + str(e))

    # Run black code formatting
    try:
        subp = subprocess.Popen(  # nosec
            [
                sys.executable,
                "-m",
                "black",
                os.path.join(item_type_plural, protocol_spec.name),
                "--quiet",
            ])
        subp.wait(10.0)
    finally:
        poll = subp.poll()
        if poll is None:  # pragma: no cover
            subp.terminate()
            subp.wait(5)

    _fingerprint_item(click_context, "protocol", protocol_spec.public_id)