Remove envs when not opted for

This commit is contained in:
Nikita P. Shupeyko 2018-03-06 17:34:54 +03:00
parent 50c8762dd0
commit 6ee9ed28ea
3 changed files with 59 additions and 36 deletions

View File

@ -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__':

View File

@ -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
)

View File

@ -348,5 +348,3 @@ mailhog
# See issue https://github.com/pydanny/cookiecutter-django/issues/1321
!/compose/local/
{% endif %}
.env