diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-11-20 14:33:11 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-11-20 14:33:11 +0100 |
commit | 4525215e384a1446404ef0c3909fd824720d2b2e (patch) | |
tree | 2ddf91d6e05863ffb1975fb6e93bf1c88e4a1e1a /module | |
parent | cz hoster - update search patterns (diff) | |
download | pyload-4525215e384a1446404ef0c3909fd824720d2b2e.tar.xz |
automatic plugin reloader
Diffstat (limited to 'module')
-rw-r--r-- | module/Utils.py | 4 | ||||
-rw-r--r-- | module/common/ImportDebugger.py | 1 | ||||
-rw-r--r-- | module/plugins/AccountManager.py | 9 | ||||
-rw-r--r-- | module/plugins/PluginManager.py | 59 | ||||
-rw-r--r-- | module/plugins/hooks/UpdateManager.py | 58 |
5 files changed, 102 insertions, 29 deletions
diff --git a/module/Utils.py b/module/Utils.py index 36713b778..3919b5ff0 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -82,9 +82,9 @@ def compare_time(start, end): if start == end: return True now = list(time.localtime()[3:5]) - if start < now and end > now: return True + if start < now < end: return True elif start > end and (now > start or now < end): return True - elif start < now and end < now and start > end: return True + elif start < now > end < start: return True else: return False diff --git a/module/common/ImportDebugger.py b/module/common/ImportDebugger.py index fdc6d90cf..a997f7b0c 100644 --- a/module/common/ImportDebugger.py +++ b/module/common/ImportDebugger.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- - import sys class ImportDebugger(object): diff --git a/module/plugins/AccountManager.py b/module/plugins/AccountManager.py index 7e30f4817..fc521d36c 100644 --- a/module/plugins/AccountManager.py +++ b/module/plugins/AccountManager.py @@ -35,18 +35,19 @@ class AccountManager(): """Constructor""" self.core = core + self.lock = Lock() + + self.initPlugins() + self.saveAccounts() # save to add categories to conf + def initPlugins(self): self.accounts = {} # key = ( plugin ) self.plugins = {} - self.lock = Lock() self.initAccountPlugins() - self.loadAccounts() - self.saveAccounts() # save to add categories to conf - #---------------------------------------------------------------------- def getAccountPlugin(self, plugin): """get account instance for plugin or None if anonymous""" if plugin in self.accounts: diff --git a/module/plugins/PluginManager.py b/module/plugins/PluginManager.py index 4617e9c7a..cf01ba5d8 100644 --- a/module/plugins/PluginManager.py +++ b/module/plugins/PluginManager.py @@ -46,16 +46,9 @@ class PluginManager: #self.config = self.core.config self.log = core.log + self.plugins = {} self.createIndex() - self.plugins = {"crypter": self.crypterPlugins, - "container": self.containerPlugins, - "hoster": self.hosterPlugins, - "captcha": self.captchaPlugins, - "accounts": self.accountPlugins, - "hooks": self.hookPlugins, - "internal": self.internalPlugins} - #register for import hook sys.meta_path.append(self) @@ -71,14 +64,14 @@ class PluginManager: f = open(join("userplugins", "__init__.py"), "wb") f.close() - self.crypterPlugins = self.parse("crypter", pattern=True) - self.containerPlugins = self.parse("container", pattern=True) - self.hosterPlugins = self.parse("hoster", pattern=True) + self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) - self.captchaPlugins = self.parse("captcha") - self.accountPlugins = self.parse("accounts") - self.hookPlugins = self.parse("hooks") - self.internalPlugins = self.parse("internal") + self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") + self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") + self.plugins["hooks"] = self.hookPlugins = self.parse("hooks") + self.plugins["internal"] = self.internalPlugins = self.parse("internal") self.log.debug("created index of plugins") @@ -329,9 +322,41 @@ class PluginManager: return sys.modules[name] - def reloadPlugins(self): + def reloadPlugins(self, type_plugins): """ reloads and reindexes plugins """ - pass + if not type_plugins: return False + + self.log.debug("Reload plugins: %s" % type_plugins) + + as_dict = {} + for t,n in type_plugins: + if t in as_dict: + as_dict[t].append(n) + else: + as_dict[t] = [n] + + # we do not reload hooks or internals, would cause to much side effects + if "hooks" in as_dict or "internal" in as_dict: + return False + + for type in as_dict.iterkeys(): + for plugin in as_dict[type]: + if plugin in self.plugins[type]: + if "module" in self.plugins[type][plugin]: + self.log.debug("Reloading %s" % plugin) + reload(self.plugins[type][plugin]["module"]) + + #index creation + self.plugins["crypter"] = self.crypterPlugins = self.parse("crypter", pattern=True) + self.plugins["container"] = self.containerPlugins = self.parse("container", pattern=True) + self.plugins["hoster"] = self.hosterPlugins = self.parse("hoster", pattern=True) + self.plugins["captcha"] = self.captchaPlugins = self.parse("captcha") + self.plugins["accounts"] = self.accountPlugins = self.parse("accounts") + + if "accounts" in as_dict: #accounts needs to be reloaded + self.core.accountManager.initPlugins() + self.core.scheduler.addJob(0, self.core.accountManager.getAccountInfos) + if __name__ == "__main__": diff --git a/module/plugins/hooks/UpdateManager.py b/module/plugins/hooks/UpdateManager.py index 2a85e505f..8212ddb65 100644 --- a/module/plugins/hooks/UpdateManager.py +++ b/module/plugins/hooks/UpdateManager.py @@ -17,8 +17,12 @@ @author: RaNaN @interface-version: 0.1 """ + +import sys import re +from os import stat from os.path import join +from time import time from module.network.RequestFactory import getURL from module.plugins.Hook import threaded, Expose, Hook @@ -28,12 +32,27 @@ class UpdateManager(Hook): __version__ = "0.1" __description__ = """checks for updates""" __config__ = [("activated", "bool", "Activated", "True"), - ("interval", "int", "Check interval in minutes", "360")] + ("interval", "int", "Check interval in minutes", "360"), + ("debug", "bool", "Check for plugin changes when in debug mode", False)] __author_name__ = ("RaNaN") __author_mail__ = ("ranan@pyload.org") + @property + def debug(self): + return self.core.debug and self.getConfig("debug") + + def setup(self): - self.interval = self.getConfig("interval") * 60 + if self.debug: + self.logDebug("Monitoring file changes") + self.interval = 4 + self.last_check = 0 #timestamp of updatecheck + self.old_periodical = self.periodical + self.periodical = self.checkChanges + self.mtimes = {} #recordes times + else: + self.interval = self.getConfig("interval") * 60 + self.updated = False self.reloaded = True @@ -47,12 +66,13 @@ class UpdateManager(Hook): else: self.log.info(_("No Updates for pyLoad")) self.checkPlugins() - + if self.updated and not self.reloaded: self.info["plugins"] = True self.log.info(_("*** Plugins have been updated, please restart pyLoad ***")) elif self.updated and self.reloaded: self.log.info(_("Plugins updated and reloaded")) + self.updated = False else: self.log.info(_("No plugin updates available")) @@ -90,6 +110,7 @@ class UpdateManager(Hook): return False updates = updates.splitlines() + reloads = [] vre = re.compile(r'__version__.*=.*("|\')([0-9.]+)') @@ -135,5 +156,32 @@ class UpdateManager(Hook): f.close() self.updated = True - self.reloaded = False - self.core.pluginManager.reloadPlugins() + reloads.append((type, name)) + + self.reloaded = self.core.pluginManager.reloadPlugins(reloads) + + def checkChanges(self): + + if self.last_check + self.getConfig("interval") * 60 < time(): + self.old_periodical() + self.last_check = time() + + modules = filter( + lambda m: m and (m.__name__.startswith("module.plugins.") or m.__name__.startswith("userplugins.")), + sys.modules.itervalues()) + + reloads = [] + + for m in modules: + root, type, name = m.__name__.rsplit(".", 2) + id = (type, name) + if type in self.core.pluginManager.plugins: + mtime = stat(m.__file__.replace(".pyc", ".py")).st_mtime + + if id not in self.mtimes: + self.mtimes[id] = mtime + elif self.mtimes[id] < mtime: + reloads.append(id) + self.mtimes[id] = mtime + + self.core.pluginManager.reloadPlugins(reloads)
\ No newline at end of file |