This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
169 lines (154 loc) · 5.18 KB
/
setup.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from pwinput import pwinput
from subprocess import check_output
from os import environ, getcwd, unlink
from os.path import join, exists
import random
import string
SECRET_KEY_LENGTH = 50
questions = [
{
"text": "Do you want to use an easy mode (hides values you don't need when running official content)",
"default": "yes",
"key": "easy_mode",
"values": ["yes", "no"],
"is_hidden": False,
},
{
"text": "Name your username to be used",
"default": None,
"key": "user_name",
"values": [],
"is_hidden": False,
},
{
"text": "Name your password to be used",
"default": None,
"key": "user_pass",
"values": [],
"is_hidden": True,
},
{
"text": "Should APX use a global steamcmd instead of one per server?",
"default": "no",
"key": "global_steam",
"values": ["yes", "no"],
"is_hidden": False,
},
{
"text": "Do you want to support the project with adding the prefix '[APX]' to the server names?",
"default": "yes",
"key": "add_prefix",
"values": ["yes", "no"],
"is_hidden": False,
},
{
"text": "What database should be used? sqlite or mariadb?",
"default": "sqlite",
"key": "add_prefix",
"values": ["sqlite", "mariadb"],
"is_hidden": False,
},
{
"text": "Is APX allowed to to speedtest to identify the bandwith (will be done once on startup). Uses speedtest.net in the background.",
"default": "yes",
"key": "allow_speedtest",
"values": ["yes", "no"],
"is_hidden": False,
},
]
answers = {}
for question in questions:
key = question["key"]
text = question["text"]
default = question["default"]
values = question["values"]
question_text = question["text"] + ": "
is_hidden = question["is_hidden"]
reinsert_needed = False
if len(values) > 0:
got = input(question["text"] + f" {values}: ")
else:
if is_hidden:
got = pwinput(mask="*")
repeat = pwinput(prompt="Repeat: ", mask="*")
if got != repeat:
print("Passwords do not match!")
reinsert_needed = True
else:
reinsert_needed = False
else:
got = input(question_text)
if not got and default is None:
answers[key] = default
while (
len(values) > 0
and got.lower() not in values
or not got
and default is None
or reinsert_needed
):
if len(values) > 0:
print(f"Name a valid option. Options: {values}")
else:
print("Name a valid option!")
if len(values) > 0:
got = input(question["text"] + f" {values}: ")
else:
if is_hidden:
got = pwinput(prompt="Set password: ", mask="*")
repeat = pwinput(prompt="Repeat: ", mask="*")
if got != repeat:
print("Passwords do not match!")
reinsert_needed = True
else:
reinsert_needed = False
else:
got = input(question_text)
answers[key] = got
settings_path = join(getcwd(), "wizard", "settings.py.tpl")
new_content = []
with open(settings_path, "r", encoding="utf-8") as file:
content = file.readlines()
easy_mode = answers["easy_mode"] == "yes"
global_steam = answers["global_steam"] == "yes"
add_prefix = answers["add_prefix"] == "yes"
allow_speedtest = answers["allow_speedtest"] == "yes"
for line in content:
if "SECRET_KEY" in line:
random_key = "".join(
random.choice(
string.ascii_uppercase
+ string.ascii_lowercase
+ string.digits
+ string.punctuation
)
for i in range(SECRET_KEY_LENGTH)
).replace(
'"', ""
) # make sure we have no string delimiters in there
line = f'SECRET_KEY = "{random_key}"\n'
if "DEBUG" in line:
line = f"DEBUG = False\n"
if "EASY_MODE" in line:
line = f"EASY_MODE = {easy_mode}\n"
if "USE_GLOBAL_STEAMCMD" in line:
line = f"USE_GLOBAL_STEAMCMD = {global_steam}\n"
if "ADD_PREFIX" in line:
line = f"ADD_PREFIX = {add_prefix}\n"
if "SPEEDTEST_ALLOWED" in line:
line = f"SPEEDTEST_ALLOWED = {allow_speedtest}\n"
new_content.append(line)
settings_path = join(getcwd(), "wizard", "settings.py")
with open(settings_path, "w", encoding="utf-8") as file:
for line in new_content:
file.write(line)
django_env = dict(
environ,
DJANGO_SUPERUSER_USERNAME=answers["user_name"],
DJANGO_SUPERUSER_PASSWORD=answers["user_pass"],
)
got = check_output(
"python.exe manage.py createsuperuser --noinput --email apx@localhost",
env=django_env,
shell=True,
).decode("utf-8")