def test_from_project(self): """Test builder set from project dir.""" self.expected_input_file = "custom_input_file" self.expected_output_file = "custom_output_file" self._add_stub_connection_config() with pytest.raises( AEAEnforceError, match=r"Component \(protocol, some_author/non_existing_package:0.1.0\) not declared in the agent configuration.", ): with cd(self._get_cwd()): AEABuilder.from_aea_project(Path(self._get_cwd()))
def test_install_dependency(self): """Test dependencies installed.""" package_name = "async_generator" dependency = Dependency(package_name, "==1.10") sys.modules.pop(package_name, None) run_install_subprocess( [sys.executable, "-m", "pip", "uninstall", package_name, "-y"] ) try: import_module(package_name) raise Exception("should not be raised") except ModuleNotFoundError: pass builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with patch( "aea.aea_builder._DependenciesManager.pypi_dependencies", {"package_name": dependency}, ): builder.install_pypi_dependencies() import_module(package_name) sys.modules.pop(package_name) run_install_subprocess( [sys.executable, "-m", "pip", "uninstall", package_name, "-y"] ) try: import_module(package_name) raise Exception("should not be raised") except ModuleNotFoundError: pass
def test_check_dependencies_correct(self): """Test dependencies properly listed.""" self.run_cli_command( "add", "--local", "connection", "fetchai/http_client", cwd=self._get_cwd() ) builder = AEABuilder.from_aea_project(Path(self._get_cwd())) assert "aiohttp" in builder._package_dependency_manager.pypi_dependencies
def _launch_threads(click_context: click.Context, agents: List[Path]) -> int: """ Launch many agents, multithreaded. :param agents: the click context. :param agents: list of paths to agent projects. :return: exit status """ aeas = [] # type: List[AEA] for agent_directory in agents: with cd(agent_directory): aeas.append(AEABuilder.from_aea_project(".").build()) threads = [Thread(target=agent.start) for agent in aeas] for t in threads: t.start() try: while sum([t.is_alive() for t in threads]) != 0: # exit when all threads are not alive. # done to avoid block on joins for t in threads: t.join(0.1) except KeyboardInterrupt: logger.info("Keyboard interrupt detected.") finally: for idx, agent in enumerate(aeas): if not agent.liveness.is_stopped: agent.stop() threads[idx].join() logger.info("Agent {} has been stopped.".format(agent.name)) return 0
def _build_aea(self) -> AEA: """Build an AEA.""" builder = AEABuilder.from_aea_project(self._get_cwd()) builder.set_storage_uri("sqlite://some_file.db") aea = builder.build() aea.runtime._threaded = True return aea
def _launch_threads(agents: List[Path]) -> int: """ Launch many agents, multithreaded. :param click_context: the click context. :param agents: list of paths to agent projects. :return: exit status """ aeas = [] # type: List[AEA] for agent_directory in agents: with cd(agent_directory): aeas.append(AEABuilder.from_aea_project(".").build()) runner = AEARunner(agents=aeas, mode="threaded", fail_policy=ExecutorExceptionPolicies.log_only) try: runner.start(threaded=True) runner.join_thread( ) # for some reason on windows and python 3.7/3.7 keyboard interuption exception gets lost so run in threaded mode to catch keyboard interruped except KeyboardInterrupt: logger.info("Keyboard interrupt detected.") finally: runner.stop() return runner.num_failed
def setup(self): """Set up the test.""" self.builder = AEABuilder.from_aea_project(Path(self._get_cwd())) self.component_id = "component_id" # add project-wide build entrypoint self.script_path = Path("script.py") self.builder._build_entrypoint = str(self.script_path)
def test_from_project(self): """Test builder set from project dir.""" self._add_dummy_skill_config() builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with cd(self._get_cwd()): aea = builder.build() dummy_skill = aea.resources.get_skill(DUMMY_SKILL_PUBLIC_ID) assert dummy_skill is None, "Shouldn't have found the skill in Resources."
def _build_aea(connection_ids: Optional[List[PublicId]], skip_consistency_check: bool) -> AEA: try: builder = AEABuilder.from_aea_project( Path("."), skip_consistency_check=skip_consistency_check) aea = builder.build(connection_ids=connection_ids) return aea except Exception as e: raise click.ClickException(str(e))
def load_agent(agent_dir: Union[PathLike, str]) -> AEA: """ Load AEA from directory. :param agent_dir: agent configuration directory :return: AEA instance """ with cd(agent_dir): return AEABuilder.from_aea_project(".").build()
def _build_aea(connection_ids: Optional[List[PublicId]], skip_consistency_check: bool) -> AEA: try: builder = AEABuilder.from_aea_project( Path("."), skip_consistency_check=skip_consistency_check) aea = builder.build(connection_ids=connection_ids) return aea except AEAPackageLoadingError as e: # pragma: nocover raise click.ClickException("Package loading error: {}".format(str(e))) except Exception as e: raise click.ClickException(str(e))
def test_from_project(self): """Test builder set from project dir.""" self._add_dummy_skill_config() self.run_cli_command("issue-certificates", cwd=self._get_cwd()) builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with cd(self._get_cwd()): builder.call_all_build_entrypoints() aea = builder.build() dummy_skill = aea.resources.get_skill(DUMMY_SKILL_PUBLIC_ID) assert dummy_skill is None, "Shouldn't have found the skill in Resources."
def _build_aea(connection_ids: Optional[List[PublicId]], skip_consistency_check: bool) -> AEA: try: builder = AEABuilder.from_aea_project( Path("."), skip_consistency_check=skip_consistency_check) aea = builder.build(connection_ids=connection_ids) return aea except Exception as e: # TODO use an ad-hoc exception class for predictable errors # all the other exceptions should be logged with logger.exception logger.error(str(e)) sys.exit(1)
def _build_aea(connection_ids: Optional[List[PublicId]], skip_consistency_check: bool) -> AEA: try: builder = AEABuilder.from_aea_project( Path("."), skip_consistency_check=skip_consistency_check) aea = builder.build(connection_ids=connection_ids) return aea except AEAPackageLoadingError as e: # pragma: nocover raise click.ClickException("Package loading error: {}".format(str(e))) except Exception as e: # TODO use an ad-hoc exception class for predictable errors # all the other exceptions should be logged with ClickException raise click.ClickException(str(e))
def test_from_project(self): """Test builder set from project dir.""" self.expected_input_file = "custom_input_file" self.expected_output_file = "custom_output_file" self._add_stub_connection_config() builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with cd(self._get_cwd()): aea = builder.build() assert aea.name == self.agent_name stub_connection_id = StubConnection.connection_id stub_connection = aea.resources.get_connection(stub_connection_id) assert stub_connection.configuration.config == dict( input_file=self.expected_input_file, output_file=self.expected_output_file )
def test_from_project(self): """Test builder set from project dir.""" self.new_behaviour_args = {"behaviour_arg_1": 42} self.new_handler_args = {"handler_arg_1": 42} self.new_model_args = {"model_arg_1": 42} self._add_dummy_skill_config() builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with cd(self._get_cwd()): aea = builder.build() dummy_skill = aea.resources.get_skill(DUMMY_SKILL_PUBLIC_ID) dummy_behaviour = dummy_skill.behaviours["dummy"] assert dummy_behaviour.config == {"behaviour_arg_1": 42, "behaviour_arg_2": "2"} dummy_handler = dummy_skill.handlers["dummy"] assert dummy_handler.config == {"handler_arg_1": 42, "handler_arg_2": "2"} dummy_model = dummy_skill.models["dummy"] assert dummy_model.config == {"model_arg_1": 42, "model_arg_2": "2"}
def build_aea(skip_consistency_check: bool) -> None: """ Build an AEA. That is, run the 'build entrypoint' script of each AEA package of the project. :param skip_consistency_check: the skip consistency check boolean. :return: None """ try: builder = AEABuilder.from_aea_project( Path("."), skip_consistency_check=skip_consistency_check, create_keys=False, ) builder.call_all_build_entrypoints() except Exception as e: raise click.ClickException(str(e)) click.echo("Build completed!")
def test_from_project(self): """Test builder set from project dir.""" builder = AEABuilder.from_aea_project(Path(self._get_cwd())) with cd(self._get_cwd()): aea = builder.build() assert aea.name == self.agent_name