summaryrefslogtreecommitdiffstats
path: root/module/plugins/hooks/UpdateManager.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/plugins/hooks/UpdateManager.py')
-rw-r--r--module/plugins/hooks/UpdateManager.py395
1 files changed, 252 insertions, 143 deletions
diff --git a/module/plugins/hooks/UpdateManager.py b/module/plugins/hooks/UpdateManager.py
index 5f88ea6d0..c72699228 100644
--- a/module/plugins/hooks/UpdateManager.py
+++ b/module/plugins/hooks/UpdateManager.py
@@ -1,121 +1,223 @@
# -*- coding: utf-8 -*-
-import sys
+from __future__ import with_statement
+
import re
-from os import remove, stat
-from os.path import join, isfile
-from time import time
+import sys
+
+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.16"
- __description__ = """Checks for updates"""
- __config__ = [("activated", "bool", "Activated", True),
- ("interval", "int", "Check interval in minutes", 480),
- ("debug", "bool", "Check for plugin changes when in debug mode", False)]
- __author_name__ = ("RaNaN", "stickell")
- __author_mail__ = ("ranan@pyload.org", "l.stickell@yahoo.it")
-
- URL = "http://updatemanager.pyload.org"
- MIN_TIME = 3 * 60 * 60 # 3h minimum check interval
-
- @property
- def debug(self):
- return self.core.debug and self.getConfig("debug")
+ __name__ = "UpdateManager"
+ __type__ = "hook"
+ __version__ = "0.42"
+
+ __config__ = [("activated" , "bool" , "Activated" , True ),
+ ("mode" , "pyLoad + plugins;plugins only", "Check updates for" , "pyLoad + plugins"),
+ ("interval" , "int" , "Check interval in hours" , 8 ),
+ ("autorestart" , "bool" , "Automatically restart pyLoad when required" , True ),
+ ("reloadplugins", "bool" , "Monitor plugins for code changes in debug mode", True ),
+ ("nodebugupdate", "bool" , "Don't check for updates in debug mode" , True )]
+
+ __description__ = """ Check for updates """
+ __license__ = "GPLv3"
+ __authors__ = [("Walter Purcaro", "vuolter@gmail.com")]
+
+
+ # event_list = ["pluginConfigChanged"]
+
+ SERVER_URL = "http://updatemanager.pyload.org"
+ VERSION = re.compile(r'__version__.*=.*("|\')([\d.]+)')
+ MIN_INTERVAL = 3 * 60 * 60 #: 3h minimum check interval (value is in seconds)
+
+
+ def pluginConfigChanged(self, plugin, name, value):
+ 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 is True and self.core.debug:
+ self.periodical2()
+
+
+ def coreReady(self):
+ 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):
- 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 = max(self.getConfig("interval") * 60, self.MIN_TIME)
+ self.cb2 = None
+ self.interval = self.MIN_INTERVAL
+ self.updating = False
+ self.info = {'pyload': False, 'version': None, 'plugins': False}
+ self.mtimes = {} #: store modification time for each plugin
- self.updated = False
- self.reloaded = True
- self.version = "None"
- self.info = {"pyload": False, "plugins": False}
+ def periodical2(self):
+ if not self.updating:
+ self.autoreloadPlugins()
+
+ self.cb2 = self.core.scheduler.addJob(4, self.periodical2, threaded=False)
- @threaded
- def periodical(self):
- updates = self.checkForUpdate()
- if updates:
- self.checkPlugins(updates)
-
- if self.updated and not self.reloaded:
- self.info["plugins"] = True
- self.logInfo(_("*** Plugins have been updated, please restart pyLoad ***"))
- elif self.updated and self.reloaded:
- self.logInfo(_("Plugins updated and reloaded"))
- self.updated = False
- elif self.version == "None":
- self.logInfo(_("No plugin updates available"))
@Expose
- def recheckForUpdates(self):
- """recheck if updates are available"""
- self.periodical()
+ 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()
+ )
+
+ reloads = []
- def checkForUpdate(self):
- """checks if an update is available, return result"""
+ for m in modules:
+ root, type, name = m.__name__.rsplit(".", 2)
+ id = (type, name)
+ if type in self.core.pluginManager.plugins:
+ f = m.__file__.replace(".pyc", ".py")
+ if not path.isfile(f):
+ continue
+
+ mtime = stat(f).st_mtime
+
+ if id not in self.mtimes:
+ self.mtimes[id] = mtime
+ elif self.mtimes[id] < mtime:
+ reloads.append(id)
+ self.mtimes[id] = mtime
+
+ return True if self.core.pluginManager.reloadPlugins(reloads) else False
+
+
+ def periodical(self):
+ if not self.info['pyload'] and not (self.getConfig("nodebugupdate") and self.core.debug):
+ self.updateThread()
+
+
+ def server_request(self):
try:
- if self.version == "None": # No updated known
- version_check = getURL(self.URL, get={'v': self.core.api.getServerVersion()}).splitlines()
- self.version = version_check[0]
-
- # Still no updates, plugins will be checked
- if self.version == "None":
- self.logInfo(_("No Updates for pyLoad"))
- return version_check[1:]
-
- self.info["pyload"] = True
- self.logInfo(_("*** New pyLoad Version %s available ***") % self.version)
- self.logInfo(_("*** Get it here: http://pyload.org/download ***"))
+ return getURL(self.SERVER_URL, get={'v': self.core.api.getServerVersion()}).splitlines()
except:
- self.logWarning(_("Not able to connect server for updates"))
+ self.logWarning(_("Unable to contact server to get updates"))
- return None # Nothing will be done
- def checkPlugins(self, updates):
- """ checks for plugins updates"""
+ @threaded
+ def updateThread(self):
+ self.updating = True
- # plugins were already updated
- if self.info["plugins"]:
- return
+ status = self.update(onlyplugin=self.getConfig("mode") == "plugins only")
+
+ if status is 2 and self.getConfig("autorestart"):
+ self.core.api.restart()
+ else:
+ self.updating = False
- reloads = []
- vre = re.compile(r'__version__.*=.*("|\')([0-9.]+)')
- url = updates[0]
- schema = updates[1].split("|")
- if 'BLACKLIST' in updates:
+ @Expose
+ def updatePlugins(self):
+ """ simple wrapper for calling plugin update quickly """
+ return self.update(onlyplugin=True)
+
+
+ @Expose
+ def update(self, onlyplugin=False):
+ """ check for updates """
+ data = self.server_request()
+
+ if not data:
+ exitcode = 0
+
+ elif data[0] == "None":
+ self.logInfo(_("No new pyLoad version available"))
+ updates = data[1:]
+ exitcode = self._updatePlugins(updates)
+
+ elif onlyplugin:
+ 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 ***"))
+ 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']:
+ return False #: plugins were already updated
+
+ exitcode = 0
+ updated = []
+
+ url = updates[0]
+ schema = updates[1].split('|')
+
+ if "BLACKLIST" in updates:
blacklist = updates[updates.index('BLACKLIST') + 1:]
- updates = updates[2:updates.index('BLACKLIST')]
+ updates = updates[2:updates.index('BLACKLIST')]
else:
blacklist = None
- updates = updates[2:]
+ updates = updates[2:]
+
+ upgradable = [dict(zip(schema, x.split('|'))) for x in updates]
+ blacklisted = [(x.split('|')[0], x.split('|')[1].rsplit('.', 1)[0]) for x in blacklist] if blacklist else []
+
+ if blacklist:
+ # Protect UpdateManager from self-removing
+ try:
+ blacklisted.remove(("hook", "UpdateManager"))
+ except:
+ pass
+
+ for t, n in blacklisted:
+ for idx, plugin in enumerate(upgradable):
+ if n == plugin['name'] and t == plugin['type']:
+ upgradable.pop(idx)
+ break
+
+ for t, n in self.removePlugins(sorted(blacklisted)):
+ self.logInfo(_("Removed blacklisted plugin [%(type)s] %(name)s") % {
+ 'type': t,
+ 'name': n,
+ })
- for plugin in updates:
- info = dict(zip(schema, plugin.split("|")))
- filename = info["name"]
- prefix = info["type"]
- version = info["version"]
+ for plugin in sorted(upgradable, key=itemgetter("type", "name")):
+ 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:
@@ -123,79 +225,86 @@ class UpdateManager(Hook):
plugins = getattr(self.core.pluginManager, "%sPlugins" % type)
- if name in plugins:
- if float(plugins[name]["v"]) >= float(version):
- continue
+ oldver = float(plugins[name]['v']) if name in plugins else None
+ newver = float(version)
- if name in IGNORE or (type, name) in IGNORE:
+ if not oldver:
+ msg = "New plugin: [%(type)s] %(name)s (v%(newver).2f)"
+ elif newver > oldver:
+ msg = "New version of plugin: [%(type)s] %(name)s (v%(oldver).2f -> v%(newver).2f)"
+ else:
continue
- self.logInfo(_("New version of %(type)s|%(name)s : %(version).2f") % {
- "type": type,
- "name": name,
- "version": float(version)
- })
-
+ self.logInfo(_(msg) % {'type' : type,
+ 'name' : name,
+ 'oldver': oldver,
+ 'newver': newver})
try:
- content = getURL(url % info)
+ content = getURL(url % plugin)
+ m = self.VERSION.search(content)
+
+ if m and m.group(2) == version:
+ with open(save_join("userplugins", prefix, filename), "wb") as f:
+ f.write(content)
+
+ updated.append((prefix, name))
+ else:
+ raise Exception, _("Version mismatch")
+
except Exception, e:
- self.logWarning(_("Error when updating %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.logWarning(_("Error when updating %s") % name, _("Version mismatch"))
- continue
+ 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, but need a pyLoad restart to be reloaded ***"))
+ self.info['plugins'] = True
+ exitcode = 2
+ else:
+ self.logInfo(_("No plugin updates available"))
- f = open(join("userplugins", prefix, filename), "wb")
- f.write(content)
- f.close()
- self.updated = True
+ return exitcode #: 0 = No plugins updated; 1 = Plugins updated; 2 = Plugins updated, but restart required
- reloads.append((prefix, name))
- if blacklist:
- self.executeBlacklist(blacklist)
+ @Expose
+ def removePlugins(self, type_plugins):
+ """ delete plugins from disk """
- self.reloaded = self.core.pluginManager.reloadPlugins(reloads)
+ if not type_plugins:
+ return
- def executeBlacklist(self, blacklist):
- for b in blacklist:
- type, name = b.split('|')
- if isfile(join("userplugins", type, name)):
- self.logInfo(_("Removing blacklisted plugin %(type)s|%(name)s") % {
- "type": type,
- "name": name
- })
- remove(join("userplugins", type, name))
- if isfile(join("userplugins", type, name.replace('.py', '.pyc'))):
- remove(join("userplugins", type, name.replace('.py', '.pyc')))
+ self.logDebug("Requested deletion of plugins: %s" % type_plugins)
- def checkChanges(self):
- if self.last_check + max(self.getConfig("interval") * 60, self.MIN_TIME) < time():
- self.old_periodical()
- self.last_check = time()
+ removed = []
- modules = filter(
- lambda m: m and (m.__name__.startswith("module.plugins.") or m.__name__.startswith(
- "userplugins.")) and m.__name__.count(".") >= 2, sys.modules.itervalues())
+ for type, name in type_plugins:
+ err = False
+ file = name + ".py"
- reloads = []
+ for root in ("userplugins", path.join(pypath, "module", "plugins")):
- for m in modules:
- root, type, name = m.__name__.rsplit(".", 2)
- id = (type, name)
- if type in self.core.pluginManager.plugins:
- f = m.__file__.replace(".pyc", ".py")
- if not isfile(f):
- continue
+ filename = save_join(root, type, file)
+ try:
+ remove(filename)
+ except Exception, e:
+ self.logDebug("Error removing: %s" % path.basename(filename), str(e))
+ err = True
- mtime = stat(f).st_mtime
+ filename += "c"
+ if path.isfile(filename):
+ try:
+ if type == "hook":
+ self.manager.deactivateHook(name)
+ remove(filename)
+ except Exception, e:
+ self.logDebug("Error removing: %s" % path.basename(filename), str(e))
+ err = True
- if id not in self.mtimes:
- self.mtimes[id] = mtime
- elif self.mtimes[id] < mtime:
- reloads.append(id)
- self.mtimes[id] = mtime
+ if not err:
+ id = (type, name)
+ removed.append(id)
- self.core.pluginManager.reloadPlugins(reloads)
+ return removed #: return a list of the plugins successfully removed