forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseBackend.py
More file actions
500 lines (402 loc) · 14.5 KB
/
DatabaseBackend.py
File metadata and controls
500 lines (402 loc) · 14.5 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# Copyright(c) 2008-2012 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, mkaay
###############################################################################
from threading import Thread, Event
from shutil import move
from Queue import Queue
from traceback import print_exc
from pyload.utils.fs import chmod, exists, remove
try:
from pysqlite2 import dbapi2 as sqlite3
except:
import sqlite3
DB = None
DB_VERSION = 6
def set_DB(db):
global DB
DB = db
def queue(f):
@staticmethod
def x(*args, **kwargs):
if DB:
return DB.queue(f, *args, **kwargs)
return x
def async(f):
@staticmethod
def x(*args, **kwargs):
if DB:
return DB.async(f, *args, **kwargs)
return x
def inner(f):
@staticmethod
def x(*args, **kwargs):
if DB:
return f(DB, *args, **kwargs)
return x
class DatabaseMethods:
# stubs for autocompletion
core = None
manager = None
conn = None
c = None
@classmethod
def register(cls):
DatabaseBackend.registerSub(cls)
class DatabaseJob():
def __init__(self, f, *args, **kwargs):
self.done = Event()
self.f = f
self.args = args
self.kwargs = kwargs
self.result = None
self.exception = False
# import inspect
# self.frame = inspect.currentframe()
def __repr__(self):
from os.path import basename
frame = self.frame.f_back
output = ""
for i in range(5):
output += "\t%s:%s, %s\n" % (basename(frame.f_code.co_filename), frame.f_lineno, frame.f_code.co_name)
frame = frame.f_back
del frame
del self.frame
return "DataBase Job %s:%s\n%sResult: %s" % (self.f.__name__, self.args[1:], output, self.result)
def processJob(self):
try:
self.result = self.f(*self.args, **self.kwargs)
except Exception, e:
print_exc()
try:
print "Database Error @", self.f.__name__, self.args[1:], self.kwargs, e
except:
pass
self.exception = e
finally:
self.done.set()
def wait(self):
self.done.wait()
class DatabaseBackend(Thread):
subs = []
DB_FILE = "pyload.db"
VERSION_FILE = "db.version"
def __init__(self, core):
Thread.__init__(self)
self.setDaemon(True)
self.core = core
self.manager = None # set later
self.error = None
self.running = Event()
self.jobs = Queue()
set_DB(self)
def setup(self):
""" *MUST* be called before db can be used !"""
self.start()
self.running.wait()
def init(self):
"""main loop, which executes commands"""
version = self._checkVersion()
self.conn = sqlite3.connect(self.DB_FILE)
chmod(self.DB_FILE, 0600)
self.c = self.conn.cursor()
if version is not None and version < DB_VERSION:
success = self._convertDB(version)
# delete database
if not success:
self.c.close()
self.conn.close()
try:
self.manager.core.log.warning(_("Database was deleted due to incompatible version."))
except:
print "Database was deleted due to incompatible version."
remove(self.VERSION_FILE)
move(self.DB_FILE, self.DB_FILE + ".backup")
f = open(self.VERSION_FILE, "wb")
f.write(str(DB_VERSION))
f.close()
self.conn = sqlite3.connect(self.DB_FILE)
chmod(self.DB_FILE, 0600)
self.c = self.conn.cursor()
self._createTables()
self.conn.commit()
def run(self):
try:
self.init()
except Exception, e:
self.error = e
finally:
self.running.set()
while True:
j = self.jobs.get()
if j == "quit":
self.c.close()
self.conn.commit()
self.conn.close()
self.closing.set()
break
j.processJob()
def shutdown(self):
self.running.clear()
self.closing = Event()
self.jobs.put("quit")
self.closing.wait(1)
def _checkVersion(self):
""" get db version"""
if not exists(self.VERSION_FILE):
f = open(self.VERSION_FILE, "wb")
f.write(str(DB_VERSION))
f.close()
return
f = open(self.VERSION_FILE, "rb")
v = int(f.read().strip())
f.close()
return v
def _convertDB(self, v):
try:
return getattr(self, "_convertV%i" % v)()
except:
return False
#--convert scripts start
def _convertV6(self):
return False
#--convert scripts end
def _createTables(self):
"""create tables for database"""
self.c.execute(
'CREATE TABLE IF NOT EXISTS "packages" ('
'"pid" INTEGER PRIMARY KEY AUTOINCREMENT, '
'"name" TEXT NOT NULL, '
'"folder" TEXT DEFAULT "" NOT NULL, '
'"site" TEXT DEFAULT "" NOT NULL, '
'"comment" TEXT DEFAULT "" NOT NULL, '
'"password" TEXT DEFAULT "" NOT NULL, '
'"added" INTEGER DEFAULT 0 NOT NULL,' # set by trigger
'"status" INTEGER DEFAULT 0 NOT NULL,'
'"tags" TEXT DEFAULT "" NOT NULL,'
'"shared" INTEGER DEFAULT 0 NOT NULL,'
'"packageorder" INTEGER DEFAULT -1 NOT NULL,' #incremented by trigger
'"root" INTEGER DEFAULT -1 NOT NULL, '
'"owner" INTEGER NOT NULL, '
'FOREIGN KEY(owner) REFERENCES users(uid), '
'CHECK (root != pid)'
')'
)
self.c.execute(
'CREATE TRIGGER IF NOT EXISTS "insert_package" AFTER INSERT ON "packages"'
'BEGIN '
'UPDATE packages SET added = strftime("%s", "now"), '
'packageorder = (SELECT max(p.packageorder) + 1 FROM packages p WHERE p.root=new.root) '
'WHERE rowid = new.rowid;'
'END'
)
self.c.execute(
'CREATE TRIGGER IF NOT EXISTS "delete_package" AFTER DELETE ON "packages"'
'BEGIN '
'DELETE FROM files WHERE package = old.pid;'
'UPDATE packages SET packageorder=packageorder-1 WHERE packageorder > old.packageorder AND root=old.pid;'
'END'
)
self.c.execute('CREATE INDEX IF NOT EXISTS "package_index" ON packages(root, owner)')
self.c.execute('CREATE INDEX IF NOT EXISTS "package_owner" ON packages(owner)')
self.c.execute(
'CREATE TABLE IF NOT EXISTS "files" ('
'"fid" INTEGER PRIMARY KEY AUTOINCREMENT, '
'"name" TEXT NOT NULL, '
'"size" INTEGER DEFAULT 0 NOT NULL, '
'"status" INTEGER DEFAULT 0 NOT NULL, '
'"media" INTEGER DEFAULT 1 NOT NULL,'
'"added" INTEGER DEFAULT 0 NOT NULL,'
'"fileorder" INTEGER DEFAULT -1 NOT NULL, '
'"url" TEXT DEFAULT "" NOT NULL, '
'"plugin" TEXT DEFAULT "" NOT NULL, '
'"hash" TEXT DEFAULT "" NOT NULL, '
'"dlstatus" INTEGER DEFAULT 0 NOT NULL, '
'"error" TEXT DEFAULT "" NOT NULL, '
'"package" INTEGER NOT NULL, '
'"owner" INTEGER NOT NULL, '
'FOREIGN KEY(owner) REFERENCES users(uid), '
'FOREIGN KEY(package) REFERENCES packages(id)'
')'
)
self.c.execute('CREATE INDEX IF NOT EXISTS "file_index" ON files(package, owner)')
self.c.execute('CREATE INDEX IF NOT EXISTS "file_owner" ON files(owner)')
self.c.execute(
'CREATE TRIGGER IF NOT EXISTS "insert_file" AFTER INSERT ON "files"'
'BEGIN '
'UPDATE files SET added = strftime("%s", "now"), '
'fileorder = (SELECT max(f.fileorder) + 1 FROM files f WHERE f.package=new.package) '
'WHERE rowid = new.rowid;'
'END'
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "collector" ('
'"owner" INTEGER NOT NULL, '
'"data" TEXT NOT NULL, '
'FOREIGN KEY(owner) REFERENCES users(uid), '
'PRIMARY KEY(owner) ON CONFLICT REPLACE'
') '
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "storage" ('
'"identifier" TEXT NOT NULL, '
'"key" TEXT NOT NULL, '
'"value" TEXT DEFAULT "", '
'PRIMARY KEY (identifier, key) ON CONFLICT REPLACE'
')'
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "users" ('
'"uid" INTEGER PRIMARY KEY AUTOINCREMENT, '
'"name" TEXT NOT NULL UNIQUE, '
'"email" TEXT DEFAULT "" NOT NULL, '
'"password" TEXT NOT NULL, '
'"role" INTEGER DEFAULT 0 NOT NULL, '
'"permission" INTEGER DEFAULT 0 NOT NULL, '
'"folder" TEXT DEFAULT "" NOT NULL, '
'"traffic" INTEGER DEFAULT -1 NOT NULL, '
'"dllimit" INTEGER DEFAULT -1 NOT NULL, '
'"dlquota" TEXT DEFAULT "" NOT NULL, '
'"hddquota" INTEGER DEFAULT -1 NOT NULL, '
'"template" TEXT DEFAULT "default" NOT NULL, '
'"user" INTEGER DEFAULT -1 NOT NULL, ' # set by trigger to self
'FOREIGN KEY(user) REFERENCES users(uid)'
')'
)
self.c.execute('CREATE INDEX IF NOT EXISTS "username_index" ON users(name)')
self.c.execute(
'CREATE TRIGGER IF NOT EXISTS "insert_user" AFTER INSERT ON "users"'
'BEGIN '
'UPDATE users SET user = new.uid, folder=new.name '
'WHERE rowid = new.rowid;'
'END'
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "settings" ('
'"plugin" TEXT NOT NULL, '
'"user" INTEGER DEFAULT -1 NOT NULL, '
'"config" TEXT NOT NULL, '
'FOREIGN KEY(user) REFERENCES users(uid), '
'PRIMARY KEY (plugin, user) ON CONFLICT REPLACE'
')'
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "accounts" ('
'"plugin" TEXT NOT NULL, '
'"loginname" TEXT NOT NULL, '
'"owner" INTEGER NOT NULL DEFAULT -1, '
'"activated" INTEGER NOT NULL DEFAULT 1, '
'"password" TEXT DEFAULT "", '
'"shared" INTEGER NOT NULL DEFAULT 0, '
'"options" TEXT DEFAULT "", '
'FOREIGN KEY(owner) REFERENCES users(uid), '
'PRIMARY KEY (plugin, loginname, owner) ON CONFLICT REPLACE'
')'
)
self.c.execute(
'CREATE TABLE IF NOT EXISTS "stats" ('
'"user" INTEGER NOT NULL, '
'"plugin" TEXT NOT NULL, '
'"time" INTEGER NOT NULL, '
'"premium" INTEGER DEFAULT 0 NOT NULL, '
'"amount" INTEGER DEFAULT 0 NOT NULL, '
'FOREIGN KEY(user) REFERENCES users(uid), '
'PRIMARY KEY(user, plugin, time)'
')'
)
self.c.execute('CREATE INDEX IF NOT EXISTS "stats_time" ON stats(time)')
#try to lower ids
self.c.execute('SELECT max(fid) FROM files')
fid = self.c.fetchone()[0]
fid = int(fid) if fid else 0
self.c.execute('UPDATE SQLITE_SEQUENCE SET seq=? WHERE name=?', (fid, "files"))
self.c.execute('SELECT max(pid) FROM packages')
pid = self.c.fetchone()[0]
pid = int(pid) if pid else 0
self.c.execute('UPDATE SQLITE_SEQUENCE SET seq=? WHERE name=?', (pid, "packages"))
self.c.execute('VACUUM')
def createCursor(self):
return self.conn.cursor()
@async
def commit(self):
self.conn.commit()
@queue
def syncSave(self):
self.conn.commit()
@async
def rollback(self):
self.conn.rollback()
def async(self, f, *args, **kwargs):
args = (self, ) + args
job = DatabaseJob(f, *args, **kwargs)
self.jobs.put(job)
def queue(self, f, *args, **kwargs):
# Raise previous error of initialization
if self.error: raise self.error
args = (self, ) + args
job = DatabaseJob(f, *args, **kwargs)
self.jobs.put(job)
# only wait when db is running
if self.running.isSet(): job.wait()
return job.result
@classmethod
def registerSub(cls, klass):
cls.subs.append(klass)
@classmethod
def unregisterSub(cls, klass):
cls.subs.remove(klass)
def __getattr__(self, attr):
for sub in DatabaseBackend.subs:
if hasattr(sub, attr):
return getattr(sub, attr)
raise AttributeError(attr)
if __name__ == "__main__":
db = DatabaseBackend()
db.setup()
class Test():
@queue
def insert(db):
c = db.createCursor()
for i in range(1000):
c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar"))
@async
def insert2(db):
c = db.createCursor()
for i in range(1000 * 1000):
c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar"))
@queue
def select(db):
c = db.createCursor()
for i in range(10):
res = c.execute("SELECT value FROM storage WHERE identifier=? AND key=?", ("foo", i))
print res.fetchone()
@queue
def error(db):
c = db.createCursor()
print "a"
c.execute("SELECT myerror FROM storage WHERE identifier=? AND key=?", ("foo", i))
print "e"
db.registerSub(Test)
from time import time
start = time()
for i in range(100):
db.insert()
end = time()
print end - start
start = time()
db.insert2()
end = time()
print end - start
db.error()