This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_config.py
64 lines (48 loc) · 1.77 KB
/
gen_config.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
from hashlib import sha256
from os import mkdir, urandom, getenv
from os.path import exists, join
from random import choices
from string import ascii_letters
from sys import argv, path
def hash(str, salt):
return sha256((str + salt).encode()).hexdigest()
def generaterPass(password, salt):
return hash(hash(password, salt), salt)
def generaterSalt():
return "".join(choices(ascii_letters, k=16))
def gen_config(password, url_prefix=''):
config = {}
cwd = path[0]
with open(join(cwd, 'file_mapping/config.example.py'), 'rb') as f:
exec(f.read(), None, config)
salt = generaterSalt()
config['LOGIN_SALT'] = salt
config['ADMIN_PASSWORD'] = generaterPass(password, salt)
config['SECRET_KEY'] = urandom(16)
config['URL_PREFIX'] = getenv('URL_PREFIX') or url_prefix
config['UPLOAD_PATH'] = getenv('UPLOAD_PATH') or join(cwd, 'uploads')
config['SQLALCHEMY_DATABASE_URI'] = getenv('SQLALCHEMY_DATABASE_URI') or config['SQLALCHEMY_DATABASE_URI']
proxy = False
if getenv('BEHIND_PROXY'):
proxy = getenv('BEHIND_PROXY')
if proxy.lower() == 'true' or proxy == '1':
proxy = True
else:
proxy = False
config['BEHIND_PROXY'] = proxy
with open(join(path[0], 'file_mapping/config.py'), 'w') as f:
for key in config:
f.write(f"{key} = ")
if type(config[key]) == str:
f.write(f"'{config[key]}'")
else:
f.write(f"{config[key]}")
f.write('\n')
if __name__ == '__main__':
if len(argv) != 2 and len(argv) != 3:
print(f'Usage: {argv[0]} password (url_prefix)')
else:
if len(argv) == 2:
gen_config(argv[1])
else:
gen_config(argv[1], argv[2])