This repository was archived by the owner on Jan 5, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_gen_project.py
71 lines (52 loc) · 1.98 KB
/
post_gen_project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Post-generation hook for cookiecutter project.
After the project is generated through ``cookiecutter``,
``main()`` is called. Simple as that.
"""
from pathlib import Path
import fileinput
import os
import random
import re
import secrets
import string
def remove_open_source_files():
"""Remove files related to open source development.
Borrowed from cookiecutter-django:
https://github.com/pydanny/cookiecutter-django
"""
file_names = ["CONTRIBUTORS.txt", "LICENSE"]
for file_name in file_names:
os.remove(file_name)
def remove_gplv3_files():
"""Remove files related to the GPLv3 license.
Borrowed from cookiecutter-django:
https://github.com/pydanny/cookiecutter-django
"""
file_names = ["COPYING"]
for file_name in file_names:
os.remove(file_name)
def add_to_env_file(key, val):
"""Add the given key and value to the environment file."""
env_file_path = Path("{{ cookiecutter.project_slug }}") / "core" / ".env"
with open(env_file_path, "a") as envfile:
envfile.write(f"{key}={val}\n")
def new_secret_key(length=50):
"""Generate a new key to use for Django's SECRET_KEY setting."""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return ''.join(secrets.choice(chars) for _ in range(length))
def main():
if "{{ cookiecutter.open_source_license }}" == "Not open source":
remove_open_source_files()
if "{{ cookiecutter.open_source_license}}" != "GPLv3":
remove_gplv3_files()
# Create environment variables.
add_to_env_file("DEBUG", "on")
add_to_env_file("SECRET_KEY", new_secret_key())
if "{{ cookiecutter.database_backend }}" == "postgres":
add_to_env_file("POSTGRES_NAME", "mydatabase")
add_to_env_file("POSTGRES_USER", "mydatabaseuser")
add_to_env_file("POSTGRES_PASSWORD", "mypassword")
add_to_env_file("POSTGRES_HOST", "127.0.0.1")
add_to_env_file("POSTGRES_PORT", "5432")
if __name__ == "__main__":
main()