From 66c85782e865fcd80a06cc920668c7dd9dae0831 Mon Sep 17 00:00:00 2001 From: ronny abraham Date: Thu, 20 Jul 2023 13:10:28 +0300 Subject: [PATCH] modified utils.print_console so that it can have an exit option and give an exit code, also modified database.generate so that it can selectively generate one of the scripts if chosen, has better docutils and also prints a help message when asked --- modules/database.py | 48 +++++++++++++++++++++++++++++++++++++++------ modules/utils.py | 15 +++++++++++++- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/modules/database.py b/modules/database.py index e284cb3..8db81c8 100644 --- a/modules/database.py +++ b/modules/database.py @@ -15,6 +15,7 @@ try: as utils_upload_template from customfabric.modules.utils import loggify, print_console, booleanize + from customfabric.modules.utils import printerr except ImportError: raise @@ -33,6 +34,9 @@ NOTE = """ you will NOT BE ABLE TO SYNC. SO TURN THE FUCKING THING OFF\n\n\n""" +DATABASE_ERROR_CODE_BAD_SCRIPT_PARAM = -3 + + def generate_sql(script_name): configuration = env.config @@ -145,19 +149,51 @@ def execute_sql(script_name, add_dbname=True, is_admin=False): @task -def generate(): +def generate(scriptparam='all'): """ - helper function to upload all the scripts + helper function to generate and upload the scripts + + :parameter scriptparam: the name of the script we want to generate + defaults to all, in which case, all scripts are generated + :type scriptparam: str + """ - generate_sql('init') - generate_sql('re_init') - generate_sql('drop_db') - generate_sql('drop_all') + options = ['init', 're_init', 'drop_db', 'drop_all'] + + if scriptparam == 'all': + for scriptname in options: + generate_sql(scriptname) + elif scriptparam == 'help': + message = "usage: fab database.generate:scriptparam='all'"\ + " or some value of {options} \n leave the parameter "\ + "blank to generate all scripts. Exiting.".format(options=options) + + print_console(message, exit_program=True) + + elif scriptparam in options: + generate_sql(scriptparam) + + else: + msg = "error - script parameters: " \ + "{scriptparam} is not one of {options}".format( + scriptparam=scriptparam, options=options) + + printerr(message=msg, + errcode=DATABASE_ERROR_CODE_BAD_SCRIPT_PARAM, + exit=True) + + # generate_sql('init') + # generate_sql('re_init') + # generate_sql('drop_db') + # generate_sql('drop_all') @task def clear_scripts(): + """ + does nothing, there is no code here yet + """ print("this does nothing, the code isn't here") diff --git a/modules/utils.py b/modules/utils.py index 265e43e..e32d6ae 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -89,7 +89,8 @@ def loggify(module, func, prefix=""): return logging.getLogger(loggername) -def print_console(string, prepend="\n\n", append="\n\n", sep="-", numsep=44): +def print_console(string, prepend="\n\n", append="\n\n", sep="-", numsep=44, + exit_program=False, exit_code=0): """ helper function to take a string, and format it so it prints to the console in a way that is pleasing to the eye. @@ -109,6 +110,14 @@ def print_console(string, prepend="\n\n", append="\n\n", sep="-", numsep=44): :parameter numsep: number of times the separator is printed out on a line :type numsep: int + + :parameter exit_program: whether to exit the program after printing the + message. defaults to False + :type exit_program: bool + + :parameter exit_code: what exit code to use if we exit the program after + printing the message. defaults to 0 + :type exit_code: int """ print(prepend) @@ -123,6 +132,10 @@ def print_console(string, prepend="\n\n", append="\n\n", sep="-", numsep=44): print(append) + if exit_program: + import sys + sys.exit(exit_code) + def print_debug(debugstr, module, function): """