Skip to content

Commit

Permalink
fix: solve Persistent startup not loading roles.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RakuJa committed Dec 14, 2024
1 parent 2f2a124 commit b942860
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
12 changes: 9 additions & 3 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
54 changes: 54 additions & 0 deletions post_build.py
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 25 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b942860

Please sign in to comment.