diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-09-07 23:40:50 +0200 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-09-14 10:58:42 +0200 |
commit | 887ad58e4c6c20b992311bbdf931bcd18e73d384 (patch) | |
tree | f31beb241bacca0bfea4c1acc4e9ace813755cef /module/plugins/hooks/UpdateManager.py | |
parent | [AccountManager] Fixed #733 (diff) | |
parent | [File4safe] distributing LINK_PATTERN (diff) | |
download | pyload-887ad58e4c6c20b992311bbdf931bcd18e73d384.tar.xz |
Merge branch 'stable' into 0.4.10
Conflicts:
module/plugins/Account.py
module/plugins/AccountManager.py
module/plugins/Hook.py
module/plugins/OCR.py
module/plugins/Plugin.py
module/plugins/PluginManager.py
module/plugins/ReCaptcha.py
module/plugins/accounts/Ftp.py
module/plugins/accounts/Http.py
module/plugins/internal/MultiHoster.py
module/plugins/ocr/GigasizeCom.py
module/plugins/ocr/LinksaveIn.py
module/plugins/ocr/NetloadIn.py
module/plugins/ocr/ShareonlineBiz.py
Diffstat (limited to 'module/plugins/hooks/UpdateManager.py')
-rw-r--r-- | module/plugins/hooks/UpdateManager.py | 225 |
1 files changed, 136 insertions, 89 deletions
diff --git a/module/plugins/hooks/UpdateManager.py b/module/plugins/hooks/UpdateManager.py index 9f8ccdb80..546e6e6e8 100644 --- a/module/plugins/hooks/UpdateManager.py +++ b/module/plugins/hooks/UpdateManager.py @@ -1,67 +1,81 @@ # -*- coding: utf-8 -*- -import sys import re +import sys -from os import remove, stat -from os.path import join, isfile -from time import time +from operator import itemgetter +from os import path, remove, stat -from module.ConfigParser import IGNORE from module.network.RequestFactory import getURL -from module.plugins.Hook import threaded, Expose, Hook +from module.plugins.Hook import Expose, Hook, threaded +from module.utils import save_join class UpdateManager(Hook): __name__ = "UpdateManager" - __version__ = "0.24" - __description__ = """Check for updates""" + __type__ = "hook" + __version__ = "0.35" + __config__ = [("activated", "bool", "Activated", True), ("mode", "pyLoad + plugins;plugins only", "Check updates for", "pyLoad + plugins"), ("interval", "int", "Check interval in hours", 8), ("reloadplugins", "bool", "Monitor plugins for code changes (debug mode only)", True), ("nodebugupdate", "bool", "Don't check for updates in debug mode", True)] - __author_name__ = ("RaNaN", "stickell", "Walter Purcaro") - __author_mail__ = ("ranan@pyload.org", "l.stickell@yahoo.it", "vuolter@gmail.com") - SERVER_URL = "http://updatemanager.pyload.org" - MIN_TIME = 3 * 60 * 60 #: 3h minimum check interval + __description__ = """ Check for updates """ + __author_name__ = "Walter Purcaro" + __author_mail__ = "vuolter@gmail.com" + event_list = ["pluginConfigChanged"] + SERVER_URL = "http://updatemanager.pyload.org" + MIN_INTERVAL = 3 * 60 * 60 #: 3h minimum check interval (value is in seconds) + def pluginConfigChanged(self, plugin, name, value): - if name == "interval" and 0 < value != self.interval: - self.interval = max(value * 60 * 60, self.MIN_TIME) - self.initPeriodical() + if name == "interval": + interval = value * 60 * 60 + if self.MIN_INTERVAL <= interval != self.interval: + self.core.scheduler.removeJob(self.cb) + self.interval = interval + self.initPeriodical() + else: + self.logDebug("Invalid interval value, kept current") elif name == "reloadplugins": if self.cb2: self.core.scheduler.removeJob(self.cb2) - if value and self.core.debug: + if value is True and self.core.debug: self.periodical2() def coreReady(self): - self.pluginConfigChanged(self.__name__, "reloadplugins", self.getConfig("reloadplugins")) + self.pluginConfigChanged(self.__name__, "interval", self.getConfig("interval")) + x = lambda: self.pluginConfigChanged(self.__name__, "reloadplugins", self.getConfig("reloadplugins")) + self.core.scheduler.addJob(10, x, threaded=False) + + def unload(self): + self.pluginConfigChanged(self.__name__, "reloadplugins", False) def setup(self): self.cb2 = None - self.interval = self.MIN_TIME + self.interval = self.MIN_INTERVAL self.updating = False - self.info = {"pyload": False, "version": None, "plugins": False} + self.info = {'pyload': False, 'version': None, 'plugins': False} self.mtimes = {} #: store modification time for each plugin - def periodical2(self): if not self.updating: self.autoreloadPlugins() - self.cb2 = self.core.scheduler.addJob(10, self.periodical2, threaded=True) + self.cb2 = self.core.scheduler.addJob(4, self.periodical2, threaded=False) @Expose def autoreloadPlugins(self): """ reload and reindex all modified plugins """ modules = filter( - lambda m: m and (m.__name__.startswith("module.plugins.") or m.__name__.startswith( - "userplugins.")) and m.__name__.count(".") >= 2, sys.modules.itervalues()) + lambda m: m and (m.__name__.startswith("module.plugins.") or + m.__name__.startswith("userplugins.")) and + m.__name__.count(".") >= 2, sys.modules.itervalues() + ) reloads = [] @@ -70,7 +84,7 @@ class UpdateManager(Hook): id = (type, name) if type in self.core.pluginManager.plugins: f = m.__file__.replace(".pyc", ".py") - if not isfile(f): + if not path.isfile(f): continue mtime = stat(f).st_mtime @@ -83,18 +97,24 @@ class UpdateManager(Hook): return True if self.core.pluginManager.reloadPlugins(reloads) else False - @threaded def periodical(self): - if not self.info["pyload"] and not (self.getConfig("nodebugupdate") and self.core.debug): - self.updating = True - self.update(onlyplugin=True if self.getConfig("mode") == "plugins only" else False) - self.updating = False + if not self.info['pyload'] and not (self.getConfig("nodebugupdate") and self.core.debug): + self.updateThread() - def server_response(self): + def server_request(self): try: return getURL(self.SERVER_URL, get={'v': self.core.api.getServerVersion()}).splitlines() except: - self.logWarning(_("Not able to connect server to get updates")) + self.logWarning(_("Unable to contact server to get updates")) + + @threaded + def updateThread(self): + self.updating = True + status = self.update(onlyplugin=self.getConfig("mode") == "plugins only") + if status == 2: + self.core.api.restart() + else: + self.updating = False @Expose def updatePlugins(self): @@ -104,53 +124,54 @@ class UpdateManager(Hook): @Expose def update(self, onlyplugin=False): """ check for updates """ - data = self.server_response() + data = self.server_request() if not data: - r = False + exitcode = 0 elif data[0] == "None": - self.logInfo(_("No pyLoad version available")) + self.logInfo(_("No new pyLoad version available")) updates = data[1:] - r = self._updatePlugins(updates) + exitcode = self._updatePlugins(updates) elif onlyplugin: - r = False + exitcode = 0 else: newversion = data[0] self.logInfo(_("*** New pyLoad Version %s available ***") % newversion) self.logInfo(_("*** Get it here: https://github.com/pyload/pyload/releases ***")) - r = self.info["pyload"] = True - self.info["version"] = newversion - return r + exitcode = 3 + self.info['pyload'] = True + self.info['version'] = newversion + return exitcode #: 0 = No plugins updated; 1 = Plugins updated; 2 = Plugins updated, but restart required; 3 = No plugins updated, new pyLoad version available def _updatePlugins(self, updates): """ check for plugin updates """ - if self.info["plugins"]: + if self.info['plugins']: return False #: plugins were already updated updated = [] vre = re.compile(r'__version__.*=.*("|\')([0-9.]+)') url = updates[0] - schema = updates[1].split("|") - if 'BLACKLIST' in updates: + schema = updates[1].split('|') + if "BLACKLIST" in updates: blacklist = updates[updates.index('BLACKLIST') + 1:] updates = updates[2:updates.index('BLACKLIST')] else: blacklist = None updates = updates[2:] - for plugin in updates: - info = dict(zip(schema, plugin.split("|"))) - filename = info["name"] - prefix = info["type"] - version = info["version"] + upgradable = sorted(map(lambda x: dict(zip(schema, x.split('|'))), updates), key=itemgetter("type", "name")) + for plugin in upgradable: + filename = plugin['name'] + prefix = plugin['type'] + version = plugin['version'] if filename.endswith(".pyc"): name = filename[:filename.find("_")] else: name = filename.replace(".py", "") - #TODO: obsolete in 0.5.0 + #@TODO: obsolete after 0.4.10 if prefix.endswith("s"): type = prefix[:-1] else: @@ -158,77 +179,103 @@ class UpdateManager(Hook): plugins = getattr(self.core.pluginManager, "%sPlugins" % type) - if name not in plugins or name in IGNORE or (type, name) in IGNORE: - continue - - oldver = float(plugins[name]["v"]) + oldver = float(plugins[name]['v']) if name in plugins else None newver = float(version) - if oldver >= newver: - continue + if not oldver: + msg = "New [%(type)s] %(name)s (v%(newver)s)" + elif newver > oldver: + msg = "New version of [%(type)s] %(name)s (v%(oldver)s -> v%(newver)s)" else: - self.logInfo(_("New version of [%(type)s] %(name)s (v%(oldver)s -> v%(newver)s)") % { - "type": type, - "name": name, - "oldver": oldver, - "newver": newver - }) + continue + + self.logInfo(_(msg) % { + 'type': type, + 'name': name, + 'oldver': oldver, + 'newver': newver, + }) try: - content = getURL(url % info) + content = getURL(url % plugin) + m = vre.search(content) + if m and m.group(2) == version: + f = open(save_join("userplugins", prefix, filename), "wb") + f.write(content) + f.close() + updated.append((prefix, name)) + else: + raise Exception, _("Version mismatch") except Exception, e: - self.logError(_("Error when updating plugin %s") % filename, str(e)) - continue + self.logError(_("Error updating plugin %s") % filename, str(e)) - m = vre.search(content) - if not m or m.group(2) != version: - self.logError(_("Error when updating plugin %s") % name, _("Version mismatch")) - continue + if blacklist: + blacklisted = sorted(map(lambda x: (x.split('|')[0], x.split('|')[1].rsplit('.', 1)[0]), blacklist)) - f = open(join("userplugins", prefix, filename), "wb") - f.write(content) - f.close() - updated.append((prefix, name)) + # Always protect UpdateManager from self-removing + try: + blacklisted.remove(("hook", "UpdateManager")) + except: + pass - if blacklist: - removed = self.removePlugins(map(lambda x: x.split('|'), blacklist)) + removed = self.removePlugins(blacklisted) for t, n in removed: - self.logInfo(_("Removed blacklisted plugin: [%(type)s] %(name)s") % { - "type": t, - "name": n + self.logInfo(_("Removed blacklisted plugin [%(type)s] %(name)s") % { + 'type': t, + 'name': n, }) if updated: reloaded = self.core.pluginManager.reloadPlugins(updated) if reloaded: self.logInfo(_("Plugins updated and reloaded")) + exitcode = 1 else: - self.logInfo(_("*** Plugins have been updated, pyLoad will be restarted now ***")) - self.info["plugins"] = True - self.core.scheduler.addJob(4, self.core.api.restart(), threaded=False) #: risky, but pyload doesn't let more - return True + self.logInfo(_("*** Plugins have been updated, but need a pyLoad restart to be reloaded ***")) + self.info['plugins'] = True + exitcode = 2 else: self.logInfo(_("No plugin updates available")) - return False + exitcode = 0 + + return exitcode #: 0 = No plugins updated; 1 = Plugins updated; 2 = Plugins updated, but restart required @Expose def removePlugins(self, type_plugins): - """ delete plugins under userplugins directory""" + """ delete plugins from disk """ + if not type_plugins: - return None + return self.logDebug("Request deletion of plugins: %s" % type_plugins) removed = [] for type, name in type_plugins: - py = join("userplugins", type, name) - pyc = join("userplugins", type, name.replace('.py', '.pyc')) - if isfile(py): + err = False + file = name + ".py" + + for root in ("userplugins", path.join(pypath, "module", "plugins")): + + filename = save_join(root, type, file) + try: + remove(filename) + except Exception, e: + self.logDebug("Error deleting \"%s\"" % path.basename(filename), str(e)) + err = True + + filename += "c" + if path.isfile(filename): + try: + if type == "hook": + self.manager.deactivateHook(name) + remove(filename) + except Exception, e: + self.logDebug("Error deleting \"%s\"" % path.basename(filename), str(e)) + err = True + + if not err: id = (type, name) - remove(py) removed.append(id) - if isfile(pyc): - remove(pyc) return removed #: return a list of the plugins successfully removed |