-
Notifications
You must be signed in to change notification settings - Fork 1
/
init
executable file
·130 lines (102 loc) · 3.53 KB
/
init
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""Inititalize the recording-validation Django site.
Usage:
init [--prod | --testing | --dev] [--force] [--debug | --no-debug]
init (-h | --help)
Options:
--debug set DEBUG=True
--dev initialize for developement enviornment
--force overwrite .env if it already exists
--no-debug set DEGUB=False
--production initialize for production enviornment
--testing initialize for testing enviornment
-h --help show this help screen
"""
# TODO: set secret key length?
import os
import sys
import shutil
from enum import Enum
from pathlib import Path
from secrets import token_urlsafe
from docopt import docopt # type: ignore
root = Path(__file__).parent
envfilename = root / '.env'
class DeploymentEnvironment(Enum):
DEVELOPMENT = 'development'
PRODUCTION = 'production'
TESTING = 'testing'
def __str__(self) -> str:
return self.value
DEVELOPMENT = DeploymentEnvironment.DEVELOPMENT
PRODUCTION = DeploymentEnvironment.PRODUCTION
TESTING = DeploymentEnvironment.TESTING
# Parse options.
options = docopt(__doc__)
# Do not overwrite the .env file if it exists.
if envfilename.exists() and not options['--force']:
print("Not overwriting existing `.env` file", file=sys.stderr)
print("Use `init --force` to overwrite.", file=sys.stderr)
sys.exit(2)
# Figure out the environment
if options['--prod']:
environment = PRODUCTION
elif options['--dev']:
environment = DEVELOPMENT
elif options['--testing']:
environment = TESTING
else:
if os.getenv('CI', None) in ('1', 'true'):
environment = TESTING
else:
environment = PRODUCTION
print("Assuming", environment, "environment")
print("Please specify one of --dev, --testing, or --production if this is not the case")
# Remind the user to download metadata
print('Reminder: You must manually download "Master Recordings MetaData" from Google Drive')
print("Save the downloaded .csv file as private/metadata.csv")
def get_debug() -> bool:
"""
Determine whether debug should be set, depending on flags and environment.
"""
if options['--debug'] and environment == PRODUCTION:
print("Setting debug in production is a REALLY bad idea!")
print("Refusing to continue.")
sys.exit(1)
elif options['--no-debug'] and environment == DEVELOPMENT:
return False
elif environment == DEVELOPMENT:
return True
elif options['--no-debug']:
return False
elif options['--debug']:
return True
else:
return False
env_contents = dict(
# Django NEEDS a secret key before it starts up, so generate one.
SECRET_KEY=token_urlsafe(nbytes=64),
DEBUG=get_debug(),
)
# Create the file in shell format.
env_code = '\n'.join(f'{var}="{param!s}"' for var, param in env_contents.items())
env_text = f"""# Automatically created .env file.
# Environment: {environment!s}
{env_code}
"""
print("Creating .env file for the", environment, "environment with contents:")
print()
print(env_text)
# TODO: prompt to confirm?
envfilename.write_text(env_text, encoding='UTF-8')
print(f"Wrote {envfilename!s}!")
print("!!! NOTE !!! You may still need to configure the following in .env:")
print(" - RECVAL_SESSIONS_DIR")
print(" - RECVAL_SQLITE_DB_PATH")
print()
print("Once that's done, you must run the following to initialize the database:")
print(" pipenv run python ./manage.py migrate")
print()
print("You will also want to create a superuser:")
print(" pipenv run python ./manage.py createsuperuser")