-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate_local_settings.py
43 lines (33 loc) · 1.53 KB
/
create_local_settings.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
#!/usr/bin/env python3
import codecs
import os
import random
import shutil
import string
import tempfile
LOCAL_SETTINGS_PATH = './care/local_settings.py'
LOCAL_SETTINGS_EXAMPLE_PATH = './care/local_settings_example.py'
def main():
if os.path.exists(LOCAL_SETTINGS_PATH):
print('ERROR: ' + LOCAL_SETTINGS_PATH +
' already exists! Please remove this file manually if you intent to overwrite it.')
return
shutil.copyfile(LOCAL_SETTINGS_EXAMPLE_PATH, LOCAL_SETTINGS_PATH)
secret_key_random = generate_random_secret_key()
replace(LOCAL_SETTINGS_PATH, "SECRET_KEY = ''", "SECRET_KEY = '" + secret_key_random + "'")
def replace(source_file_path, pattern, substring):
fh, target_file_path = tempfile.mkstemp()
with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
for line in source_file:
target_file.write(line.replace(pattern, substring))
os.remove(source_file_path)
shutil.move(target_file_path, source_file_path)
def generate_random_secret_key():
# source: https://gist.github.com/mattseymour/9205591
# Get ascii Characters numbers and punctuation (minus quote characters as they could terminate string).
chars = ''.join([string.ascii_letters, string.digits, string.punctuation]).replace('\'', '').replace('"', '').replace('\\', '')
secret_key = ''.join([random.SystemRandom().choice(chars) for i in range(50)])
return secret_key
if __name__ == "__main__":
main()