def create(click_context, agent_name, author, local): """Create an agent.""" try: _check_is_parent_folders_are_aea_projects_recursively() except Exception: raise click.ClickException( "The current folder is already an AEA project. Please move to the parent folder." ) if author is not None: if local: do_init(author, False, False) else: raise click.ClickException( "Author is not set up. Please use 'aea init' to initialize.") config = get_or_create_cli_config() set_author = config.get(AUTHOR_KEY, None) if set_author is None: raise click.ClickException( "The AEA configurations are not initialized. Uses `aea init` before continuing or provide optional argument `--author`." ) if Path(agent_name).exists(): raise click.ClickException("Directory already exist. Aborting...") click.echo("Initializing AEA project '{}'".format(agent_name)) click.echo("Creating project directory './{}'".format(agent_name)) _create_aea(click_context, agent_name, set_author, local)
def create(click_context, agent_name, author, local): """Create an agent.""" try: _check_is_parent_folders_are_aea_projects_recursively() except Exception: logger.error( "The current folder is already an AEA project. Please move to the parent folder." ) sys.exit(1) if author is not None: if local: do_init(author, False, False) else: raise click.ClickException( "Author is not set up. Please use 'aea init' to initialize.") config = _get_or_create_cli_config() set_author = config.get(AUTHOR, None) if set_author is None: click.echo( "The AEA configurations are not initialized. Uses `aea init` before continuing or provide optional argument `--author`." ) sys.exit(1) ctx = cast(Context, click_context.obj) path = Path(agent_name) click.echo("Initializing AEA project '{}'".format(agent_name)) click.echo("Creating project directory './{}'".format(agent_name)) # create the agent's directory try: path.mkdir(exist_ok=False) # set up packages directories. _setup_package_folder(Path(agent_name, "protocols")) _setup_package_folder(Path(agent_name, "contracts")) _setup_package_folder(Path(agent_name, "connections")) _setup_package_folder(Path(agent_name, "skills")) # set up a vendor directory Path(agent_name, "vendor").mkdir(exist_ok=False) Path(agent_name, "vendor", "__init__.py").touch(exist_ok=False) # create a config file inside it click.echo("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) config_file = open(os.path.join(agent_name, DEFAULT_AEA_CONFIG_FILE), "w") agent_config = AgentConfig( agent_name=agent_name, aea_version=aea.__version__, author=set_author, version=DEFAULT_VERSION, license=DEFAULT_LICENSE, registry_path=os.path.join("..", DEFAULT_REGISTRY_PATH), description="", ) agent_config.default_connection = DEFAULT_CONNECTION agent_config.default_ledger = DEFAULT_LEDGER ctx.agent_loader.dump(agent_config, config_file) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name click.echo("Adding default packages ...") if local: ctx.set_config("is_local", True) _add_item(click_context, "connection", DEFAULT_CONNECTION) _add_item(click_context, "skill", DEFAULT_SKILL) except OSError: logger.error("Directory already exist. Aborting...") sys.exit(1) except ValidationError as e: logger.error(str(e)) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1) except Exception as e: logger.exception(e) shutil.rmtree(agent_name, ignore_errors=True) sys.exit(1)
def create_aea( ctx: Context, agent_name: str, local: bool, author: Optional[str] = None, empty: bool = False, ) -> None: """ Create AEA project. :param ctx: Context object. :param local: boolean flag for local folder usage. :param agent_name: agent name. :param author: optional author name (valid with local=True only). :param empty: optional boolean flag for skip adding default dependencies. :return: None :raises: ClickException if an error occured. """ try: _check_is_parent_folders_are_aea_projects_recursively() except Exception: raise click.ClickException( "The current folder is already an AEA project. Please move to the parent folder." ) if author is not None: if local: do_init(author, False, False) else: raise click.ClickException( "Author is not set up. Please use 'aea init' to initialize.") config = get_or_create_cli_config() set_author = config.get(AUTHOR_KEY, None) if set_author is None: raise click.ClickException( "The AEA configurations are not initialized. Uses `aea init` before continuing or provide optional argument `--author`." ) if Path(agent_name).exists(): raise click.ClickException("Directory already exist. Aborting...") click.echo("Initializing AEA project '{}'".format(agent_name)) click.echo("Creating project directory './{}'".format(agent_name)) path = Path(agent_name) ctx.clean_paths.append(str(path)) # we have already checked that the directory does not exist. path.mkdir(exist_ok=False) try: # set up packages directories. _setup_package_folder(Path(agent_name, "protocols")) _setup_package_folder(Path(agent_name, "contracts")) _setup_package_folder(Path(agent_name, "connections")) _setup_package_folder(Path(agent_name, "skills")) # set up a vendor directory Path(agent_name, "vendor").mkdir(exist_ok=False) Path(agent_name, "vendor", "__init__.py").touch(exist_ok=False) # create a config file inside it click.echo("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) agent_config = _crete_agent_config(ctx, agent_name, set_author) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name if not empty: click.echo("Adding default packages ...") if local: ctx.set_config("is_local", True) add_item(ctx, "connection", DEFAULT_CONNECTION) add_item(ctx, "skill", DEFAULT_SKILL) except Exception as e: raise click.ClickException(str(e))