From b942860b2ca858ae5bc3cdad8ff06fe6a7dcf754 Mon Sep 17 00:00:00 2001 From: rakuja Date: Sun, 15 Dec 2024 00:03:49 +0100 Subject: [PATCH] fix: solve Persistent startup not loading roles. Roles are always calculated at startup in Clean mode, but in Persistent mode the calculation is skipped entirely, trusting the DB. The build process deleted part of the DB leading to no role reloaded. This post build script will ensure the integrity of the DB after the build process is completed --- Makefile.toml | 12 +++++++++--- post_build.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 37 +++++++++++++++++++++++------------ 3 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 post_build.py diff --git a/Makefile.toml b/Makefile.toml index 71c2a01..3705ced 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -4,7 +4,11 @@ args = ["clean"] [tasks.prebuild] command = "python3" -args = ["setup.py", "--db_version", "2.4.0"] +args = ["setup.py", "--copy_db", "--db_version", "2.4.0"] + +[tasks.post_build] +command = "python3" +args = ["post_build.py"] [tasks.format] install_crate = "rustfmt" @@ -47,12 +51,14 @@ dependencies = [ [tasks.bybe-release] dependencies = [ "test", - "build-release" + "build-release", + "post_build" ] [tasks.bybe-docker-release] dependencies = [ - "build-docker-release" + "build-docker-release", + "post_build" ] [tasks.format-and-lint] diff --git a/post_build.py b/post_build.py new file mode 100644 index 0000000..687d562 --- /dev/null +++ b/post_build.py @@ -0,0 +1,54 @@ +# setup.py +import os +import pathlib +import getopt, sys +import shutil + +# Remove 1st argument from the +# list of command line arguments +argumentList = sys.argv[1:] + +# Options +options = "hn:" + +# Long options +long_options = ["help", "no_clean"] + +def handle_command_line_arguments() -> bool: + no_cleanup: bool = False + try: + # Parsing argument + arguments, values = getopt.getopt(argumentList, options, long_options) + + # checking each argument + for currentArgument, _currentValue in arguments: + + if currentArgument in ("-h", "--help"): + print("This script is used as a post build cleanup script for BYBE. \n" + "Should be executed every time a Persistent build (app) needs to be mad as it ensure a correct DB is used. \n" + "Pass the --no-clean or -n argument to avoid deleting files (db used for builds)") + elif currentArgument in ("-n", "--no_clean"): + no_cleanup = True + except getopt.error: + pass + return no_cleanup + +def main(): + # Check if the file already exists or needs downloading + print("Executing post build script...") + no_cleanup: bool = handle_command_line_arguments() is True + clean_db: str = "database.db_copy" + dirty_db: str = "database.db" + if os.path.exists(clean_db): + print("Restoring clean DB file...") + pathlib.Path(dirty_db).unlink(missing_ok=True) + shutil.copyfile(clean_db, dirty_db) + else: + print("Cannot restore clean DB, will use the dirty DB used for build...") + if not no_cleanup: + print("Cleaning up DB copy...") + pathlib.Path(clean_db).unlink(missing_ok=True) + + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index ae863b6..6f2b338 100644 --- a/setup.py +++ b/setup.py @@ -1,47 +1,60 @@ # setup.py import os +import shutil import urllib.request import getopt, sys -from typing import Optional +from typing import Tuple # Remove 1st argument from the # list of command line arguments argumentList = sys.argv[1:] # Options -options = "hd:" +options = "hdc:" # Long options -long_options = ["help", "db_version="] +long_options = ["help", "db_version=", "copy_db"] -def handle_command_line_arguments() -> Optional[str]: +def handle_command_line_arguments() -> Tuple[str, bool]: + db_version: str = "2.3.0" # Oldest DB version publicly available + copy_db: bool = False try: # Parsing argument arguments, values = getopt.getopt(argumentList, options, long_options) - # checking each argument for currentArgument, currentValue in arguments: if currentArgument in ("-h", "--help"): print("This script downloads or creates necessary file to build BYBE. \n" "Should be executed the first time the project is built in the machine or when resetting the database \n" - "Pass the --db_version or -d argument to input a specific BYBE-DB version to download (>= 2.3.0)") + "Pass the --db_version or -d argument to input a specific BYBE-DB version to download (>= 2.3.0) \n" + "Use the --copy or -c argument to temporary copy the database (both downloaded or already present). \n" + "This is used to enable Persistent startup (build with db copy and then delete the dirty db used to build)") elif currentArgument in ("-d", "--db_version"): - return currentValue + db_version = currentValue + elif currentArgument in ("-c", "--copy_db"): + copy_db = True except getopt.error: pass + return db_version, copy_db def main(): # Check if the file already exists or needs downloading - db_version: str = handle_command_line_arguments() - print(f"Using DB version: {db_version}") or "2.3.0" # Oldest DB version publicly available + x = handle_command_line_arguments() + db_version: str = x[0] + copy_enabled: bool = x[1] + print(f"Using DB version: {db_version}") remote_url: str = f"https://github.com/RakuJa/BYBE-DB/releases/download/v{db_version}/database.db" - destination_file: str = "database.db" - if not os.path.exists(destination_file): + database_file: str = "database.db" + if not os.path.exists(database_file): print("Downloading the database file...") - urllib.request.urlretrieve(remote_url, destination_file) + urllib.request.urlretrieve(remote_url, database_file) else: print("Database file already exists, skipping download.") + if copy_enabled: + destination_file: str = f"{database_file}_copy" + shutil.copyfile(database_file, destination_file) + print(f"Copied db to {destination_file}") if __name__ == '__main__': main()