Repository navigation
Expand file tree
/
Copy pathSetup.py
More file actions
565 lines (453 loc) · 18.6 KB
/
Copy pathSetup.py
File metadata and controls
565 lines (453 loc) · 18.6 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# -*- coding: utf-8 -*-
# @author: vuolter
from __future__ import with_statement
import __builtin__
import getpass
import os
import subprocess
import sys
from pyload.network.JsEngine import JsEngine
from pyload.utils import get_console_encoding, load_translation, fs_join, versiontuple
class SetupAssistant(object):
""" pyLoads initial setup configuration assistant """
def __init__(self, config):
self.config = config
self.lang = "en"
self.stdin_encoding = get_console_encoding(sys.stdin.encoding)
# Input shorthand for yes
self.yes = "y"
# Input shorthand for no
self.no = "n"
def start(self):
print
langs = sorted(self.config.getMetaData("general", "language")['type'].split(";"))
self.lang = self.ask(u"Choose setup language", "en", langs)
load_translation("setup", self.lang)
# Input shorthand for yes
self.yes = _("y")
# Input shorthand for no
self.no = _("n")
# print
# print _("Would you like to configure pyLoad via Webinterface?")
# print _("You need a Browser and a connection to this PC for it.")
# viaweb = self.ask(_("Start initial webinterface for configuration?"), "y", bool=True)
# ...
print
print
print _("## Welcome to the pyLoad Configuration Assistant ##")
print
print _("It will check your system and make a basic setup in order to run pyLoad.")
print
print _("The value in brackets [] always is the default value,")
print _("in case you don't want to change it or you are unsure what to choose, just hit enter.")
print _(
"Don't forget: You can always rerun this assistant with --setup or -s parameter, when you start pyload.py .")
print _("If you have any problems with this assistant hit STRG-C,")
print _("to abort and don't let him start with pyload.py automatically anymore.")
print
print
raw_input(_("When you are ready for system check, hit enter."))
print
print
basic, ssl, captcha, web, js = self.system_check()
print
print
if not basic:
print _("You need pycurl, sqlite and python 2.5, 2.6 or 2.7 to run pyLoad.")
print _("Please correct this and re-run pyLoad.")
print
print _("Setup will now close.")
print
print
raw_input(_("Press Enter to exit."))
return False
raw_input(_("System check finished, hit enter to see your status report."))
print
print
print _("## Status ##")
print
avail = []
if self.check_module("Crypto"):
avail.append(_("- container decrypting"))
if ssl:
avail.append(_("- ssl connection"))
if captcha:
avail.append(_("- automatic captcha decryption"))
if web:
avail.append(_("- webinterface"))
if js:
avail.append(_("- extended Click'N'Load"))
if avail:
print _("AVAILABLE FEATURES:")
for feature in avail:
print feature
print
if len(avail) < 5:
print _("MISSING FEATURES:")
if not self.check_module("Crypto"):
print _("- no py-crypto available")
print _("You need this if you want to decrypt container files.")
print
if not ssl:
print _("- no SSL available")
print _("This is needed if you want to establish a secure connection to core or webinterface.")
print _("If you only want to access locally to pyLoad ssl is not usefull.")
print
if not captcha:
print _("- no Captcha Recognition available")
print _("Only needed for some hosters and as freeuser.")
print
if not js:
print _("- no JavaScript engine found")
print _("You will need this for some Click'N'Load links. Install Spidermonkey, ossp-js, pyv8 or rhino")
print
print
print _("You can abort the setup now and fix some dependicies if you want.")
else:
print _("NO MISSING FEATURES!")
print
print
con = self.ask(_("Continue with setup?"), self.yes, bool=True)
if not con:
return False
print
print
print _("CURRENT CONFIG PATH: %s") % configdir
print
print _("NOTE: If you use pyLoad on a server or the home partition lives on an iternal flash it may be a good idea to change it.")
confpath = self.ask(_("Do you want to change the config path?"), self.no, bool=True)
if confpath:
print
self.conf_path()
print
print
print _("Do you want to configure login data and basic settings?")
print _("This is recommend for first run.")
con = self.ask(_("Make basic setup?"), self.yes, bool=True)
if con:
print
print
self.conf_basic()
if ssl:
print
print _("Do you want to configure ssl?")
ssl = self.ask(_("Configure ssl?"), self.no, bool=True)
if ssl:
print
print
self.conf_ssl()
if web:
print
print _("Do you want to configure webinterface?")
web = self.ask(_("Configure webinterface?"), self.yes, bool=True)
if web:
print
print
self.conf_web()
print
print
print _("Setup finished successfully!")
print
print
raw_input(_("Hit enter to exit and restart pyLoad."))
return True
def system_check(self):
""" make a systemcheck and return the results """
import platform
print _("## System Information ##")
print
print _("Platform: ") + platform.platform(aliased=True)
print _("OS: ") + platform.system() or "Unknown"
print _("Python: ") + sys.version.replace("\n", "")
print
print
print _("## System Check ##")
print
if (2, 5) > sys.version_info > (2, 7):
python = False
else:
python = True
self.print_dep("python", python, false="NOT OK")
curl = self.check_module("pycurl")
self.print_dep("pycurl", curl)
sqlite = self.check_module("sqlite3")
self.print_dep("sqlite3", sqlite)
basic = python and curl and sqlite
print
crypto = self.check_module("Crypto")
self.print_dep("pycrypto", crypto)
ssl = self.check_module("OpenSSL")
self.print_dep("py-OpenSSL", ssl)
print
pil = self.check_module("Image")
self.print_dep("py-imaging", pil)
if os.name == "nt":
tesser = self.check_prog([path.join(pypath, "tesseract", "tesseract.exe"), "-v"])
else:
tesser = self.check_prog(["tesseract", "-v"])
self.print_dep("tesseract", tesser)
captcha = pil and tesser
print
try:
import jinja2
v = jinja2.__version__
if v and versiontuple(v) < (2, 5, 0):
jinja = False
else:
jinja = True
except Exception:
jinja = False
jinja_error = "MISSING"
else:
jinja_error = "NOT OK"
self.print_dep("jinja2", jinja, false=jinja_error)
beaker = self.check_module("beaker")
self.print_dep("beaker", beaker)
bjoern = self.check_module("bjoern")
self.print_dep("bjoern", bjoern)
web = sqlite and beaker
js = bool(JsEngine.find())
self.print_dep(_("JS engine"), js)
if not python:
print
print
if sys.version_info > (2, 7):
print _("WARNING: Your python version is too NEW!")
print _("Please use Python version 2.6/2.7 .")
else:
print _("WARNING: Your python version is too OLD!")
print _("Please use at least Python version 2.5 .")
if not jinja and jinja_error == "NOT OK":
print
print
print _("WARNING: Your installed jinja2 version %s is too OLD!") % jinja2.__version__
print _("You can safely continue but if the webinterface is not working,")
print _("please upgrade or uninstall it, because pyLoad self-includes jinja2 libary.")
return basic, ssl, captcha, web, js
def conf_basic(self):
print _("## Basic Setup ##")
print
print _("The following logindata is valid for CLI and webinterface.")
from pyload.Database import DatabaseBackend
db = DatabaseBackend(None)
db.setup()
print _("NOTE: Consider a password of 10 or more symbols if you expect to access to your local network from outside (ex. internet).")
print
username = self.ask(_("Username"), "User")
password = self.ask("", "", password=True)
db.addUser(username, password)
db.shutdown()
print
print
print _("External clients (GUI, CLI or other) need remote access to work over the network.")
print _("However, if you only want to use the webinterface you may disable it to save ram.")
self.config.set("remote", "activated", self.ask(_("Enable remote access"), self.no, bool=True))
print
langs = sorted(self.config.getMetaData("general", "language")['type'].split(";"))
self.config.set("general", "language", self.ask(_("Choose system language"), self.lang, langs))
print
self.config.set("general", "download_folder", self.ask(_("Download folder"), "Downloads"))
print
self.config.set("download", "max_downloads", self.ask(_("Max parallel downloads"), "3"))
print
reconnect = self.ask(_("Use Reconnect?"), self.no, bool=True)
self.config.set("reconnect", "activated", reconnect)
if reconnect:
self.config.set("reconnect", "method", self.ask(_("Reconnect script location"), "./reconnect.sh"))
def conf_web(self):
print _("## Webinterface Setup ##")
print
print _("Listen address, if you use 127.0.0.1 or localhost, the webinterface will only accessible locally.")
self.config.set("webui", "host", self.ask(_("Address"), "0.0.0.0"))
self.config.set("webui", "port", self.ask(_("Port"), "8000"))
print
print _("pyLoad offers several server backends, now following a short explanation.")
print "- auto:", _("Automatically choose the best webserver for your platform.")
print "- builtin:", _("First choice if you plan to use pyLoad just for you.")
print "- threaded:", _("Support SSL connection and can serve simultaneously more client flawlessly.")
print "- fastcgi:", _(
"Can be used by apache, lighttpd, etc.; needs to be properly configured before.")
if os.name != "nt":
print "- lightweight:", _("Very fast alternative to builtin; requires libev and bjoern packages.")
print
print _("NOTE: In some rare cases the builtin server not works correctly, so if you have troubles with the web interface")
print _("run this setup assistant again and change the builtin server to the threaded.")
if os.name == "nt":
servers = ["auto", "builtin", "threaded", "fastcgi"]
else:
servers = ["auto", "builtin", "threaded", "fastcgi", "lightweight"]
self.config.set("webui", "server", self.ask(_("Choose webserver"), "auto", servers))
def conf_ssl(self):
print _("## SSL Setup ##")
print
print _("Execute these commands from pyLoad config folder to make ssl certificates:")
print
print "openssl genrsa -out ssl.key 1024"
print "openssl req -new -key ssl.key -out ssl.csr"
print "openssl req -days 36500 -x509 -key ssl.key -in ssl.csr > ssl.crt "
print
print _("If you're done and everything went fine, you can activate ssl now.")
ssl = self.ask(_("Activate SSL?"), self.yes, bool=True)
self.config.set("remote", "ssl", ssl)
self.config.set("webui", "ssl", ssl)
def set_user(self):
load_translation("setup", self.config.get("general", "language"))
from pyload.Database import DatabaseBackend
db = DatabaseBackend(None)
db.setup()
noaction = True
try:
while True:
print _("Select action")
print _("1 - Create/Edit user")
print _("2 - List users")
print _("3 - Remove user")
print _("4 - Quit")
action = raw_input("1/2/3/4: ")
if not action in ("1", "2", "3", "4"):
continue
elif action == "1":
print
username = self.ask(_("Username"), "User")
password = self.ask("", "", password=True)
db.addUser(username, password)
noaction = False
elif action == "2":
print
print _("Users")
print "-----"
users = db.listUsers()
noaction = False
for user in users:
print user
print "-----"
print
elif action == "3":
print
username = self.ask(_("Username"), "")
if username:
db.removeUser(username)
noaction = False
elif action == "4":
break
finally:
if not noaction:
db.shutdown()
def set_configdir(self, configdir, persistent=False):
dirname = os.path.abspath(configdir)
try:
if not os.path.exists(os.path.dirname):
os.makedirs(os.path.dirname, 0700)
os.chdir(os.path.dirname)
if persistent:
c = os.path.join(rootdir, "config", "configdir")
if not os.path.exists(c):
os.makedirs(c, 0700)
with open(c, "wb") as f:
f.write(os.path.dirname)
except IOError:
return False
else:
__builtin__.configdir = dirname
return dirname #: return always abspath
def conf_path(self):
print _("Setting new config path.")
print _("NOTE: Current configuration will not be transfered!")
while True:
confdir = self.ask(_("CONFIG PATH"), configdir)
confpath = self.set_configdir(confdir)
print
if not confpath:
print _("Failed to change the current CONFIG PATH!")
print
else:
print _("CONFIG PATH successfully changed to: %s") % configdir
break
def print_dep(self, name, value, false="MISSING", true="OK"):
""" Print Status of dependency """
if value and isinstance(value, basestring):
info = ", ".join(value)
else:
info = ""
print "%(dep)-12s %(bool)s (%(info)s)" % {'dep': name + ':',
'bool': _(true if value else false).upper(),
'info': info}
def check_module(self, module):
try:
__import__(module)
return True
except Exception:
return False
def check_prog(self, command):
pipe = subprocess.PIPE
try:
subprocess.call(command, stdout=pipe, stderr=pipe)
return True
except Exception:
return False
def ask(self, qst, default, answers=[], bool=False, password=False):
""" produce one line to asking for input """
if answers:
info = "("
for i, answer in enumerate(answers):
info += (", " if i != 0 else "") + str((answer == default and "[%s]" % answer) or answer)
info += ")"
elif bool:
if default == self.yes:
info = "([%s]/%s)" % (self.yes, self.no)
else:
info = "(%s/[%s])" % (self.yes, self.no)
else:
info = "[%s]" % default
if password:
p1 = True
p2 = False
pwlen = 8
while p1 != p2:
sys.stdout.write(_("Password: "))
p1 = getpass.getpass("").strip("\n\r")
if len(p1) < pwlen:
print
print _("Password too short! Use at least %s symbols." % pwlen)
print
continue
elif not p1.isalnum():
print
print _("Password must be alphanumeric.")
print
continue
sys.stdout.write(_("Password (again): "))
p2 = getpass.getpass("").strip("\n\r")
if p1 == p2:
print
if self.ask(_("Show password?"), self.no, bool=True):
print
print _("Your Password is: %s") % p1
return p1
else:
print
print _("Passwords did not match.")
while True:
try:
input = raw_input(qst + " %s: " % info)
except KeyboardInterrupt:
print "\nSetup interrupted"
sys.exit()
input = input.decode(self.stdin_encoding)
if input.strip() == "":
input = default
if bool:
# yes, true, t are inputs for booleans with value true
if input.lower().strip() in [self.yes, _("yes"), _("true"), _("t"), "yes"]:
return True
# no, false, f are inputs for booleans with value false
elif input.lower().strip() in [self.no, _("no"), _("false"), _("f"), "no"]:
return False
else:
print _("Invalid Input")
print
continue
if not answers or input in answers:
return input
else:
print _("Invalid Input")