From 327d0c20385a66fc73057455b56f9d748917cdf4 Mon Sep 17 00:00:00 2001 From: Jannis Gebauer Date: Fri, 8 Apr 2016 09:45:09 +0200 Subject: [PATCH] major project cleanup --- cookiecutter.json | 4 + hooks/post_gen_project.py | 97 +++++++++++++++---- .../{ => utility}/install_os_dependencies.sh | 0 .../install_python_dependencies.sh | 0 .../{ => utility}/requirements.apt | 0 .../templates/base.html | 6 +- 6 files changed, 83 insertions(+), 24 deletions(-) rename {{cookiecutter.repo_name}}/{ => utility}/install_os_dependencies.sh (100%) rename {{cookiecutter.repo_name}}/{ => utility}/install_python_dependencies.sh (100%) rename {{cookiecutter.repo_name}}/{ => utility}/requirements.apt (100%) diff --git a/cookiecutter.json b/cookiecutter.json index 9e75f14b..d0fb27e0 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -18,5 +18,9 @@ "use_pycharm": "n", "windows": "n", "use_python2": "n", + "use_docker": "y", + "use_heroku": "n", + "use_grunt": "n", + "use_angular": "n", "open_source_license": ["MIT", "BSD", "Not open source"] } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 80742e86..e4f17a53 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -12,13 +12,11 @@ A portion of this code was adopted from Django's standard crypto functions and utilities, specifically: https://github.com/django/django/blob/master/django/utils/crypto.py """ -import hashlib +from __future__ import print_function import os import random import shutil -from cookiecutter.config import DEFAULT_CONFIG - # Get the root project directory PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -27,11 +25,9 @@ try: random = random.SystemRandom() using_sysrandom = True except NotImplementedError: - # import warnings - # warnings.warn('A secure pseudo-random number generator is not available ' - # 'on your system. Falling back to Mersenne Twister.') using_sysrandom = False + def get_random_string( length=50, allowed_chars='abcdefghijklmnopqrstuvwxyz0123456789!@#%^&*(-_=+)'): @@ -40,21 +36,15 @@ def get_random_string( The default length of 12 with the a-z, A-Z, 0-9 character set returns a 71-bit value. log_2((26+26+10)^12) =~ 71 bits """ - if not using_sysrandom: - # This is ugly, and a hack, but it makes things better than - # the alternative of predictability. This re-seeds the PRNG - # using a value that is hard for an attacker to predict, every - # time a random string is required. This may change the - # properties of the chosen random sequence slightly, but this - # is better than absolute predictability. - random.seed( - hashlib.sha256( - ("%s%s%s" % ( - random.getstate(), - time.time(), - settings.SECRET_KEY)).encode('utf-8') - ).digest()) - return ''.join(random.choice(allowed_chars) for i in range(length)) + if using_sysrandom: + return ''.join(random.choice(allowed_chars) for i in range(length)) + print( + "cookiecutter-django couldn't find a secure pseudo-random number generator on your system." + " Please change change your SECRET_KEY variables in conf/settings/local.py and env.example" + " manually." + ) + return "CHANGEME!!" + def set_secret_key(setting_file_location): # Open locals.py @@ -113,6 +103,40 @@ def remove_pycharm_dir(project_directory): docs_dir_location = os.path.join(PROJECT_DIRECTORY, 'docs/pycharm/') shutil.rmtree(docs_dir_location) + +def remove_heroku_files(): + """ + Removes files needed for heroku if it isn't going to be used + """ + for filename in ["app.json", "Procfile", "requirements.txt", "runtime.txt"]: + os.remove(os.path.join( + PROJECT_DIRECTORY, filename + )) + + +def remove_docker_files(): + """ + Removes files needed for docker if it isn't going to be used + """ + for filename in ["dev.yml", "docker-compose.yml", ".dockerignore"]: + os.remove(os.path.join( + PROJECT_DIRECTORY, filename + )) + + shutil.rmtree(os.path.join( + PROJECT_DIRECTORY, "compose" + )) + + +def remove_grunt_files(): + """ + Removes files needed for grunt if it isn't going to be used + """ + for filename in ["Gruntfile.js", "package.json"]: + os.remove(os.path.join( + PROJECT_DIRECTORY, filename + )) + # IN PROGRESS # def copy_doc_files(project_directory): # cookiecutters_dir = DEFAULT_CONFIG['cookiecutters_dir'] @@ -142,5 +166,36 @@ if '{{ cookiecutter.use_celery }}'.lower() == 'n': if '{{ cookiecutter.use_pycharm }}'.lower() != 'y': remove_pycharm_dir(PROJECT_DIRECTORY) +# 4. Removes all heroku files if it isn't going to be used +if '{{ cookiecutter.use_heroku }}'.lower() != 'y': + remove_heroku_files() + +# 5. Removes all docker files if it isn't going to be used +if '{{ cookiecutter.use_docker }}'.lower() != 'y': + remove_docker_files() + +# 6. Removes all grunt files if it isn't going to be used +if '{{ cookiecutter.use_grunt }}'.lower() != 'y': + remove_grunt_files() + + +# 7. Display a warning if use_docker and use_grunt are selected. Grunt isn't supported by our +# docker config atm. +if '{{ cookiecutter.use_grunt }}'.lower() == 'y' and '{{ cookiecutter.use_docker }}'.lower() == 'y': + print( + "You selected to use docker and grunt. This is NOT supported out of the box for now. You " + "can continue to use the project like you normally would, but you will need to add a " + " grunt service to your docker configuration manually." + ) + +# 7. Display a warning if use_docker and use_mailhog are selected. Mailhog isn't supported by our +# docker config atm. +if '{{ cookiecutter.use_mailhog }}'.lower() == 'y' and '{{ cookiecutter.use_docker }}'.lower() == 'y': + print( + "You selected to use docker and mailhog. This is NOT supported out of the box for now. You" + " can continue to use the project like you normally would, but you will need to add a " + " mailhog service to your docker configuration manually." + ) + # 4. Copy files from /docs/ to {{ cookiecutter.repo_name }}/docs/ # copy_doc_files(PROJECT_DIRECTORY) diff --git a/{{cookiecutter.repo_name}}/install_os_dependencies.sh b/{{cookiecutter.repo_name}}/utility/install_os_dependencies.sh similarity index 100% rename from {{cookiecutter.repo_name}}/install_os_dependencies.sh rename to {{cookiecutter.repo_name}}/utility/install_os_dependencies.sh diff --git a/{{cookiecutter.repo_name}}/install_python_dependencies.sh b/{{cookiecutter.repo_name}}/utility/install_python_dependencies.sh similarity index 100% rename from {{cookiecutter.repo_name}}/install_python_dependencies.sh rename to {{cookiecutter.repo_name}}/utility/install_python_dependencies.sh diff --git a/{{cookiecutter.repo_name}}/requirements.apt b/{{cookiecutter.repo_name}}/utility/requirements.apt similarity index 100% rename from {{cookiecutter.repo_name}}/requirements.apt rename to {{cookiecutter.repo_name}}/utility/requirements.apt diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/base.html b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/base.html index 9c0324c5..cd97ca6a 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/base.html +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/base.html @@ -1,5 +1,5 @@ {% raw %}{% load staticfiles i18n %} - + @@ -23,9 +23,9 @@ {% endblock %} - {% block angular %} + {% endraw %}{% if cookiecutter.use_angular == "y" %}{% raw %}{% block angular %} - {% endblock %} + {% endblock %}{% endraw %}{% endif %}{% raw %}