From 6ee9ed28ea0c5efcc682c07409b91d00fbadde50 Mon Sep 17 00:00:00 2001 From: "Nikita P. Shupeyko" Date: Tue, 6 Mar 2018 17:34:54 +0300 Subject: [PATCH] Remove envs when not opted for --- hooks/post_gen_project.py | 63 ++++++++++++++++++------ hooks/pre_gen_project.py | 30 +++++------ {{cookiecutter.project_slug}}/.gitignore | 2 - 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 235b337df..2ac504a1e 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -7,12 +7,12 @@ NOTE: TODO: ? restrict Cookiecutter Django project initialization to Python 3.x environments only """ +from __future__ import print_function import os import random import shutil import string -import sys try: # Inspired by @@ -22,6 +22,12 @@ try: except NotImplementedError: using_sysrandom = False +TERMINATOR = "\x1b[0m" +WARNING = "\x1b[1;33m [WARNING]: " +INFO = "\x1b[1;33m [INFO]: " +HINT = "\x1b[3;33m" +SUCCESS = "\x1b[1;32m [SUCCESS]: " + PROJECT_DIR_PATH = os.path.realpath(os.path.curdir) # TODO: ? I doubt even need that @@ -146,8 +152,7 @@ def set_flag(file_path, if value is None: random_string = generate_random_string(*args, **kwargs) if random_string is None: - import sys - sys.stdout.write( + print( "We couldn't find a secure pseudo-random number generator on your system. " "Please, make sure to manually {} later.".format(flag) ) @@ -223,8 +228,6 @@ def append_to_gitignore_file(s): def set_flags_in_envs(postgres_user): - append_to_gitignore_file('.envs' + '/**/*') - local_postgres_envs_path = os.path.join(PROJECT_DIR_PATH, '.envs', '.local', '.postgres') set_postgres_user(local_postgres_envs_path, value=postgres_user) set_postgres_password(local_postgres_envs_path) @@ -243,6 +246,11 @@ def set_flags_in_settings_files(): set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'test.py')) +def remove_envs_and_associated_files(): + shutil.rmtree('.envs') + os.remove('merge_production_dotenvs_in_dotenv.py') + + def main(): postgres_user = generate_postgres_user() set_flags_in_envs(postgres_user) @@ -262,6 +270,24 @@ def main(): if '{{ cookiecutter.use_heroku }}'.lower() == 'n': remove_heroku_files() + envs_make_sense = '{{ cookiecutter.use_docker }}'.lower() == 'n' \ + and '{{ cookiecutter.use_heroku }}'.lower() == 'n' + if envs_make_sense: + if '{{ cookiecutter.keep_local_envs_in_vcs }}'.lower() == 'y': + print( + INFO + + ".env(s) are only utilized when Docker Compose and/or " + "Heroku support is enabled so keeping them does not " + "make sense given your current setup." + + TERMINATOR + ) + remove_envs_and_associated_files() + else: + append_to_gitignore_file('.env') + append_to_gitignore_file('.envs' + '/**/*') + if '{{ cookiecutter.keep_local_envs_in_vcs }}'.lower() == 'y': + append_to_gitignore_file('!.envs/.local/') + if '{{ cookiecutter.js_task_runner}}'.lower() == 'gulp': remove_grunt_files() elif '{{ cookiecutter.js_task_runner}}'.lower() == 'grunt': @@ -270,18 +296,20 @@ def main(): remove_gulp_files() remove_grunt_files() remove_packagejson_file() - if '{{ cookiecutter.js_task_runner }}'.lower() in ['grunt', 'gulp'] \ and '{{ cookiecutter.use_docker }}'.lower() == 'y': - TERMINATOR = "\x1b[0m" - INFO = "\x1b[1;33m [INFO]: " - sys.stdout.write( - INFO + - "Docker and {} JS task runner ".format('{{ cookiecutter.js_task_runner }}'.lower().capitalize()) + + print( + WARNING + + "Docker and {} JS task runner ".format( + '{{ cookiecutter.js_task_runner }}' + .lower() + .capitalize() + ) + "working together not supported yet. " - "You can continue using the generated project like you normally would, " - "however you would need to add a JS task runner service " - "to your Docker Compose configuration manually." + + "You can continue using the generated project like you " + "normally would, however you would need to add a JS " + "task runner service to your Docker Compose configuration " + "manually." + TERMINATOR ) @@ -291,8 +319,11 @@ def main(): if '{{ cookiecutter.use_travisci }}'.lower() == 'n': remove_dottravisyml_file() - if '{{ cookiecutter.keep_local_envs_in_vcs }}'.lower() == 'y': - append_to_gitignore_file('!.envs/.local/') + print( + SUCCESS + + "Project initialized, keep up the good work!" + + TERMINATOR + ) if __name__ == '__main__': diff --git a/hooks/pre_gen_project.py b/hooks/pre_gen_project.py index c48ce0ad8..cfe1349fe 100644 --- a/hooks/pre_gen_project.py +++ b/hooks/pre_gen_project.py @@ -6,6 +6,15 @@ NOTE: TODO: ? restrict Cookiecutter Django project initialization to Python 3.x environments only """ +from __future__ import print_function + +import sys + +TERMINATOR = "\x1b[0m" +WARNING = "\x1b[1;33m [WARNING]: " +INFO = "\x1b[1;33m [INFO]: " +HINT = "\x1b[3;33m" +SUCCESS = "\x1b[1;32m [SUCCESS]: " project_slug = '{{ cookiecutter.project_slug }}' if hasattr(project_slug, 'isidentifier'): @@ -13,20 +22,11 @@ if hasattr(project_slug, 'isidentifier'): assert "\\" not in "{{ cookiecutter.author_name }}", "Don't include backslashes in author name." - using_docker = '{{ cookiecutter.use_docker }}'.lower() if using_docker == 'n': - TERMINATOR = "\x1b[0m" - WARNING = "\x1b[1;33m [WARNING]: " - INFO = "\x1b[1;33m [INFO]: " - HINT = "\x1b[3;33m" - SUCCESS = "\x1b[1;32m [SUCCESS]: " - - import sys - python_major_version = sys.version_info[0] if python_major_version == 2: - sys.stdout.write( + print( WARNING + "Cookiecutter Django does not support Python 2. " "Stability is guaranteed with Python 3.6+ only, " @@ -39,14 +39,14 @@ if using_docker == 'n': if choice in yes_options: break elif choice in no_options: - sys.stdout.write( + print( INFO + "Generation process stopped as requested." + TERMINATOR ) sys.exit(1) else: - sys.stdout.write( + print( HINT + "Please respond with {} or {}: ".format( ', '.join(["'{}'".format(o) for o in yes_options if not o == '']), @@ -54,9 +54,3 @@ if using_docker == 'n': ) + TERMINATOR ) - - sys.stdout.write( - SUCCESS + - "Project initialized, keep up the good work!" + - TERMINATOR - ) diff --git a/{{cookiecutter.project_slug}}/.gitignore b/{{cookiecutter.project_slug}}/.gitignore index 36e9f5a75..207d2044d 100644 --- a/{{cookiecutter.project_slug}}/.gitignore +++ b/{{cookiecutter.project_slug}}/.gitignore @@ -348,5 +348,3 @@ mailhog # See issue https://github.com/pydanny/cookiecutter-django/issues/1321 !/compose/local/ {% endif %} - -.env