-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.py
More file actions
429 lines (344 loc) · 8.91 KB
/
installer.py
File metadata and controls
429 lines (344 loc) · 8.91 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#import python dependencies
import os, shutil, sys, socket, platform
#import third-party dependencies
import requests
from PyQt4 import QtGui, QtCore
#settings
caption = 'FreeDiscovery Installer'
width = 520
height = 240
deps = { 'python':'Python',
'numpy':'Numpy',
'scipy':'Scipy',
'scikit':'SciKit Learn',
'pathlib': 'Pathlib'
}
#system
app = None
window = None
font = None
#network
def dl_loc (name):
get = os.getcwd().replace('\\', '/')
if '/' not in get: get = '.'
return get + '/' + name
def download (From, to):
dfile = open(dl_loc(to), 'wb')
req = requests.get(From, stream=True)
raw = req.raw
while True:
get = raw.read()
if get: dfile.write(get)
else: break
#gui
widgets = []
def label (x, y, text):
new = QtGui.QLabel(window)
new.setText(text)
new.setFont(font)
new.move(x, y)
new.show()
widgets.append(new)
return new
section_x = 10
section_y = 10
def section (text, x = 0):
global section_x, section_y
sect = label(section_x + x, section_y, text)
section_y += sect.height()
return sect
class check_section:
def __init__(c, checked, text):
c.checked = checked
c.text = text
c.sect = section(c.text)
def yes (c): c.checked = True
def no (c): c.checked = False
def bar (range_max=10):
new = QtGui.QProgressBar(window)
new.resize(width - 40, 20)
new.move(20, height - 50)
new.setValue(0)
new.setRange(0, range_max)
new.show()
widgets.append(new)
return new
def logo ():
pic = QtGui.QLabel(window)
#pic.setGeometry(10, 10, 400, 200)
pixmap = QtGui.QPixmap(logo_pix)
#pixmap = pixmap.scaledToHeight(200)
pic.setPixmap(pixmap)
pic.show()
widgets.append(pic)
return pic
def description (text):
new = label(0, 0, ''.join([line.strip() + '\n' for line in text.strip().split('\n')]).replace('TAB', '\t'))
return new
def next_button (text = 'Next'):
global width, height
new = QtGui.QPushButton(window)
new.setText(text)
new.move(width - new.width(), height - new.height())
new.show()
widgets.append(new)
return new
#main program structure
class error(Exception):
def __init__(e, message): e.message = message
def __str__(e): return e.message
glos = {}
state_keys = []
state_funcs = {}
def state (func):
'''
Decorator function to be able to expand later if needed. Right now,
functions are added. Each function represents a different "state" of
the installer. There is a "start" function for win the state starts,
and a "run" function for when the function is running.
'''
name = func.__name__.replace('_start', '').replace('_run', '')
if name not in state_keys: state_keys.append(name)
if name not in state_funcs: state_funcs[name] = {'mode':'start', 'values':{}}
state_funcs[name][func.__name__.replace(name + '_', '')] = func
return func
def clear ():
global sections, section_y
state_keys.pop(0)
while widgets:
widget = widgets.pop(0)
widget.setParent(None)
widget.destroy()
sections = []
section_y = 10
def main ():
global widgets, section_y, state_keys
'''
This is the main function for the installer. It runs all
of the state funcs saved. Designed to be modular.
'''
#check if it's still going
if state_keys:
#run
try:
dic = state_funcs[state_keys[0]]
if dic[dic['mode']]( dic['values'] ):
if dic['mode'] == 'start': dic['mode'] = 'run'
else: clear()
#error occurred!
except error as e:
clear()
state_keys = ['error']
state_funcs['error'] = {
'values': {'error' : e.message},
'mode': 'start',
'start': error_start,
'run': error_run,
}
#nope!
else: app.exit(1)
#error screen
def error_start (values):
section('Error:')
section(values['error'])
section('\nPlease try the installer again.')
values['exit'] = next_button('Exit')
return True
def error_run (values): return values['exit'].isDown()
#splash screen
@state
def splash_start (values):
#has admin rights?
if 'lin' in sys.platform and os.getuid() > 0:
raise error('This installer needs admin rights to run (sudo)')
#page
'The opening of the installer, introduces the project'
section('FreeDiscovery installer...')
values['next'] = next_button()
#finish
return True
@state
def splash_run (values): return values['next'].isDown()
#check dependencies splash
@state
def check_deps_splash_start (values):
section('Going to check for any dependencies that you may')
section('already have.')
section('* Python', 10)
section('* Numpy', 10)
section('* SciPy', 10)
section('* SciKit Learn', 10)
section('* Pathlib', 10)
section('Press "Next" to start the scan.', 10)
values['next'] = next_button()
return True
@state
def check_deps_splash_run (values): return values['next'].isDown()
#checking dependencies
@state
def check_deps_start (values):
section('Checking for dependencies')
for dep in deps:
values[dep] = check_section(False, deps[dep])
glos[dep + '_checked'] = False
glos[dep] = False
values['bar'] = bar(len(deps))
values['mode'] = ''
return True
@state
def check_deps_run (values):
#start
if values['mode'] == '':
values['mode'] = 'python'
return False
#check for python
elif values['mode'] == 'python':
#has python
if glos['python'] and os.path.exists('C:/Python35'):
values['bar'].setValue(1)
glos['python_check'] = True
values['mode'] = 'numpy'
return False
#else
else:
for dep in deps: values[dep + '_check'] = True
return True
#check for numpy
elif values['mode'] == 'numpy':
values['bar'].setValue(2)
glos['numpy_check'] = True
values['mode'] = 'scipy'
return False
#check for scipy
elif values['mode'] == 'scipy':
values['bar'].setValue(3)
glos['scipy_check'] = True
values['mode'] = 'scikit'
return False
#check for scikit
elif values['mode'] == 'scikit':
values['bar'].setValue(4)
glos['scikit_check'] = True
values['mode'] = 'pathlib'
return False
#check for pathlib
elif values['mode'] == 'pathlib':
values['bar'].setValue(5)
glos['pathlib_check'] = True
return True
#
def _install_dep (name): os.system('pip install ' + name)
@state
def install_deps_start (values):
#needs some dependencies
if any([not glos[name] for name in deps]):
glos['skip'] = False
section('Will now install the following dependencies:')
for name in deps:
if not glos[name]: section(deps[name])
#has everything!
else:
glos['skip'] = True
section('Looks like all dependencies are installed!')
section('Click "Next" to install FreeDiscovery.')
#finished
values['mode'] = ''
values['bar'] = bar(len(deps))
values['next'] = next_button()
return True
@state
def install_deps_run (values):
#
if glos['skip']: return True
else:
#start
if values['mode'] == '':
values['mode'] = 'python'
return False
#check for python
elif values['mode'] == 'python':
#install linux
if 'linux' in sys.platform: os.system('apt-get install python')
#install for windows
else:
if 'darwin' in sys.platform:
url = '3.5.0/python-3.5.0-macosx10.6.pkg'
name = 'pyinstall.pkg'
elif 'win' in sys.platform:
if '64' in platform.architecture()[0]: url = '3.5.0/python-3.5.0-amd64.exe'
else: url = '3.5.0/python-3.5.0.exe'
name = 'pyinstall.exe'
os.environ['path'] = os.environ['path'] + ';C:\\Python35'
else: raise error('Platform not supported')
py = download('https://www.python.org/ftp/python/' + url, name)
os.system(dl_loc(name))
os.remove(dl_loc(name))
#progress
values['bar'].setValue(1)
glos['python_check'] = True
values['mode'] = 'numpy'
return False
#check for numpy
elif values['mode'] == 'numpy':
_install_dep ('numpy')
#
values['bar'].setValue(2)
glos['numpy_check'] = True
values['mode'] = 'scipy'
return False
#check for scipy
elif values['mode'] == 'scipy':
_install_dep('scipy')
values['bar'].setValue(3)
glos['scipy_check'] = True
values['mode'] = 'scikit'
return False
#check for scikit
elif values['mode'] == 'scikit':
_install_dep('scikit-learn')
values['bar'].setValue(4)
glos['scikit_check'] = True
values['mode'] = 'pathlib'
return False
#check for pathlib
elif values['mode'] == 'pathlib':
_install_dep('pathlib')
values['bar'].setValue(5)
glos['pathlib_check'] = True
return True
#
@state
def install_freedisc_start (values):
section('Now installing FreeDiscovery...')
return True
@state
def install_freedisc_run (values):
_install_dep('freediscovery[engine]')
return True
#
@state
def complete_start (values):
section('FreeDiscovery has now been installed!')
section('Press "Exit" to finish')
values['exit'] = next_button('Exit')
return True
@state
def complete_run (values): return values['exit'].isDown()
#main program
if __name__ == '__main__':
#create QT app
app = QtGui.QApplication([])
#create Window
window = QtGui.QWidget()
window.resize(width, height)
window.setWindowTitle(caption)
window.show()
#make font
font = QtGui.QFont()
font.setPointSize(14)
#start process
process = QtCore.QTimer()
process.timeout.connect(main)
process.start(1)
#execute!
app.exec_()