From 5c75151f2ba3c010ebc3b0071124677fc9ecdd07 Mon Sep 17 00:00:00 2001 From: ronny abraham Date: Wed, 23 Apr 2025 20:12:29 +0300 Subject: [PATCH] updated database files so that user and database names are put in quotes, also fixed some problems with pip --- modules/maintenance.py | 2 +- modules/pip.py | 122 ++++++++++-------- .../database/files/db.drop_all.sql.jinja2 | 4 +- .../conf/database/files/db.drop_db.sql.jinja2 | 2 +- .../conf/database/files/db.init.sql.jinja2 | 13 +- .../conf/database/files/db.re_init.sql.jinja2 | 11 +- 6 files changed, 86 insertions(+), 68 deletions(-) diff --git a/modules/maintenance.py b/modules/maintenance.py index a00ead4..bd272c9 100644 --- a/modules/maintenance.py +++ b/modules/maintenance.py @@ -95,7 +95,7 @@ def edit(remote_path, host_string=None): if sys.platform == "darwin": editor = "mvim" else: - editor = "nvim" + editor = "mvim" # if env.key_filename: # cmd_edit = "{editor} sftp://{user}@{host_string}/{remote_path}" \ diff --git a/modules/pip.py b/modules/pip.py index c10775c..d1d39f7 100644 --- a/modules/pip.py +++ b/modules/pip.py @@ -1,14 +1,20 @@ +import sys +import os +import logging + from fabric.api import env, task from fabric.operations import run from fabric.context_managers import hide, settings from fabric.utils import abort -import sys -import os -import logging - from .utils import virtualenv_source, virtualenv from .utils import printerr +from .utils import booleanize, handle_flag_message +from .utils import prompt_continue +from .utils import upload_template + +from .initialize import get_config + ERROR_BAD_BRANCH_PARAM = -3 ERROR_BAD_PARAM = -2 @@ -46,34 +52,32 @@ def setup_virtualenv(python3=True): if python3: mkvirtualenv_cmd += "--python=python3 " - mkvirtualenv_cmd += "{virtualenv_name}".format( - virtualenv_name=configuration.virtualenv.name) + mkvirtualenv_cmd += f"{configuration.virtualenv.name}" if env.debug: - logging.debug("python3 install: %s" % python3) + logging.debug("python3 install: %s", python3) - logging.debug("virtualenv.workon : %s" - % configuration.virtualenv.workon) + logging.debug("virtualenv.workon : %s", + configuration.virtualenv.workon) - logging.debug("virtualenv.activate : %s" - % configuration.virtualenv.activate) + logging.debug("virtualenv.activate : %s", + configuration.virtualenv.activate) - logging.debug("virtualenv.name : %s" - % configuration.virtualenv.name) + logging.debug("virtualenv.name : %s", + configuration.virtualenv.name) - logging.debug("virtualenv.paths.bin : %s" - % configuration.virtualenv.paths.bin) + logging.debug("virtualenv.paths.bin : %s", + configuration.virtualenv.paths.bin) - logging.debug("virtualenv.paths.root : %s" - % configuration.virtualenv.paths.root) + logging.debug("virtualenv.paths.root : %s", + configuration.virtualenv.paths.root) - logging.debug("mkvirtualenv_cmd: %s" % mkvirtualenv_cmd) + logging.debug("mkvirtualenv_cmd: %s", mkvirtualenv_cmd) logging.debug( "with virtualenv_source(): " - "run(\"\n\t{mkvirtualenv_cmd}\n\t\")".format( - mkvirtualenv_cmd=mkvirtualenv_cmd)) + "run(\"\n\t%s\n\t\")", mkvirtualenv_cmd) else: # run("source virtualenvwrapper.sh; mkvirtualenv " @@ -123,7 +127,7 @@ def update_pip(): pipinstall_cmd = "pip install --upgrade pip" if env.debug: - logging.debug("with virtualenv(): run(\"\n\t%s\n\t\")" % + logging.debug("with virtualenv(): run(\"\n\t%s\n\t\")", pipinstall_cmd) else: with virtualenv(): @@ -150,11 +154,13 @@ def setup(): format='\n%(levelname)s: deploy.pip %(message)s', level=logging.DEBUG) - pipinstall_cmd = "pip install -r {requirements}".format( - requirements=configuration.virtualenv.requirements.filepath) + pipinstall_cmd = ( + f"pip install -r " + f"{configuration.virtualenv.requirements.filepath}" + ) if env.debug: - logging.debug("with virtualenv(): run(\"\n\t%s\n\t\")" % + logging.debug("with virtualenv(): run(\"\n\t%s\n\t\")", pipinstall_cmd) else: with virtualenv(): @@ -181,14 +187,17 @@ def install(package_name=None): abort("You must specify a package name to be installed") if package_name == "--all": - pipinstall_cmd = "pip install -r {requirements_file}".format( - requirements_file=configuration.virtualenv.requirements.filepath) + pipinstall_cmd = ( + f"pip install -r " + f"{configuration.virtualenv.requirements.filepath}" + ) + else: - pipinstall_cmd = "pip install {package}".format( - package=package_name) + pipinstall_cmd = f"pip install {package_name}" if env.debug: - print("pipinstall_cmd: %s" % pipinstall_cmd) + print(f"pipinstall_cmd: {pipinstall_cmd}") + else: with virtualenv(): run(pipinstall_cmd) @@ -231,8 +240,6 @@ def freeze(param='help'): \tFalse (default) - print the freeze output to the console """ - from .utils import booleanize, handle_flag_message - if handle_flag_message(param, msg_help, 'help'): sys.exit() else: @@ -240,15 +247,17 @@ def freeze(param='help'): param = booleanize(param) except TypeError: printerr( - "the parameter value you gave, '{param}' , is not a " - "valid parameter\n{msg_help}".format( - param=param, msg_help=msg_help), + f"the parameter value you gave, '{param}'," + f" is not a valid parameter\n{msg_help}", ERROR_BAD_PARAM ) if param: - cmd_pipfreeze = "pip freeze > {requirements}".format( - requirements=configuration.virtualenv.requirements.filepath) + cmd_pipfreeze = ( + f"pip freeze > " + f"{configuration.virtualenv.requirements.filepath}" + ) + else: cmd_pipfreeze = "pip freeze" @@ -257,44 +266,48 @@ def freeze(param='help'): @task -def copyto(branch): +def copyto(branch=None): """ copy requirements from the current branch to the specified branch this only changes the requirements on the local branches. It does not - upload remotely. This is because I want to use deploy.sync to do all - remote updates + upload remotely. Use deploy.sync to perform remote updates. Keyword Arguments: branch -- the branch to copy to + + Usage: + fab copyto --branch=staging """ - configuration = env.config + if branch is None: + printerr( + "Missing required parameter: 'branch'\n" + "Usage: fab pip.copyto:\n" + "Valid options: development, staging, production", + ERROR_BAD_BRANCH_PARAM + ) + return + configuration = env.config branch_list = ['staging', 'production', 'development'] if branch not in branch_list: - printerr( - "Branch parameter '{branch}' must be one of {branchlist} " - "values.".format(branch=branch, branchlist=branch_list), - ERROR_BAD_BRANCH_PARAM) + f"Branch parameter '{branch}' must be one of {branch_list} " + "values.", ERROR_BAD_BRANCH_PARAM) elif branch == 'development': - printerr( "This method does not allow copying to development branch", ERROR_BAD_BRANCH_PARAM) elif configuration.project.branch == branch: - printerr( "You have set the source branch to the current branch." "This will simply copy over \n\tthe requirements file for " "this branch with itself", ERROR_BAD_BRANCH_PARAM) - from .initialize import get_config - branch_config = get_config(branch) current_local_path = os.path.join( @@ -305,17 +318,16 @@ def copyto(branch): configuration.virtualenv.requirements.local, branch_config.virtualenv.requirements.filename) - print("current_local_path: %s" % current_local_path) - print("branch_local_path: %s" % branch_local_path) + print(f"current_local_path: {current_local_path}") + print("branch_local_path: {branch_local_path}") - message = "Copying file from current branch '{branch_src}' to " \ - "destination branch '{branch_dst}'. Continue? Y/n ".format( - branch_src=configuration.project.branch, branch_dst=branch) + message = ( + f"Copying file from current branch '{configuration.project.branch}'" + f"to destination branch '{branch}'. Continue? Y/n " + ) - from .utils import prompt_continue prompt_continue(message=message) - from .utils import upload_template upload_template( filename=configuration.virtualenv.requirements.filename, destination=branch_local_path, diff --git a/share/templates/conf/database/files/db.drop_all.sql.jinja2 b/share/templates/conf/database/files/db.drop_all.sql.jinja2 index 53408cc..13a50b9 100644 --- a/share/templates/conf/database/files/db.drop_all.sql.jinja2 +++ b/share/templates/conf/database/files/db.drop_all.sql.jinja2 @@ -1,2 +1,2 @@ -DROP DATABASE {{db_name}}; -DROP USER {{db_user}}; +DROP DATABASE '{{db_name}}'; +DROP USER '{{db_user}}'; diff --git a/share/templates/conf/database/files/db.drop_db.sql.jinja2 b/share/templates/conf/database/files/db.drop_db.sql.jinja2 index e368f19..5eba1f6 100644 --- a/share/templates/conf/database/files/db.drop_db.sql.jinja2 +++ b/share/templates/conf/database/files/db.drop_db.sql.jinja2 @@ -1 +1 @@ -DROP DATABASE {{db_name}}; +DROP DATABASE '{{db_name}}'; diff --git a/share/templates/conf/database/files/db.init.sql.jinja2 b/share/templates/conf/database/files/db.init.sql.jinja2 index c8bc93a..53c1309 100644 --- a/share/templates/conf/database/files/db.init.sql.jinja2 +++ b/share/templates/conf/database/files/db.init.sql.jinja2 @@ -1,5 +1,8 @@ -CREATE USER {{db_user}} WITH PASSWORD '{{db_password}}'; -ALTER USER {{db_user}} CREATEDB; -CREATE DATABASE {{db_name}} WITH ENCODING='UTF8'; -ALTER DATABASE {{db_name}} OWNER TO {{db_user}}; -GRANT ALL PRIVILEGES ON DATABASE {{db_name}} TO {{db_user}}; +CREATE USER '{{db_user}}' WITH PASSWORD '{{db_password}}'; +ALTER USER '{{db_user}}' CREATEDB; + +CREATE DATABASE '{{db_name}}' WITH ENCODING='UTF8'; +ALTER DATABASE '{{db_name}}' OWNER TO '{{db_user}}'; + +GRANT ALL PRIVILEGES ON DATABASE '{{db_name}}' TO '{{db_user}}'; +GRANT ALL ON SCHEMA public TO '{{db_user}}'; diff --git a/share/templates/conf/database/files/db.re_init.sql.jinja2 b/share/templates/conf/database/files/db.re_init.sql.jinja2 index e1b4261..57100b0 100644 --- a/share/templates/conf/database/files/db.re_init.sql.jinja2 +++ b/share/templates/conf/database/files/db.re_init.sql.jinja2 @@ -1,4 +1,7 @@ -DROP DATABASE {{db_name}}; -CREATE DATABASE {{db_name}} WITH ENCODING='UTF8'; -ALTER DATABASE {{db_name}} OWNER TO {{db_user}}; -GRANT ALL PRIVILEGES ON DATABASE {{db_name}} TO {{db_user}}; +DROP DATABASE '{{db_name}}'; + +CREATE DATABASE '{{db_name}}' WITH ENCODING='UTF8'; +ALTER DATABASE '{{db_name}}' OWNER TO '{{db_user}}'; + +GRANT ALL PRIVILEGES ON DATABASE '{{db_name}}' TO '{{db_user}}'; +GRANT ALL ON SCHEMA public TO '{{db_user}}';