From 08d044b24765dd26621d9c90bc2f6b02e87d6cc7 Mon Sep 17 00:00:00 2001 From: Choco Date: Thu, 3 Aug 2023 14:19:48 +0800 Subject: [PATCH] Optimized update process and reminder --- README.md | 15 +++++++- update.py | 111 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 84 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index a3db67a..aeee6d4 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,17 @@ MONGODB_NAME = Vocard * For `default_controller` you can set custom embeds and buttons in controller, [Example Here](https://github.com/ChocoMeow/Vocard/blob/main/PLACEHOLDERS.md#controller-embeds) ## How to update? (For Windows and Linux) -1. Run `python update.py --check` to check if your bot is up to date -2. Run `python update.py --start` to start update your bot
***Note: Make sure there are no personal files in the directory! Otherwise it will be deleted.*** +```sh +# Check the current version +python update.py -c + +# Install the latest version +python update.py -l + +# Install the specified version +python update.py -v VERSION + +# Install the beta version +python update.py -b +``` diff --git a/update.py b/update.py index e667012..849a6ff 100644 --- a/update.py +++ b/update.py @@ -1,4 +1,4 @@ -import requests, zipfile, os, sys, shutil, traceback +import requests, zipfile, os, shutil, argparse from io import BytesIO root_dir = os.path.dirname(os.path.abspath(__file__)) @@ -8,6 +8,12 @@ GITHUB_API_URL = "https://api.github.com/repos/ChocoMeow/Vocard/releases/latest" VOCARD_URL = "https://github.com/ChocoMeow/Vocard/archive/" IGNORE_FILES = ["settings.json", ".env"] +class bcolors: + WARNING = '\033[93m' + FAIL = '\033[91m' + OKGREEN = '\033[92m' + ENDC = '\033[0m' + def check_version(with_msg=False): """Check for the latest version of the project. @@ -20,8 +26,8 @@ def check_version(with_msg=False): response = requests.get(GITHUB_API_URL) latest_version = response.json().get("name", __version__) if with_msg: - msg = f"Your bot is up-to-date! - {latest_version}" if latest_version == __version__ else \ - f"Your bot is not up-to-date! The latest version is {latest_version} and you are currently running version {__version__}\n. Run `python update.py --start` to update your bot!" + msg = f"{bcolors.OKGREEN}Your bot is up-to-date! - {latest_version}{bcolors.ENDC}" if latest_version == __version__ else \ + f"{bcolors.WARNING}Your bot is not up-to-date! The latest version is {latest_version} and you are currently running version {__version__}\n. Run `python update.py --start` to update your bot!{bcolors.ENDC}" print(msg) return latest_version @@ -37,6 +43,9 @@ def download_file(version=None): version = version if version else check_version() print(f"Downloading Vocard version: {version}") response = requests.get(VOCARD_URL + version + ".zip") + if response.status_code == 404: + print(f"{bcolors.FAIL}Warning: Version not found!{bcolors.ENDC}") + exit() print("Download Completed") return response @@ -47,47 +56,69 @@ def install(response, version): response (BytesIO): the downloaded zip file. version (str): the version to install. """ - print("Installing ...") - zfile = zipfile.ZipFile(BytesIO(response.content)) - zfile.extractall(root_dir) - - version = version.replace("v", "") - source_dir = os.path.join(root_dir, f"Vocard-{version}") - if os.path.exists(source_dir): - for filename in os.listdir(root_dir): - if filename in IGNORE_FILES + [f"Vocard-{version}"]: - continue - filename = os.path.join(root_dir, filename) - if os.path.isdir(filename): - shutil.rmtree(filename) - else: - os.remove(filename) - for filename in os.listdir(source_dir): - shutil.move(os.path.join(source_dir, filename), os.path.join(root_dir, filename)) - os.rmdir(source_dir) - -def start(): - """Start the update process.""" - try: - user_input = input("--------------------------------------------------------------------------\n" + user_input = input(f"{bcolors.WARNING}--------------------------------------------------------------------------\n" "Note: Before proceeding, please ensure that there are no personal files or\n" \ "sensitive information in the directory you're about to delete. This action\n" \ "is irreversible, so it's important to double-check that you're making the \n" \ - "right decision. Continue with caution? (Y/n) ") + f"right decision. {bcolors.ENDC} Continue with caution? (Y/n) ") - if user_input.lower() in ["y", "yes"]: - response = download_file() - version = check_version() - install(response, version) - print("Update Successfully! Run `python main.py` to start your bot") - else: - print("Update canceled!") + if user_input.lower() in ["y", "yes"]: + print("Installing ...") + zfile = zipfile.ZipFile(BytesIO(response.content)) + zfile.extractall(root_dir) - except Exception as e: - print(traceback.format_exc()) + version = version.replace("v", "") + source_dir = os.path.join(root_dir, f"Vocard-{version}") + if os.path.exists(source_dir): + for filename in os.listdir(root_dir): + if filename in IGNORE_FILES + [f"Vocard-{version}"]: + continue -if "--start" in sys.argv: - start() + filename = os.path.join(root_dir, filename) + if os.path.isdir(filename): + shutil.rmtree(filename) + else: + os.remove(filename) + for filename in os.listdir(source_dir): + shutil.move(os.path.join(source_dir, filename), os.path.join(root_dir, filename)) + os.rmdir(source_dir) + print(f"{bcolors.OKGREEN}Version {version} installed Successfully! Run `python main.py` to start your bot{bcolors.ENDC}") + else: + print("Update canceled!") -if "--check" in sys.argv: - check_version(with_msg=True) \ No newline at end of file +def parse_args(): + """Parse command line arguments.""" + parser = argparse.ArgumentParser(description='Update script for Vocard.') + parser.add_argument('-c', '--check', action='store_true', help='Check the current version of the Vocard') + parser.add_argument('-v', '--version', type=str, help='Install the specified version of the Vocard') + parser.add_argument('-l', '--latest', action='store_true', help='Install the latest version of the Vocard from Github') + parser.add_argument('-b', '--beta', action='store_true', help='Install the beta version of the Vocard from Github') + return parser.parse_args() + +def main(): + """Main function.""" + args = parse_args() + + if args.check: + check_version(with_msg=True) + + elif args.version: + version = args.version + response = download_file(version) + install(response, version) + + elif args.latest: + response = download_file() + version = check_version() + install(response, version) + + elif args.beta: + response = download_file("refs/heads/beta") + install(response, "beta.zip") + pass + + else: + print(f"{bcolors.FAIL}No arguments provided. Run `python update.py -h` for help.{bcolors.ENDC}") + +if __name__ == "__main__": + main() \ No newline at end of file