Skip to content

Commit

Permalink
feat: proper bot bots and python file treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed Oct 25, 2024
1 parent 2f65af6 commit 1b9fe60
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,36 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot):
asyncio.run(runner.run())


def build_helper(docker_fn: str, dockerfile_c: str):
dockerfile_path = Path.cwd() / ".silverback-images" / docker_fn
dockerfile_path.parent.mkdir(exist_ok=True)
dockerfile_path.write_text(dockerfile_c.strip() + "\n")
click.echo(f"Generated {dockerfile_path}")



@cli.command(section="Local Commands")
@click.option("--generate", is_flag=True, default=False)
@click.argument("path", required=False, type=str, default="bots")
def build(generate, path):
"""Generate Dockerfiles and build bot images"""
if generate:
if not (path := Path.cwd() / path).exists():
if not (path := Path.cwd() / path).exists() and not (path := Path.cwd() / "bot").exists() and not (path := Path.cwd() / "bot.py").exists():
raise FileNotFoundError(
f"The bots directory '{path}' does not exist. "
"You should have a `{path}/` folder in the root of your project."
f"The bots directory '{path}', 'bot/' and 'bot.py' does not exist in your path. "
f"You should have a '{path}/' or 'bot/' folder, or a 'bot.py' file in the root of your project."
)
files = {file for file in path.iterdir() if file.is_file()}
if path.is_file():
dockerfile_content = DOCKERFILE_CONTENT
docker_filename = f"Dockerfile.{path.parent.name}-bot"
dockerfile_content += f"COPY {path.name}/ /app/bot\n"
build_helper(docker_filename, dockerfile_content)
return

files = sorted({file for file in path.iterdir() if file.is_file()}, reverse=True)
bots = []
for file in files:
if "__init__" in file.name:
if file.name == "__init__.py" or file.name == "bot.py":
bots = [file]
break
bots.append(file)
Expand All @@ -158,16 +173,13 @@ def build(generate, path):
dockerfile_content += "COPY ape-config.yaml .\n"
dockerfile_content += "RUN ape plugins install -U .\n"

if "__init__" in bot.name:
docker_filename = f"Dockerfile.{bot.parent.name}"
if bot.name == "__init__.py" or bot.name == "bot.py":
docker_filename = f"Dockerfile.{bot.parent.parent.name}-bot"
dockerfile_content += f"COPY {path.name}/ /app/bot\n"
else:
docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}"
docker_filename = f"Dockerfile.{bot.name.replace('.py', '')}-bot"
dockerfile_content += f"COPY {path.name}/{bot.name} /app/bot.py\n"
dockerfile_path = Path.cwd() / ".silverback-images" / docker_filename
dockerfile_path.parent.mkdir(exist_ok=True)
dockerfile_path.write_text(dockerfile_content.strip() + "\n")
click.echo(f"Generated {dockerfile_path}")
build_helper(docker_filename, dockerfile_content)
return

if not (path := Path.cwd() / ".silverback-images").exists():
Expand Down

0 comments on commit 1b9fe60

Please sign in to comment.