2015-09-10 03:56:27 +03:00
|
|
|
"""
|
2015-09-15 04:55:41 +03:00
|
|
|
Does the following:
|
|
|
|
|
|
|
|
1. Generates and saves random secret key
|
|
|
|
2. Removes the taskapp if celery isn't going to be used
|
2016-03-23 20:51:25 +03:00
|
|
|
3. Removes the .idea directory if PyCharm isn't going to be used
|
2016-04-20 20:00:35 +03:00
|
|
|
4. Copy files from /docs/ to {{ cookiecutter.project_slug }}/docs/
|
2015-09-18 20:55:06 +03:00
|
|
|
|
|
|
|
TODO: this might have to be moved to a pre_gen_hook
|
2015-09-15 04:55:41 +03:00
|
|
|
|
|
|
|
A portion of this code was adopted from Django's standard crypto functions and
|
2015-09-10 03:56:27 +03:00
|
|
|
utilities, specifically:
|
|
|
|
https://github.com/django/django/blob/master/django/utils/crypto.py
|
|
|
|
"""
|
2018-01-15 23:33:51 +03:00
|
|
|
from __future__ import print_function
|
2015-09-10 03:56:27 +03:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import shutil
|
2017-02-14 02:37:14 +03:00
|
|
|
import string
|
2015-09-10 03:56:27 +03:00
|
|
|
|
2015-09-15 04:55:41 +03:00
|
|
|
# Get the root project directory
|
|
|
|
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
|
|
|
|
|
2015-09-10 03:56:27 +03:00
|
|
|
# Use the system PRNG if possible
|
|
|
|
try:
|
|
|
|
random = random.SystemRandom()
|
|
|
|
using_sysrandom = True
|
|
|
|
except NotImplementedError:
|
|
|
|
using_sysrandom = False
|
|
|
|
|
2016-04-08 10:45:09 +03:00
|
|
|
|
2017-02-14 02:37:14 +03:00
|
|
|
def get_random_string(length=50):
|
2015-09-10 03:56:27 +03:00
|
|
|
"""
|
|
|
|
Returns a securely generated 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
|
|
|
|
"""
|
2017-02-14 08:57:59 +03:00
|
|
|
punctuation = string.punctuation.replace('"', '').replace("'", '')
|
2017-02-14 08:59:21 +03:00
|
|
|
punctuation = punctuation.replace('\\', '')
|
2016-04-08 10:45:09 +03:00
|
|
|
if using_sysrandom:
|
2017-02-14 02:37:14 +03:00
|
|
|
return ''.join(random.choice(
|
2017-02-14 08:57:59 +03:00
|
|
|
string.digits + string.ascii_letters + punctuation
|
2017-02-14 02:37:14 +03:00
|
|
|
) for i in range(length))
|
2017-02-14 08:57:59 +03:00
|
|
|
|
2016-04-08 10:45:09 +03:00
|
|
|
print(
|
2016-05-10 07:12:34 +03:00
|
|
|
"Cookiecutter Django couldn't find a secure pseudo-random number generator on your system."
|
2016-04-08 10:45:09 +03:00
|
|
|
" Please change change your SECRET_KEY variables in conf/settings/local.py and env.example"
|
|
|
|
" manually."
|
|
|
|
)
|
|
|
|
return "CHANGEME!!"
|
|
|
|
|
2015-09-10 03:56:27 +03:00
|
|
|
|
2015-10-15 19:29:43 +03:00
|
|
|
def set_secret_key(setting_file_location):
|
|
|
|
# Open locals.py
|
|
|
|
with open(setting_file_location) as f:
|
|
|
|
file_ = f.read()
|
|
|
|
|
|
|
|
# Generate a SECRET_KEY that matches the Django standard
|
|
|
|
SECRET_KEY = get_random_string()
|
|
|
|
|
|
|
|
# Replace "CHANGEME!!!" with SECRET_KEY
|
2015-11-08 06:47:31 +03:00
|
|
|
file_ = file_.replace('CHANGEME!!!', SECRET_KEY, 1)
|
2015-10-15 19:29:43 +03:00
|
|
|
|
|
|
|
# Write the results to the locals.py module
|
|
|
|
with open(setting_file_location, 'w') as f:
|
|
|
|
f.write(file_)
|
|
|
|
|
|
|
|
|
2015-09-15 04:55:41 +03:00
|
|
|
def make_secret_key(project_directory):
|
|
|
|
"""Generates and saves random secret key"""
|
|
|
|
# Determine the local_setting_file_location
|
2015-10-15 19:29:43 +03:00
|
|
|
local_setting = os.path.join(
|
2015-09-15 04:55:41 +03:00
|
|
|
project_directory,
|
|
|
|
'config/settings/local.py'
|
|
|
|
)
|
|
|
|
|
2015-10-15 19:29:43 +03:00
|
|
|
# local.py settings file
|
|
|
|
set_secret_key(local_setting)
|
2015-09-15 04:55:41 +03:00
|
|
|
|
2015-10-15 19:29:43 +03:00
|
|
|
env_file = os.path.join(
|
|
|
|
project_directory,
|
|
|
|
'env.example'
|
|
|
|
)
|
|
|
|
|
|
|
|
# env.example file
|
|
|
|
set_secret_key(env_file)
|
2015-09-10 03:56:27 +03:00
|
|
|
|
|
|
|
|
2016-06-06 00:16:02 +03:00
|
|
|
def remove_file(file_name):
|
|
|
|
if os.path.exists(file_name):
|
|
|
|
os.remove(file_name)
|
|
|
|
|
|
|
|
|
2015-09-15 04:55:41 +03:00
|
|
|
def remove_task_app(project_directory):
|
|
|
|
"""Removes the taskapp if celery isn't going to be used"""
|
|
|
|
# Determine the local_setting_file_location
|
|
|
|
task_app_location = os.path.join(
|
|
|
|
PROJECT_DIRECTORY,
|
2016-04-20 20:00:35 +03:00
|
|
|
'{{ cookiecutter.project_slug }}/taskapp'
|
2015-09-15 04:55:41 +03:00
|
|
|
)
|
|
|
|
shutil.rmtree(task_app_location)
|
2015-09-10 03:56:27 +03:00
|
|
|
|
2016-03-23 20:51:25 +03:00
|
|
|
|
|
|
|
def remove_pycharm_dir(project_directory):
|
2016-03-28 02:43:37 +03:00
|
|
|
"""
|
|
|
|
Removes directories related to PyCharm
|
|
|
|
if it isn't going to be used
|
|
|
|
"""
|
2016-03-23 20:51:25 +03:00
|
|
|
idea_dir_location = os.path.join(PROJECT_DIRECTORY, '.idea/')
|
2016-04-26 01:05:59 +03:00
|
|
|
if os.path.exists(idea_dir_location):
|
|
|
|
shutil.rmtree(idea_dir_location)
|
2016-03-23 20:51:25 +03:00
|
|
|
|
2016-03-28 02:43:37 +03:00
|
|
|
docs_dir_location = os.path.join(PROJECT_DIRECTORY, 'docs/pycharm/')
|
2016-04-26 01:05:59 +03:00
|
|
|
if os.path.exists(docs_dir_location):
|
|
|
|
shutil.rmtree(docs_dir_location)
|
2016-03-23 20:51:25 +03:00
|
|
|
|
2016-04-08 10:45:09 +03:00
|
|
|
|
|
|
|
def remove_heroku_files():
|
|
|
|
"""
|
|
|
|
Removes files needed for heroku if it isn't going to be used
|
|
|
|
"""
|
2016-10-07 23:55:15 +03:00
|
|
|
filenames = ["Procfile", "runtime.txt"]
|
|
|
|
for filename in ["Procfile", "runtime.txt"]:
|
2016-06-06 00:16:02 +03:00
|
|
|
file_name = os.path.join(PROJECT_DIRECTORY, filename)
|
|
|
|
remove_file(file_name)
|
2016-04-08 10:45:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
def remove_docker_files():
|
|
|
|
"""
|
|
|
|
Removes files needed for docker if it isn't going to be used
|
|
|
|
"""
|
2018-01-17 21:13:09 +03:00
|
|
|
for filename in ["local.yml", "production.yml", ".dockerignore"]:
|
2018-01-17 09:06:28 +03:00
|
|
|
filename = os.path.join(PROJECT_DIRECTORY, filename)
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.remove(filename)
|
2016-04-08 10:45:09 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2016-06-04 03:31:36 +03:00
|
|
|
for filename in ["Gruntfile.js"]:
|
|
|
|
os.remove(os.path.join(
|
|
|
|
PROJECT_DIRECTORY, filename
|
|
|
|
))
|
|
|
|
|
|
|
|
def remove_gulp_files():
|
|
|
|
"""
|
|
|
|
Removes files needed for grunt if it isn't going to be used
|
|
|
|
"""
|
|
|
|
for filename in ["gulpfile.js"]:
|
|
|
|
os.remove(os.path.join(
|
|
|
|
PROJECT_DIRECTORY, filename
|
|
|
|
))
|
|
|
|
|
|
|
|
def remove_packageJSON_file():
|
|
|
|
"""
|
|
|
|
Removes files needed for grunt if it isn't going to be used
|
|
|
|
"""
|
|
|
|
for filename in ["package.json"]:
|
2016-04-08 10:45:09 +03:00
|
|
|
os.remove(os.path.join(
|
|
|
|
PROJECT_DIRECTORY, filename
|
|
|
|
))
|
|
|
|
|
2016-06-24 17:59:55 +03:00
|
|
|
def remove_copying_files():
|
|
|
|
"""
|
|
|
|
Removes files needed for the GPLv3 licence if it isn't going to be used
|
|
|
|
"""
|
|
|
|
for filename in ["COPYING"]:
|
|
|
|
os.remove(os.path.join(
|
|
|
|
PROJECT_DIRECTORY, filename
|
|
|
|
))
|
|
|
|
|
2017-04-24 23:56:27 +03:00
|
|
|
|
2015-09-18 20:55:06 +03:00
|
|
|
# IN PROGRESS
|
|
|
|
# def copy_doc_files(project_directory):
|
|
|
|
# cookiecutters_dir = DEFAULT_CONFIG['cookiecutters_dir']
|
|
|
|
# cookiecutter_django_dir = os.path.join(
|
|
|
|
# cookiecutters_dir,
|
|
|
|
# 'cookiecutter-django',
|
|
|
|
# 'docs'
|
|
|
|
# )
|
|
|
|
# target_dir = os.path.join(
|
|
|
|
# project_directory,
|
|
|
|
# 'docs'
|
|
|
|
# )
|
|
|
|
# for name in os.listdir(cookiecutter_django_dir):
|
|
|
|
# if name.endswith('.rst') and not name.startswith('index'):
|
|
|
|
# src = os.path.join(cookiecutter_django_dir, name)
|
|
|
|
# dst = os.path.join(target_dir, name)
|
|
|
|
# shutil.copyfile(src, dst)
|
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 1. Generates and saves random secret key
|
2015-09-15 04:55:41 +03:00
|
|
|
make_secret_key(PROJECT_DIRECTORY)
|
2015-09-10 03:56:27 +03:00
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 2. Removes the taskapp if celery isn't going to be used
|
2015-09-15 04:55:41 +03:00
|
|
|
if '{{ cookiecutter.use_celery }}'.lower() == 'n':
|
|
|
|
remove_task_app(PROJECT_DIRECTORY)
|
2015-09-18 20:55:06 +03:00
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 3. Removes the .idea directory if PyCharm isn't going to be used
|
2016-03-23 20:51:25 +03:00
|
|
|
if '{{ cookiecutter.use_pycharm }}'.lower() != 'y':
|
|
|
|
remove_pycharm_dir(PROJECT_DIRECTORY)
|
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 4. Removes all heroku files if it isn't going to be used
|
2016-04-08 10:45:09 +03:00
|
|
|
if '{{ cookiecutter.use_heroku }}'.lower() != 'y':
|
|
|
|
remove_heroku_files()
|
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 5. Removes all docker files if it isn't going to be used
|
2016-04-08 10:45:09 +03:00
|
|
|
if '{{ cookiecutter.use_docker }}'.lower() != 'y':
|
|
|
|
remove_docker_files()
|
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
# 6. Removes all JS task manager files if it isn't going to be used
|
2016-06-04 03:31:36 +03:00
|
|
|
if '{{ cookiecutter.js_task_runner}}'.lower() == 'gulp':
|
|
|
|
remove_grunt_files()
|
|
|
|
elif '{{ cookiecutter.js_task_runner}}'.lower() == 'grunt':
|
|
|
|
remove_gulp_files()
|
|
|
|
else:
|
|
|
|
remove_gulp_files()
|
2016-04-08 10:45:09 +03:00
|
|
|
remove_grunt_files()
|
2017-06-21 23:12:22 +03:00
|
|
|
remove_packageJSON_file()
|
2016-04-08 10:45:09 +03:00
|
|
|
|
2018-01-15 23:33:51 +03:00
|
|
|
|
2018-01-16 01:05:27 +03:00
|
|
|
# 9. Display a warning if use_docker and use_grunt are selected. Grunt isn't
|
2016-06-04 20:21:44 +03:00
|
|
|
# supported by our docker config atm.
|
2017-06-21 23:12:22 +03:00
|
|
|
if '{{ cookiecutter.js_task_runner }}'.lower() in ['grunt', 'gulp'] and '{{ cookiecutter.use_docker }}'.lower() == 'y':
|
2016-04-08 10:45:09 +03:00
|
|
|
print(
|
2017-06-21 23:12:22 +03:00
|
|
|
"You selected to use docker and a JS task runner. 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 "
|
|
|
|
"js task runner service to your docker configuration manually."
|
2016-04-08 10:45:09 +03:00
|
|
|
)
|
|
|
|
|
2018-01-16 01:05:27 +03:00
|
|
|
# 10. Removes files needed for the GPLv3 licence if it isn't going to be used.
|
2016-06-24 17:59:55 +03:00
|
|
|
if '{{ cookiecutter.open_source_license}}' != 'GPLv3':
|
|
|
|
remove_copying_files()
|