install.py
11.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os, sys, subprocess, platform, fileinput
DEBUG = False
DB_USER = "pyros"
DB_PASSWORD = "DjangoPyros"
SQL_SCRIPT = "CREATE DATABASE IF NOT EXISTS pyros; CREATE USER IF NOT EXISTS pyros; GRANT USAGE ON *.* TO '"+DB_USER+"'; DROP USER '"+DB_USER+"'; GRANT ALL ON pyros.* TO '"+DB_USER+"'@'localhost' IDENTIFIED BY '"+DB_PASSWORD+"'; GRANT ALL ON test_pyros.* TO '"+DB_USER+"'@'localhost'"
'''
Color class
'''
class Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
'''
Python installer class
'''
class Installer:
path = os.path.realpath(__file__)
path_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
python_path = sys.executable
python_version = sys.version_info
manage = " manage.py"
requirements = "REQUIREMENTS.txt"
venv_windows = "..\\venv\\Scripts\\"
venv_linux = "../venv/bin/"
current_command = "default"
system = platform.system()
yes_no_questions = {}
row = 1000
columns = 100
commands = []
commands_list = []
commandMatcher = []
toprint = []
executed = {}
errors = {}
def __init__(self):
os.chdir(self.path_dir)
self.commandMatcher = {"install": self.install, "update": self.update}
if (self.python_version.major != 3):
self.printColor(Colors.FAIL, "This installer must be executed with python3.")
sys.exit(2)
if self.system == 'Windows':
self.venv_pip = self.path_dir + self.venv_windows + "pip.exe"
self.venv_bin = self.path_dir + self.venv_windows + "python.exe"
self.requirements = "REQUIREMENTS_WINDOWS.txt"
else:
try:
rows, columns = os.popen('stty size', 'r').read().split()
self.columns = int(columns)
except:
self.columns = 100
if DEBUG:
print("Could not get terminal size")
self.venv_pip = self.path_dir + self.venv_linux + "pip"
self.venv_bin = self.path_dir + self.venv_linux + "python"
if DEBUG:
print(self.venv_bin)
print(self.venv_pip)
'''
Functions parameters
'''
def install(self) -> int:
if (self.execProcess("pip install --upgrade pip")):
return 1
if (self.execProcess("pip install virtualenv --upgrade")):
return 1
self.changeDirectory("..")
if (not os.path.isdir("venv")):
self.execProcess("virtualenv venv" + " -p " + self.python_path)
else:
self.printFullTerm(Colors.WARNING, "Virtual environment already exist")
self.changeDirectory(self.path_dir)
ret = self.installDatabase()
if (ret):
return ret
return self.update()
def update(self) -> int:
if (self.execProcessFromVenv(self.venv_pip + " install --upgrade pip") or
self.execProcessFromVenv(self.venv_pip + " install --upgrade wheel") or
self.execProcessFromVenv(self.venv_pip + " install -r " + self.requirements)):
return 1
return self.updateDatabase()
def installDatabase(self):
if ("database" not in self.yes_no_questions):
self.askYesNoQuestion("Do you wish to use mysql in your project ? only if mysql is installed", question_tag="database", default=False)
if (self.yes_no_questions["database"]):
username = self.askQuestion("MYSQL Username", default="root")
if (self.execProcess("echo \""+SQL_SCRIPT+"\" |mysql -u "+username+" -p") or
self.replacePatternInFile("MYSQL = False", "MYSQL = True", "../src/pyros/settings.py")):
return 1
return 0
def updateDatabase(self):
context = os.getcwd()
self.changeDirectory("../src/")
if ("database" not in self.yes_no_questions):
self.askYesNoQuestion("Do you wish to use mysql in your project ? only if mysql is installed", question_tag="database", default=False)
if (self.yes_no_questions["database"]):
if (self.execProcessFromVenv(self.venv_pip + " install mysqlclient==1.3.7")):
return 1
if (self.execProcessFromVenv(self.venv_bin + self.manage + " makemigrations") or
self.execProcessFromVenv(self.venv_bin + self.manage + " migrate") or
self.execProcessFromVenv(self.venv_bin + self.manage + " loaddata misc/fixtures/initial_fixture.json")):
return 1
self.changeDirectory(context)
return 0
'''
UTILS FUNCTIONS
'''
def execProcess(self, command: str) -> int:
self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]")
process = subprocess.Popen(command, shell=True)
process.wait()
if process.returncode == 0:
self.printFullTerm(Colors.GREEN, "Process executed successfully")
if (self.current_command in self.executed):
self.executed[self.current_command].append(command)
else:
self.executed[self.current_command] = [command]
else:
self.printFullTerm(Colors.WARNING, "Process execution failed")
if (self.current_command in self.errors):
self.errors[self.current_command].append(command)
else:
self.errors[self.current_command] = [command]
return process.returncode
def execProcessFromVenv(self, command: str) -> int:
args = command.split()
self.printFullTerm(Colors.BLUE, "Executing command [" + str(' '.join(args[1:])) + "]")
process = subprocess.Popen(args)
process.wait()
if process.returncode == 0:
self.printFullTerm(Colors.GREEN, "Process executed successfully")
if (self.current_command in self.executed):
self.executed[self.current_command].append(str(' '.join(args[1:])))
else:
self.executed[self.current_command] = [str(' '.join(args[1:]))]
else:
self.printFullTerm(Colors.WARNING, "Process execution failed")
if (self.current_command in self.errors):
self.errors[self.current_command].append(str(' '.join(args[1:])))
else:
self.errors[self.current_command] = [str(' '.join(args[1:]))]
return process.returncode
def changeDirectory(self, path: str) -> int:
if DEBUG:
print("Moving to : " + path)
os.chdir(path)
if DEBUG:
print("Current directory : " + str(os.getcwd()))
return 0
def replacePatternInFile(self, pattern: str, replace: str, file_path: str) -> int:
try:
with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(pattern, replace), end='')
except:
return 1
return 0
def printColor(self, color, message: str, file=sys.stdout, eol=os.linesep):
if (self.system == 'Windows'):
print(message, file=file, end=eol)
else:
print(color + message + Colors.ENDC, file=file, end=eol)
return 0
def askYesNoQuestion(self, message: str, question_tag: str, default: bool = True) -> bool:
print(os.linesep * 2)
self.printColor(Colors.BLUE, message + ": ", eol='')
self.printColor(Colors.WARNING, "(default: " + ("Yes" if default else "No") + ") (Y/N)")
self.printColor(Colors.BOLD, "Answer : ", eol='')
sys.stdout.flush()
answer = sys.stdin.readline().replace(os.linesep, '')
if (answer == "Y"):
self.yes_no_questions[question_tag] = True
return True
if (answer == ""):
self.yes_no_questions[question_tag] = default
return default
self.yes_no_questions[question_tag] = False
return False
def askQuestion(self, message: str, default: str = ""):
self.printColor(Colors.BLUE, message)
self.printColor(Colors.BOLD, "Answer (default="+default+"): ", eol='')
sys.stdout.flush()
ret = sys.stdin.readline().replace(os.linesep, '')
if ret == "":
return default
return ret
def addToPrint(self, message: str) -> int:
self.toprint.append(message)
return 0
def printFullTerm(self, color, string):
value = int(self.columns / 2 - len(string) / 2)
self.printColor(color, "-" * value, eol='')
self.printColor(color, string, eol='')
value += len(string)
self.printColor(color, "-" * (self.columns - value))
return 0
'''
CORE
'''
def feed(self, array) -> int:
self.commands_list = self.commands_list + array
return 0
def parse(self) -> int:
try:
for command in self.commands_list:
if not isinstance(command, str):
raise Exception("Invalid parameter + " + str(command))
self.commands.append(command)
except Exception as e:
self.logError("An exception has been raised : " + str(e))
return 1
return 0
def end(self) -> int:
count = 0
self.printFullTerm(Colors.WARNING, "Summary")
self.printColor(Colors.GREEN, "Success : ")
for command, valid in self.executed.items():
if not valid:
self.printColor(Colors.BLUE, "\t- Command : " + command + " successfully executed !")
else:
self.printColor(Colors.WARNING, "\t- In commmand : " + command)
for exe in valid:
self.printColor(Colors.GREEN, "\t\t - Command : " + exe + " successfully executed ! ")
self.printColor(Colors.FAIL, "Errors : ")
for command, items in self.errors.items():
count += 1
if (not items):
self.printColor(Colors.FAIL, "Command : " + command + " failed !")
else:
self.printColor(Colors.WARNING, "\t- In commmand : " + command)
for exe in items:
self.printColor(Colors.FAIL, "\t\t - Command : " + exe + " failed.")
for message in self.toprint:
self.printColor(Colors.BOLD, message)
return count
def exec(self) -> int:
for command in self.commands:
if command in self.commandMatcher:
self.current_command = command
self.commandMatcher[command]()
else:
self.logError("Invalid command : " + str(command))
self.errors[command] = []
return self.end()
def logError(self, message: str) -> int:
self.printColor(Colors.FAIL, "Installer : An error occurred [" + message + "]", file=sys.stderr)
return 0
if __name__ == "__main__":
installer = Installer()
installer.feed(sys.argv[1:])
installer.addToPrint("Please run 'python pyrosrun.py server' then go to localhost:8000/admin and log in with these id")
installer.addToPrint("Username : " + DB_USER + " / Password : " + DB_PASSWORD)
if installer.parse() == 0:
sys.exit(installer.exec())
sys.exit(1)