diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2014-01-11 22:23:20 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2014-01-11 22:23:20 +0100 |
commit | 94c1cceae1e58c0b7cff09bef684751978d878e3 (patch) | |
tree | 9e2f9eebce7602431aaa24cbd5f8871e48e4ff3f | |
parent | fixed error from last commit (diff) | |
download | pyload-94c1cceae1e58c0b7cff09bef684751978d878e3.tar.xz |
small cleanup, fixed some test cases
-rw-r--r-- | pyload/Core.py | 17 | ||||
-rw-r--r-- | pyload/DownloadManager.py | 28 | ||||
-rw-r--r-- | pyload/FileManager.py | 28 | ||||
-rw-r--r-- | pyload/InitHomeDir.py | 47 | ||||
-rw-r--r-- | pyload/datatypes/PyPackage.py | 2 | ||||
-rw-r--r-- | pyload/setup/Setup.py | 3 | ||||
-rw-r--r-- | pyload/utils/pylgettext.py | 61 | ||||
-rw-r--r-- | tests/helper/Stubs.py | 107 | ||||
-rw-r--r-- | tests/manager/test_accountManager.py | 9 | ||||
-rw-r--r-- | tests/manager/test_downloadManager.py | 42 | ||||
-rw-r--r-- | tests/manager/test_filemanager.py | 32 |
11 files changed, 160 insertions, 216 deletions
diff --git a/pyload/Core.py b/pyload/Core.py index d18c80cad..6f3893481 100644 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -30,6 +30,7 @@ import os from os import _exit, execl, getcwd, remove, walk, chdir, close import signal import sys +import gettext from sys import argv, executable, exit from time import time, sleep from traceback import print_exc @@ -41,7 +42,8 @@ import subprocess subprocess.__doc__ = None # the module with the largest doc we are using -import InitHomeDir +from InitHomeDir import init_dir +init_dir() from AccountManager import AccountManager from config.ConfigParser import ConfigParser @@ -54,7 +56,6 @@ from Scheduler import Scheduler from remote.RemoteManager import RemoteManager from utils.JsEngine import JsEngine -import utils.pylgettext as gettext from utils import formatSize, get_console_encoding from utils.fs import free_space, exists, makedirs, join, chmod @@ -73,19 +74,11 @@ sys.stdout = getwriter(enc)(sys.stdout, errors="replace") # - configurable auth system ldap/mysql # - cron job like sheduler # - plugin stack / multi decrypter -# - media plugin type -# - general progress info # - content attribute for files / sync status # - sync with disk content / file manager / nested packages # - sync between pyload cores # - new attributes (date|sync status) -# - embedded packages -# - would require new/modified link collector concept -# - pausable links/packages -# - toggable accounts -# - interaction manager # - improve external scripts -# - make pyload undestructable to fail plugins -> see ConfigParser first class Core(object): """pyLoad Core, one tool to rule them all... (the filehosters) :D""" @@ -309,7 +302,6 @@ class Core(object): self.config = ConfigParser() - gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None]) translation = gettext.translation("pyLoad", self.path("locale"), languages=[self.config['general']['language'], "en"], fallback=True) translation.install(True) @@ -383,6 +375,7 @@ class Core(object): from AddonManager import AddonManager from interaction.InteractionManager import InteractionManager from threads.ThreadManager import ThreadManager + from DownloadManager import DownloadManager Api.initComponents() self.api = Api(self) @@ -395,6 +388,7 @@ class Core(object): self.interactionManager = self.im = InteractionManager(self) self.accountManager = AccountManager(self) self.threadManager = ThreadManager(self) + self.downloadManager = DownloadManager(self) self.addonManager = AddonManager(self) self.remoteManager = RemoteManager(self) @@ -481,6 +475,7 @@ class Core(object): _exit(0) # TODO check exits codes, clean exit is still blocked + self.downloadManager.work() self.threadManager.work() self.interactionManager.work() self.scheduler.work() diff --git a/pyload/DownloadManager.py b/pyload/DownloadManager.py new file mode 100644 index 000000000..b877db355 --- /dev/null +++ b/pyload/DownloadManager.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################### +# Copyright(c) 2008-2014 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 +############################################################################### + + +class DownloadManager: + """ Schedules and manages download and decrypter jobs. """ + + def __init__(self, core): + self.core = core + + def work(self): + """ Does the periodical work """ + diff --git a/pyload/FileManager.py b/pyload/FileManager.py index 4ea7dc5cc..2edf81bfc 100644 --- a/pyload/FileManager.py +++ b/pyload/FileManager.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- ############################################################################### -# Copyright(c) 2008-2012 pyLoad Team +# Copyright(c) 2008-2014 pyLoad Team # http://www.pyload.org # # This file is part of pyLoad. @@ -35,14 +35,12 @@ def invalidate(func): return new -# TODO: needs to be replaced later -OWNER = 0 - class FileManager: """Handles all request made to obtain information, modify status or other request for links or packages""" ROOT_PACKAGE = -1 + ROOT_OWNER = -1 def __init__(self, core): """Constructor""" @@ -94,17 +92,17 @@ class FileManager: pass @invalidate - def addLinks(self, data, package): + def addLinks(self, data, pid, owner): """Add links, data = (plugin, url) tuple. Internal method should use API.""" - self.db.addLinks(data, package, OWNER) - self.evm.dispatchEvent("package:updated", package) + self.db.addLinks(data, pid, owner) + self.evm.dispatchEvent("package:updated", pid) @invalidate - def addPackage(self, name, folder, root, password, site, comment, paused): + def addPackage(self, name, folder, root, password, site, comment, paused, owner): """Adds a package to database""" pid = self.db.addPackage(name, folder, root, password, site, comment, - PackageStatus.Paused if paused else PackageStatus.Ok, OWNER) + PackageStatus.Paused if paused else PackageStatus.Ok, owner) p = self.db.getPackageInfo(pid) self.evm.dispatchEvent("package:inserted", pid, p.root, p.packageorder) @@ -115,7 +113,7 @@ class FileManager: def getPackage(self, pid): """return package instance""" if pid == self.ROOT_PACKAGE: - return RootPackage(self, OWNER) + return RootPackage(self, self.ROOT_OWNER) elif pid in self.packages: pack = self.packages[pid] pack.timestamp = time() @@ -133,7 +131,7 @@ class FileManager: def getPackageInfo(self, pid): """returns dict with package information""" if pid == self.ROOT_PACKAGE: - pack = RootPackage(self, OWNER).toInfoData() + pack = RootPackage(self, self.ROOT_OWNER).toInfoData() elif pid in self.packages: pack = self.packages[pid].toInfoData() pack.stats = self.db.getStatsForPackage(pid) @@ -177,7 +175,7 @@ class FileManager: @read_lock def getTree(self, pid, full, state, search=None): """ return a TreeCollection and fill the info data of containing packages. - optional filter only unfnished files + optional filter only unfinished files """ view = TreeCollection(pid) @@ -202,7 +200,7 @@ class FileManager: # root package is not in database, create an instance if pid == self.ROOT_PACKAGE: - view.root = RootPackage(self, OWNER).toInfoData() + view.root = RootPackage(self, self.ROOT_OWNER).toInfoData() packs[self.ROOT_PACKAGE] = view.root elif pid in packs: view.root = packs[pid] @@ -533,8 +531,6 @@ class FileManager: # cantor won't be happy if we put the package in itself if pid == root or p.root == root: return False - # TODO move real folders - # we assume pack is not in use anyway, so we can release it self.releasePackage(pid) self.db.movePackage(p.root, p.packageorder, pid, root) @@ -552,8 +548,6 @@ class FileManager: if not self.getPackageInfo(pid): raise PackageDoesNotExist(pid) - # TODO move real files - self.db.moveFiles(f.package, fids, pid) return True diff --git a/pyload/InitHomeDir.py b/pyload/InitHomeDir.py index 4c7fce2eb..a68e1a197 100644 --- a/pyload/InitHomeDir.py +++ b/pyload/InitHomeDir.py @@ -1,23 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -""" - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see <http://www.gnu.org/licenses/>. - - @author: RaNaN - - This modules inits working directories and global variables, pydir and homedir -""" +############################################################################### +# Copyright(c) 2008-2014 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 +############################################################################### from os import makedirs, path, chdir from os.path import join @@ -87,10 +83,17 @@ if not configdir: configname = ".pyload" if platform in ("posix", "linux2", "darwin") else "pyload" configdir = path.join(homedir, configname + dev) -if not path.exists(configdir): - makedirs(configdir, 0700) +def init_dir(other_path=None): + # switch to pyload home directory, or path at other_path + global configdir + + if other_path is not None: + configdir = join(pypath, other_path) + + if not path.exists(configdir): + makedirs(configdir, 0700) -__builtin__.configdir = configdir -chdir(configdir) + __builtin__.configdir = configdir + chdir(configdir) #print "Using %s as working directory." % configdir diff --git a/pyload/datatypes/PyPackage.py b/pyload/datatypes/PyPackage.py index 955986942..41a733fe5 100644 --- a/pyload/datatypes/PyPackage.py +++ b/pyload/datatypes/PyPackage.py @@ -90,7 +90,7 @@ class PyPackage: self.m.releasePackage(self.id) def delete(self): - self.m.deletePackage(self.id) + self.m.removePackage(self.id) def deleteIfEmpty(self): """ True if deleted """ diff --git a/pyload/setup/Setup.py b/pyload/setup/Setup.py index c61a389e2..aa65edb4b 100644 --- a/pyload/setup/Setup.py +++ b/pyload/setup/Setup.py @@ -19,13 +19,14 @@ import os import sys import socket +import gettext 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 diff --git a/pyload/utils/pylgettext.py b/pyload/utils/pylgettext.py deleted file mode 100644 index fb36fecee..000000000 --- a/pyload/utils/pylgettext.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from gettext import * - -_searchdirs = None - -origfind = find - -def setpaths(pathlist): - global _searchdirs - if isinstance(pathlist, list): - _searchdirs = pathlist - else: - _searchdirs = list(pathlist) - - -def addpath(path): - global _searchdirs - if _searchdirs is None: - _searchdirs = list(path) - else: - if path not in _searchdirs: - _searchdirs.append(path) - - -def delpath(path): - global _searchdirs - if _searchdirs is not None: - if path in _searchdirs: - _searchdirs.remove(path) - - -def clearpath(): - global _searchdirs - if _searchdirs is not None: - _searchdirs = None - - -def find(domain, localedir=None, languages=None, all=False): - if _searchdirs is None: - return origfind(domain, localedir, languages, all) - searches = [localedir] + _searchdirs - results = list() - for dir in searches: - res = origfind(domain, dir, languages, all) - if all is False: - results.append(res) - else: - results.extend(res) - if all is False: - results = filter(lambda x: x is not None, results) - if len(results) == 0: - return None - else: - return results[0] - else: - return results - -#Is there a smarter/cleaner pythonic way for this? -translation.func_globals['find'] = find diff --git a/tests/helper/Stubs.py b/tests/helper/Stubs.py index 81b7d8a09..3541e4ffb 100644 --- a/tests/helper/Stubs.py +++ b/tests/helper/Stubs.py @@ -1,33 +1,32 @@ # -*- coding: utf-8 -*- -import sys -from os.path import abspath, dirname, join +from os.path import join from time import strftime from traceback import format_exc -from collections import defaultdict - -sys.path.append(abspath(join(dirname(__file__), "..", "..", "pyload", "lib"))) -sys.path.append(abspath(join(dirname(__file__), "..", ".."))) import __builtin__ from pyload.Api import Role +from pyload.Core import Core +from pyload.InitHomeDir import init_dir from pyload.datatypes.User import User -from pyload.datatypes.PyPackage import PyPackage from pyload.threads.BaseThread import BaseThread from pyload.config.ConfigParser import ConfigParser -from pyload.network.RequestFactory import RequestFactory -from pyload.PluginManager import PluginManager -from pyload.utils.JsEngine import JsEngine -from logging import log, DEBUG, INFO, WARN, ERROR +init_dir(join("tests", "config")) +from logging import log, DEBUG, INFO, WARN, ERROR # Do nothing def noop(*args, **kwargs): pass +class NoopClass: + def __getattr__(self, item): + return noop + + ConfigParser.save = noop @@ -45,80 +44,31 @@ class LogStub: log(WARN, *args) -class NoLog: - def debug(self, *args): - pass - - def info(self, *args): - pass - - def error(self, *args): - log(ERROR, *args) - - def warning(self, *args): - log(WARN, *args) - - -class Core: +class TestCore(Core): def __init__(self): - self.log = NoLog() - - self.api = self.core = self - self.threadManager = self - self.debug = True - self.captcha = True - self.config = ConfigParser() - self.pluginManager = PluginManager(self) - self.requestFactory = RequestFactory(self) - __builtin__.pyreq = self.requestFactory - self.accountManager = AccountManager() - self.addonManager = AddonManager() - self.eventManager = self.evm = NoopClass() - self.interactionManager = self.im = NoopClass() - self.scheduler = NoopClass() - self.js = JsEngine() - self.cache = {} - self.packageCache = {} - - self.statusMsg = defaultdict(lambda: "statusmsg") + super(TestCore, self).__init__() + self.start(tests=True) + self.db.getUserData = self.getUserData self.log = LogStub() def getServerVersion(self): return "TEST_RUNNER on %s" % strftime("%d %h %Y") - def path(self, path): - return path - - def updateFile(self, *args): - pass + def init_logger(self, level): + # init with empty logger + self.log = NoopClass() - def updatePackage(self, *args): - pass - - def processingIds(self, *args): - return [] - - def getPackage(self, *args): - return PyPackage(self, 0, "tmp", "tmp", -1, 0, "", "", "", 0, "", 0, 0, 0) - - def print_exc(self): + def print_exc(self, force=False): log(ERROR, format_exc()) + def getUserData(self, uid): + if uid == 0: + return adminUser + elif uid == 1: + return normalUser -class NoopClass: - def __getattr__(self, item): - return noop - - -class AddonManager(NoopClass): - def activePlugins(self): - return [] - - -class AccountManager: - def getAccountForPlugin(self, name): - return None + return otherUser class Thread(BaseThread): @@ -136,10 +86,11 @@ class Thread(BaseThread): return dump +Core = TestCore + __builtin__._ = lambda x: x -__builtin__.pypath = abspath(join(dirname(__file__), "..", "..")) -__builtin__.addonManager = AddonManager() -__builtin__.pyreq = None adminUser = User(None, uid=0, role=Role.Admin) -normalUser = User(None, uid=1, role=Role.User)
\ No newline at end of file +normalUser = User(None, uid=1, role=Role.User) +otherUser = User(None, uid=2, role=Role.User) + diff --git a/tests/manager/test_accountManager.py b/tests/manager/test_accountManager.py index 1b328f892..b7166b2bd 100644 --- a/tests/manager/test_accountManager.py +++ b/tests/manager/test_accountManager.py @@ -4,17 +4,12 @@ from unittest import TestCase from tests.helper.Stubs import Core, adminUser, normalUser -from pyload.database import DatabaseBackend -from pyload.AccountManager import AccountManager - class TestAccountManager(TestCase): @classmethod def setUpClass(cls): cls.core = Core() - cls.db = DatabaseBackend(cls.core) - cls.core.db = cls.db - cls.db.setup() + cls.db = cls.core.db @classmethod def tearDownClass(cls): @@ -22,7 +17,7 @@ class TestAccountManager(TestCase): def setUp(self): self.db.purgeAccounts() - self.manager = AccountManager(self.core) + self.manager = self.core.accountManager def test_access(self): account = self.manager.updateAccount("Http", "User", "somepw", adminUser) diff --git a/tests/manager/test_downloadManager.py b/tests/manager/test_downloadManager.py new file mode 100644 index 000000000..9906ca1a0 --- /dev/null +++ b/tests/manager/test_downloadManager.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +from tests.helper.Stubs import Core, normalUser, adminUser +from tests.helper.BenchmarkTest import BenchmarkTest + +from pyload.database import DatabaseBackend +# disable asyncronous queries +DatabaseBackend.async = DatabaseBackend.queue + +class TestDownloadManager(BenchmarkTest): + + bench = ["add_links", "db"] + + @classmethod + def setUpClass(cls): + cls.c = Core() + cls.db = cls.c.db + cls.db.purgeAll() + + cls.files = cls.c.files + cls.m = cls.c.downloadManager + + @classmethod + def tearDownClass(cls): + cls.db.purgeAll() + cls.db.shutdown() + + def setUp(self): + self.test_add_links() + + def test_add_links(self): + # just generate some links and files + for i in range(10): + pid = self.files.addPackage("name %d", "folder", -1, "", "", "", False, normalUser.uid) + self.files.addLinks([("plugin%d" % i, "url%d" %i) for i in range(50)], pid, normalUser.uid) + + def test_db(self): + pass + + +if __name__ == "__main__": + TestDownloadManager.benchmark()
\ No newline at end of file diff --git a/tests/manager/test_filemanager.py b/tests/manager/test_filemanager.py index a7507cada..212ea6d8c 100644 --- a/tests/manager/test_filemanager.py +++ b/tests/manager/test_filemanager.py @@ -2,7 +2,7 @@ from random import choice -from tests.helper.Stubs import Core +from tests.helper.Stubs import Core, normalUser from tests.helper.BenchmarkTest import BenchmarkTest from pyload.database import DatabaseBackend @@ -10,8 +10,6 @@ from pyload.database import DatabaseBackend DatabaseBackend.async = DatabaseBackend.queue from pyload.Api import DownloadState -from pyload.FileManager import FileManager - class TestFileManager(BenchmarkTest): bench = ["add_packages", "add_files", "get_files_root", "get", @@ -24,19 +22,17 @@ class TestFileManager(BenchmarkTest): @classmethod def setUpClass(cls): c = Core() - # db needs seperate initialisation - cls.db = c.db = DatabaseBackend(c) - cls.db.setup() + # db needs separate initialisation + cls.db = c.db cls.db.purgeAll() - cls.m = cls.db.manager = FileManager(c) + cls.m = cls.db.manager = c.files @classmethod def tearDownClass(cls): cls.db.purgeAll() cls.db.shutdown() - # benchmarker ignore setup def setUp(self): self.db.purgeAll() @@ -48,7 +44,7 @@ class TestFileManager(BenchmarkTest): def test_add_packages(self): for i in range(100): - pid = self.m.addPackage("name", "folder", choice(self.pids), "", "", "", False) + pid = self.m.addPackage("name", "folder", choice(self.pids), "", "", "", False, normalUser.uid) self.pids.append(pid) if -1 in self.pids: @@ -56,7 +52,7 @@ class TestFileManager(BenchmarkTest): def test_add_files(self): for pid in self.pids: - self.m.addLinks([("plugin %d" % i, "url %s" % i) for i in range(self.count)], pid) + self.m.addLinks([("plugin %d" % i, "url %s" % i) for i in range(self.count)], pid, normalUser.uid) count = self.m.getQueueStats()[0] files = self.count * len(self.pids) @@ -120,14 +116,14 @@ class TestFileManager(BenchmarkTest): for fid in view.root.fids: assert fid in view.files def test_delete(self): - self.m.deleteFile(self.count * 5) - self.m.deletePackage(choice(self.pids)) + self.m.removeFile(self.count * 5) + self.m.removePackage(choice(self.pids)) def test_order_package(self): - parent = self.m.addPackage("order", "", -1, "", "", "", False) - self.m.addLinks([("url", "plugin") for i in range(100)], parent) + parent = self.m.addPackage("order", "", -1, "", "", "", False, normalUser.uid) + self.m.addLinks([("url", "plugin") for i in range(100)], parent, normalUser.uid) - pids = [self.m.addPackage("c", "", parent, "", "", "", False) for i in range(5)] + pids = [self.m.addPackage("c", "", parent, "", "", "", False, normalUser.uid) for i in range(5)] v = self.m.getTree(parent, False, None) self.assert_ordered(pids, 0, 5, v.root.pids, v.packages, True) @@ -143,8 +139,8 @@ class TestFileManager(BenchmarkTest): def test_order_files(self): - parent = self.m.addPackage("order", "", -1, "", "", "", False) - self.m.addLinks([("url", "plugin") for i in range(100)], parent) + parent = self.m.addPackage("order", "", -1, "", "", "", False, normalUser.uid) + self.m.addLinks([("url", "plugin") for i in range(100)], parent, normalUser.uid) v = self.m.getTree(parent, False, None) fids = v.root.fids[10:20] @@ -206,7 +202,7 @@ class TestFileManager(BenchmarkTest): self.m.movePackage(pid, -1) v = self.m.getTree(-1, False, False) - assert v.root.pids[-1] == pid + assert pid in v.root.pids assert sorted([p.packageorder for p in v.packages.values()]) == range(len(v.packages)) v = self.m.getTree(pid, False, False) |