From 056edb14434c33d08ff19bbf1dc2b863953df20c Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Sun, 3 Sep 2023 19:29:22 +0530 Subject: [PATCH 1/6] added zip function in textbase cli tool --- textbase/textbase_cli.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index 76c75339..71c3a688 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -7,6 +7,8 @@ from yaspin import yaspin import importlib.resources import re +import zipfile + CLOUD_URL = "https://us-east1-chat-agents.cloudfunctions.net/deploy-from-cli" UPLOAD_URL = "https://us-east1-chat-agents.cloudfunctions.net/upload-file" @@ -35,6 +37,25 @@ def test(path): process_local_ui.kill() click.secho("Server stopped.", fg='red') +################################################################################################################# + +@cli.command() +def compress(): + click.echo(click.style(f"Creating zip file for deployment", fg='green')) + + files_to_zip = ['requirements.txt', 'main.py'] + output_zip_filename = 'deploy.zip' + with zipfile.ZipFile(output_zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: + for file_to_zip in files_to_zip: + # Check if the file exists in the current directory + if os.path.exists(file_to_zip): + # Add the file to the zip archive + zipf.write(file_to_zip, os.path.basename(file_to_zip)) + else: + click.echo(click.style(f"Warning: {file_to_zip} not found in the current directory.", fg='red')) + + click.echo(click.style(f"Files {', '.join(files_to_zip)} have been zipped to {output_zip_filename}", fg='green')) + ################################################################################################################# def validate_bot_name(ctx, param, value): @@ -44,6 +65,7 @@ def validate_bot_name(ctx, param, value): raise click.BadParameter(error_message) return value + @cli.command() @click.option("--path", prompt="Path to the zip folder", required=True) @click.option("--bot_name", prompt="Name of the bot", required=True, callback=validate_bot_name) From 854102c11e9b8215003b7cd2820507efb3f09141 Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Wed, 6 Sep 2023 17:30:38 +0530 Subject: [PATCH 2/6] Added file check such that deploy.zip won't be created if either of main.py or requirements.txt is not present. Added --path flag to take the path of both the files from user. --- textbase/textbase_cli.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index 71c3a688..0f096354 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -38,24 +38,33 @@ def test(path): click.secho("Server stopped.", fg='red') ################################################################################################################# - +def fileExist(main_path,requirements_path): + if not os.path.exists(main_path): + click.echo(click.style(f"Error: main.py not found in the mentioned directory.", fg='red')) + return False + if not os.path.exists(requirements_path): + click.echo(click.style(f"Error: reuirements.txt not found in the mentioned directory.", fg='red')) + return False + + return True + @cli.command() -def compress(): +@click.option("--main_path", prompt="Path to the main.py file", required=True) +@click.option("--requirements_path", prompt="Path to the requirements.txt file", required=True) +def compress(main_path,requirements_path): click.echo(click.style(f"Creating zip file for deployment", fg='green')) - - files_to_zip = ['requirements.txt', 'main.py'] + # files_to_zip = ['requirements.txt', 'main.py'] + files_to_zip = [main_path, requirements_path] + files_exist = all(os.path.exists(file) for file in files_to_zip) output_zip_filename = 'deploy.zip' - with zipfile.ZipFile(output_zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: - for file_to_zip in files_to_zip: - # Check if the file exists in the current directory - if os.path.exists(file_to_zip): + + if fileExist(main_path=main_path,requirements_path=requirements_path): + with zipfile.ZipFile(output_zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: + for file_to_zip in files_to_zip: # Add the file to the zip archive zipf.write(file_to_zip, os.path.basename(file_to_zip)) - else: - click.echo(click.style(f"Warning: {file_to_zip} not found in the current directory.", fg='red')) - - click.echo(click.style(f"Files {', '.join(files_to_zip)} have been zipped to {output_zip_filename}", fg='green')) + click.echo(click.style(f"Files {', '.join(files_to_zip)} have been zipped to {output_zip_filename}", fg='green')) ################################################################################################################# def validate_bot_name(ctx, param, value): From 995812cfefe27024d2535321864ad989c3f7e457 Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Wed, 6 Sep 2023 17:42:39 +0530 Subject: [PATCH 3/6] resolved merge conflicts --- textbase/textbase_cli.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index bc7161a9..9eb7b8c4 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -8,12 +8,8 @@ from yaspin import yaspin import importlib.resources import re -<<<<<<< HEAD import zipfile -======= -import urllib.parse ->>>>>>> 52506a9621516d884223e0945c663b262b3e0466 CLOUD_URL = "https://us-east1-chat-agents.cloudfunctions.net/deploy-from-cli" UPLOAD_URL = "https://us-east1-chat-agents.cloudfunctions.net/upload-file" From b0bf5fbab38b1bdf3c6c9846bc029685ecfe1234 Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Wed, 6 Sep 2023 17:46:27 +0530 Subject: [PATCH 4/6] removed unused code. --- textbase/textbase_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index 9eb7b8c4..1e9efd49 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -75,7 +75,6 @@ def fileExist(main_path,requirements_path): @click.option("--requirements_path", prompt="Path to the requirements.txt file", required=True) def compress(main_path,requirements_path): click.echo(click.style(f"Creating zip file for deployment", fg='green')) - # files_to_zip = ['requirements.txt', 'main.py'] files_to_zip = [main_path, requirements_path] files_exist = all(os.path.exists(file) for file in files_to_zip) output_zip_filename = 'deploy.zip' From e54e0ad01855d9e386d26b02f07f063433b5bd10 Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Thu, 7 Sep 2023 20:19:28 +0530 Subject: [PATCH 5/6] modified compress command to take directory path, check if main.py and requirements.txt is in path and compress everything from directory into deploy.zip --- textbase/textbase_cli.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index 1e9efd49..fce3be7d 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -60,32 +60,28 @@ def test(path, port): click.secho("Server stopped.", fg='red') ################################################################################################################# -def fileExist(main_path,requirements_path): - if not os.path.exists(main_path): - click.echo(click.style(f"Error: main.py not found in the mentioned directory.", fg='red')) +def fileExist(path): + if not os.path.exists(os.path.join(path, "main.py")): + click.echo(click.style(f"Error: main.py not found in {path} directory.", fg='red')) return False - if not os.path.exists(requirements_path): - click.echo(click.style(f"Error: reuirements.txt not found in the mentioned directory.", fg='red')) + if not os.path.exists(os.path.join(path, "requirements.txt")): + click.echo(click.style(f"Error: requirements.txt not found in {path} directory.", fg='red')) return False - return True @cli.command() -@click.option("--main_path", prompt="Path to the main.py file", required=True) -@click.option("--requirements_path", prompt="Path to the requirements.txt file", required=True) -def compress(main_path,requirements_path): +@click.option("--path", prompt="Path to the directory containing main.py and requirements.txt file", required=True) +def compress(path): click.echo(click.style(f"Creating zip file for deployment", fg='green')) - files_to_zip = [main_path, requirements_path] - files_exist = all(os.path.exists(file) for file in files_to_zip) output_zip_filename = 'deploy.zip' - - if fileExist(main_path=main_path,requirements_path=requirements_path): + if fileExist(path): with zipfile.ZipFile(output_zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: - for file_to_zip in files_to_zip: + for root, _, files in os.walk(path): + for file in files: + file_path = os.path.join(root, file) # Add the file to the zip archive - zipf.write(file_to_zip, os.path.basename(file_to_zip)) - - click.echo(click.style(f"Files {', '.join(files_to_zip)} have been zipped to {output_zip_filename}", fg='green')) + zipf.write(file_path, os.path.relpath(file_path, path)) + click.echo(click.style(f"Files have been zipped to {output_zip_filename}", fg='green')) ################################################################################################################# def validate_bot_name(ctx, param, value): From ffaab83a2722bd99e92ac8cf419f6e9cd0025504 Mon Sep 17 00:00:00 2001 From: Etancher <30360093+jahanvir@users.noreply.github.com> Date: Thu, 7 Sep 2023 21:47:44 +0530 Subject: [PATCH 6/6] added the condition to avoid deploy.zip being created inside deploy.zip --- textbase/textbase_cli.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/textbase/textbase_cli.py b/textbase/textbase_cli.py index fce3be7d..19baaa84 100644 --- a/textbase/textbase_cli.py +++ b/textbase/textbase_cli.py @@ -78,9 +78,10 @@ def compress(path): with zipfile.ZipFile(output_zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, _, files in os.walk(path): for file in files: - file_path = os.path.join(root, file) - # Add the file to the zip archive - zipf.write(file_path, os.path.relpath(file_path, path)) + if file != output_zip_filename: + file_path = os.path.join(root, file) + # Add the file to the zip archive + zipf.write(file_path, os.path.relpath(file_path, path)) click.echo(click.style(f"Files have been zipped to {output_zip_filename}", fg='green')) #################################################################################################################