updated database files so that user and database names are put in quotes, also fixed some problems with pip

This commit is contained in:
ronny abraham 2025-04-23 20:12:29 +03:00
parent 82b8699533
commit 5c75151f2b
6 changed files with 86 additions and 68 deletions

View file

@ -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}" \

View file

@ -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:<branch>\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,

View file

@ -1,2 +1,2 @@
DROP DATABASE {{db_name}};
DROP USER {{db_user}};
DROP DATABASE '{{db_name}}';
DROP USER '{{db_user}}';

View file

@ -1 +1 @@
DROP DATABASE {{db_name}};
DROP DATABASE '{{db_name}}';

View file

@ -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}}';

View file

@ -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}}';