Repository navigation
Expand file tree
/
Copy pathSetup.py
More file actions
414 lines (334 loc) · 14 KB
/
Copy pathSetup.py
File metadata and controls
414 lines (334 loc) · 14 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# Copyright(c) 2008-2013 pyLoad Team
# http://www.pyload.org
#
# This file is part of pyLoad.
# pyLoad is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Subjected to the terms and conditions in LICENSE
#
# @author: RaNaN
###############################################################################
import os
import sys
import socket
import webbrowser
from getpass import getpass
from time import time
from sys import exit
from pyload.utils import pylgettext as gettext
from pyload.utils.fs import abspath, dirname, exists, join, makedirs
from pyload.utils import get_console_encoding
from pyload.web.ServerThread import WebServer
from system import get_system_info
from dependencies import deps
class Setup():
"""
pyLoads initial setup configuration assistant
"""
@staticmethod
def check_system():
return get_system_info()
@staticmethod
def check_deps():
result = {
"core": [],
"opt": []
}
for d in deps:
avail, v = d.check()
check = {
"name": d.name,
"avail": avail,
"v": v
}
if d.optional:
result["opt"].append(check)
else:
result["core"].append(check)
return result
def __init__(self, path, config):
self.path = path
self.config = config
self.stdin_encoding = get_console_encoding(sys.stdin.encoding)
self.lang = None
self.db = None
# We will create a timestamp so that the setup will be completed in a specific interval
self.timestamp = time()
# TODO: probably unneeded
self.yes = "yes"
self.no = "no"
def start(self):
import __builtin__
# set the gettext translation
__builtin__._ = lambda x: x
web = WebServer(pysetup=self)
web.start()
error = web.check_error()
# TODO: start cli in this case
if error: #todo errno 44 port already in use
print error
url = "http://%s:%d/" % (socket.gethostbyname(socket.gethostname()), web.port)
print "Setup is running at %s" % url
opened = webbrowser.open_new_tab(url)
if not opened:
print "Please point your browser to the url above."
cli = self.ask("Use commandline for configuration instead?", self.no, bool=True)
if cli:
print "Not implemented yet!"
print "Use web configuration or config files"
raw_input()
return True
def start_cli(self):
self.ask_lang()
print _("Welcome to the pyLoad Configuration Assistent.")
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 assistent with --setup or -s parameter, when you start pyLoadCore.")
print _("If you have any problems with this assistent hit CTRL+C,")
print _("to abort and don't let him start with pyLoadCore automatically anymore.")
print ""
print _("When you are ready for system check, hit enter.")
raw_input()
# TODO: new system check + deps
con = self.ask(_("Continue with setup?"), self.yes, bool=True)
if not con:
return False
print ""
print _("Do you want to change the config path? Current is %s") % abspath("")
print _(
"If you use pyLoad on a server or the home partition lives on an internal flash it may be a good idea to change it.")
path = self.ask(_("Change config path?"), self.no, bool=True)
if path:
self.conf_path()
#calls exit when changed
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:
self.conf_basic()
if ssl:
print ""
print _("Do you want to configure ssl?")
ssl = self.ask(_("Configure ssl?"), self.no, bool=True)
if ssl:
self.conf_ssl()
print ""
print _("Do you want to configure webinterface?")
web = self.ask(_("Configure webinterface?"), self.yes, bool=True)
if web:
self.conf_web()
print ""
print _("Setup finished successfully.")
print _("Hit enter to exit and restart pyLoad")
raw_input()
return True
def conf_basic(self):
print ""
print _("## Basic Setup ##")
print ""
print _("The following logindata is valid for CLI, GUI and webinterface.")
from pyload.database import DatabaseBackend
db = DatabaseBackend(None)
db.setup()
username = self.ask(_("Username"), "User")
password = self.ask("", "", password=True)
db.addUser(username, password)
db.shutdown()
print ""
langs = self.config.getMetaData("general", "language")
self.config["general"]["language"] = self.ask(_("Language"), "en", langs.type.split(";"))
self.config["general"]["download_folder"] = self.ask(_("Download folder"), "Downloads")
self.config["download"]["max_downloads"] = self.ask(_("Max parallel downloads"), "3")
reconnect = self.ask(_("Use Reconnect?"), self.no, bool=True)
self.config["reconnect"]["activated"] = reconnect
if reconnect:
self.config["reconnect"]["method"] = self.ask(_("Reconnect script location"), "./reconnect.sh")
def conf_web(self):
print ""
print _("## Webinterface Setup ##")
print ""
self.config["webinterface"]["activated"] = self.ask(_("Activate webinterface?"), self.yes, bool=True)
print ""
print _("Listen address, if you use 127.0.0.1 or localhost, the webinterface will only accessible locally.")
self.config["webinterface"]["host"] = self.ask(_("Address"), "0.0.0.0")
self.config["webinterface"]["port"] = self.ask(_("Port"), "8000")
print ""
print _("pyLoad offers several server backends, now following a short explanation.")
print "threaded:", _("Default server, this server offers SSL and is a good alternative to builtin.")
print "fastcgi:", _(
"Can be used by apache, lighttpd, requires you to configure them, which is not too easy job.")
print "lightweight:", _("Very fast alternative written in C, requires libev and linux knowledge.")
print "\t", _("Get it from here: https://github.com/jonashaag/bjoern, compile it")
print "\t", _("and copy bjoern.so to pyload/lib")
print
print _(
"Attention: In some rare cases the builtin server is not working, if you notice problems with the webinterface")
print _("come back here and change the builtin server to the threaded one here.")
self.config["webinterface"]["server"] = self.ask(_("Server"), "threaded",
["builtin", "threaded", "fastcgi", "lightweight"])
def conf_ssl(self):
print ""
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.")
self.config["ssl"]["activated"] = self.ask(_("Activate SSL?"), self.yes, bool=True)
def set_user(self):
gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
translation = gettext.translation("setup", join(self.path, "locale"),
languages=[self.config["general"]["language"], "en"], fallback=True)
translation.install(True)
self.openDB()
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)
self.db.addUser(username, password)
elif action == "2":
print ""
print _("Users")
print "-----"
users = self.db.getAllUserData()
for user in users.itervalues():
print user.name
print "-----"
print ""
elif action == "3":
print ""
username = self.ask(_("Username"), "")
if username:
self.db.removeUserByName(username)
elif action == "4":
self.db.syncSave()
break
finally:
self.closeDB()
def addUser(self, username, password):
self.openDB()
try:
self.db.addUser(username, password)
finally:
self.closeDB()
def openDB(self):
from pyload.database import DatabaseBackend
if self.db is None:
self.db = DatabaseBackend(None)
self.db.setup()
def closeDB(self):
if self.db is not None:
self.db.syncSave()
self.db.shutdown()
def save(self):
self.config.save()
self.closeDB()
def conf_path(self, trans=False):
if trans:
gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
translation = gettext.translation("setup", join(self.path, "locale"),
languages=[self.config["general"]["language"], "en"], fallback=True)
translation.install(True)
print _("Setting new configpath, current configuration will not be transferred!")
path = self.ask(_("Config path"), abspath(""))
try:
path = join(pypath, path)
if not exists(path):
makedirs(path)
f = open(join(pypath, "pyload", "config", "configdir"), "wb")
f.write(path)
f.close()
print _("Config path changed, setup will now close, please restart to go on.")
print _("Press Enter to exit.")
raw_input()
exit()
except Exception, e:
print _("Setting config path failed: %s") % str(e)
def ask_lang(self):
langs = self.config.getMetaData("general", "language").type.split(";")
self.lang = self.ask(u"Choose your Language / Wähle deine Sprache", "en", langs)
gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None])
translation = gettext.translation("setup", join(self.path, "locale"), languages=[self.lang, "en"], fallback=True)
translation.install(True)
#l10n Input shorthand for yes
self.yes = _("y")
#l10n Input shorthand for no
self.no = _("n")
def ask(self, qst, default, answers=[], bool=False, password=False):
""" Generate dialog on command line """
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
while p1 != p2:
# getpass(_("Password: ")) will crash on systems with broken locales (Win, NAS)
sys.stdout.write(_("Password: "))
p1 = getpass("")
if len(p1) < 4:
print _("Password too short. Use at least 4 symbols.")
continue
sys.stdout.write(_("Password (again): "))
p2 = getpass("")
if p1 == p2:
return p1
else:
print _("Passwords did not match.")
while True:
input = raw_input(qst + " %s: " % info)
input = input.decode(self.stdin_encoding)
if input.strip() == "":
input = default
if bool:
#l10n yes, true,t are inputs for booleans with value true
if input.lower().strip() in [self.yes, _("yes"), _("true"), _("t"), "yes"]:
return True
#l10n 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")
continue
if not answers:
return input
else:
if input in answers:
return input
else:
print _("Invalid Input")
if __name__ == "__main__":
test = Setup(join(abspath(dirname(__file__)), ".."), None)
test.start()