-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_app.py
66 lines (53 loc) · 2.23 KB
/
setup_app.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
""" Script to set up application """
import argparse
import os
# Parse arguments passed on command line
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--quiet",
help="Run quietly and force new secret key and db",
action="store_true")
parser.add_argument("--testing",
help='Set test mode to True',
action="store_true")
args = parser.parse_args()
config_path = 'flask_app/config/'
def generate_new_secret(force=False):
""" Generates a new secret key in config dir """
key_file_path = config_path + 'secret_key.py'
if force or not os.path.isfile(key_file_path):
secret_key = os.urandom(24).encode('hex').strip()
with open(key_file_path, 'w') as key_file:
key_file.write('""" Auto generated secret key file """\n\n' +
'SECRET_KEY = "' + secret_key + '".decode("hex")\n')
def copy_secret_config():
""" Make a copy of secret_config.py.template if secret_config.py does not exist """
if not os.path.isfile(config_path + 'secret_config.py'):
with open(config_path + 'secret_config.py', 'w') as secret_config_file:
with open(config_path + 'secret_config.py.template') as secret_config_file_template:
secret_config_file.write(secret_config_file_template.read())
def initialize_db():
""" Initializes a new database """
from flask_app import init_security
from flask_app.database import create_db
# Initialize database context
init_security()
# Drop and create tables
create_db()
if args.testing:
with open(config_path + 'configuration.py', 'a') as test_config:
test_config.write('\nTESTING = True\n')
copy_secret_config()
if args.quiet:
generate_new_secret()
initialize_db()
else:
if os.path.isfile(config_path + 'secret_key.py'):
# Ask for permission to overwrite
input_answer = raw_input('Generate new secret key and overwrite existing? (y, n)\n')
if input_answer.strip() == 'y':
generate_new_secret(True)
else:
generate_new_secret()
input_answer = raw_input('Initialize database (drops all tables)? (y, n)\n')
if input_answer.strip() == 'y':
initialize_db()