/
UpdateManager.py
388 lines (307 loc) · 13.5 KB
/
UpdateManager.py
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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import operator
import os
import re
import sys
import time
from ..internal.Addon import Addon
from ..internal.misc import Expose, encode, exists, fsjoin, threaded
class UpdateManager(Addon):
__name__ = "UpdateManager"
__type__ = "hook"
__version__ = "1.21"
__status__ = "testing"
__config__ = [("activated", "bool", "Activated", True),
("checkinterval", "int", "Check interval in hours", 6),
("autorestart", "bool", "Auto-restart pyLoad when required", True),
("checkonstart", "bool", "Check for updates on startup", True),
("checkperiod", "bool", "Check for updates periodically", True),
("reloadplugins", "bool", "Monitor plugin code changes in debug mode", True),
("nodebugupdate", "bool", "Don't update plugins in debug mode", False)]
__description__ = """Check for updates"""
__license__ = "GPLv3"
__authors__ = [("Walter Purcaro", "vuolter@gmail.com")]
_VERSION = re.compile(r'^\s*__version__\s*=\s*("|\')([\d.]+)\1', re.M)
# SERVER_URL = "http://updatemanager.pyload.org"
# SERVER_URL = "http://updatemanager-spyload.rhcloud.com"
SERVER_URL = "https://raw.githubusercontent.com/pyload/updates/master/plugins.txt"
CHECK_INTERVAL = 3 * 60 * 60 #: 3 hours
def activate(self):
if self.checkonstart:
self.pyload.api.pauseServer()
self.update()
if self.do_restart is False:
self.pyload.api.unpauseServer()
self.periodical.start(10)
def init(self):
self.info.update({
'pyload': False,
'plugins': False,
'last_check': time.time()})
self.mtimes = {} #: Store modification time for each plugin
self.event_map = {'allDownloadsProcessed': "all_downloads_processed"}
if self.config.get('checkonstart'):
self.pyload.api.pauseServer()
self.checkonstart = True
else:
self.checkonstart = False
self.do_restart = False
def all_downloads_processed(self):
if self.do_restart is True:
self.pyload.api.restart()
def periodical_task(self):
if self.pyload.debug:
if self.config.get('reloadplugins'):
self.autoreload_plugins()
if self.config.get('nodebugupdate'):
return
if self.config.get('checkperiod') and \
time.time() - max(self.CHECK_INTERVAL, self.config.get('checkinterval') * 60 * 60) > self.info['last_check']:
self.update()
if self.do_restart is True:
if self.pyload.threadManager.pause and not self.pyload.api.statusDownloads():
self.pyload.api.restart()
#: Deprecated method, use `autoreload_plugins` instead
@Expose
def autoreloadPlugins(self, *args, **kwargs):
"""
See `autoreload_plugins`
"""
return self.autoreload_plugins(*args, **kwargs)
@Expose
def autoreload_plugins(self):
"""
Reload and reindex all modified plugins
"""
reloads = []
modules = filter(
lambda m: m and (m.__name__.startswith("module.plugins.") or
m.__name__.startswith("userplugins.")) and
m.__name__.count(".") >= 2, sys.modules.values()
)
for m in modules:
root, plugin_type, plugin_name = m.__name__.rsplit(".", 2)
plugin_id = (plugin_type, plugin_name)
if plugin_type in self.pyload.pluginManager.plugins:
f = m.__file__.replace(".pyc", ".py")
if not os.path.isfile(f):
continue
mtime = os.path.getmtime(f)
if plugin_id not in self.mtimes:
self.mtimes[plugin_id] = mtime
elif self.mtimes[plugin_id] < mtime:
reloads.append(plugin_id)
self.mtimes[plugin_id] = mtime
return True if self.pyload.pluginManager.reloadPlugins(reloads) else False
def server_response(self, line=None):
try:
html = self.load(self.SERVER_URL,
get={'v': self.pyload.api.getServerVersion()})
except Exception:
self.log_warning(_("Unable to connect to the server to retrieve updates"))
else:
res = html.splitlines()
if line is not None:
try:
res = res[line]
except IndexError:
res = None
return res
@Expose
@threaded
def update(self):
"""
Check for updates
"""
if self._update() != 2 or not self.config.get('autorestart'):
return
if not self.pyload.api.statusDownloads():
self.pyload.api.restart()
else:
self.log_warning(_("pyLoad restart scheduled"),
_("Downloads are active, pyLoad restart postponed once the download is done"))
self.pyload.api.pauseServer()
self.do_restart = True
def _update(self):
newversion = self.server_response(0)
self.info['pyload'] = False
self.info['last_check'] = time.time()
if not newversion:
exitcode = 0
elif newversion == self.pyload.api.getServerVersion():
self.log_info(_("pyLoad is up to date!"))
exitcode = self.update_plugins()
elif re.search(r'^\d+(?:\.\d+){0,3}[a-z]?$', newversion):
self.log_info(_("*** New pyLoad %s available ***") % newversion)
self.log_info(_("*** Get it here: https://github.com/pyload/pyload/releases ***"))
self.info['pyload'] = True
exitcode = 3
else:
exitcode = 0
#: Exit codes:
#: -1 = No plugin updated, new pyLoad version available
#: 0 = No plugin updated
#: 1 = Plugins updated
#: 2 = Plugins updated, but restart required
return exitcode
@Expose
def update_plugins(self):
server_data = self.server_response()
if not server_data or server_data[0] != self.pyload.api.getServerVersion():
return 0
updated = self._update_plugins(server_data)
if updated:
self.log_info(_("*** Plugins updated ***"))
if self.pyload.pluginManager.reloadPlugins(updated):
exitcode = 1
else:
self.log_warning(_("You have to restart pyLoad to use the updated plugins"))
self.info['plugins'] = True
exitcode = 2
paused = self.pyload.threadManager.pause
self.pyload.api.pauseServer()
self.manager.dispatchEvent("plugin_updated", updated)
if not paused:
self.pyload.api.unpauseServer()
else:
self.log_info(_("All plugins are up to date!"))
exitcode = 0
#: Exit codes:
#: 0 = No plugin updated
#: 1 = Plugins updated
#: 2 = Plugins updated, but restart required
return exitcode
def parse_updates(self, server_data):
schema = server_data[2].split('|')
if "BLACKLIST" in server_data:
blacklist = server_data[server_data.index('BLACKLIST') + 1:]
updatelist = server_data[3:server_data.index('BLACKLIST')]
else:
blacklist = []
updatelist = server_data[3:]
for l in updatelist, blacklist:
nl = []
for line in l:
d = dict(zip(schema, line.split('|')))
d['name'] = d['name'].rsplit('.py', 1)[0]
nl.append(d)
l[:] = nl
updatelist = sorted(updatelist,
key=operator.itemgetter("type", "name"))
blacklist = sorted(blacklist, key=operator.itemgetter("type", "name"))
return updatelist, blacklist
def _update_plugins(self, server_data):
"""
Check for plugin updates
"""
updated = []
updatelist, blacklist = self.parse_updates(server_data)
url = server_data[1]
req = self.pyload.requestFactory.getRequest(self.classname)
if blacklist:
#@NOTE: Protect UpdateManager from self-removing
if os.name == "nt":
#@NOTE: Windows filesystem is case insensitive, make sure we do not delete legitimate plugins
whitelisted_plugins = [
(plugin['type'], plugin['name'].upper()) for plugin in updatelist]
blacklisted_plugins = [(plugin['type'], plugin['name']) for plugin in blacklist
if not (plugin['name'] == self.classname and plugin['type'] == self.__type__)
and (plugin['type'], plugin['name'].upper()) not in whitelisted_plugins]
else:
blacklisted_plugins = [(plugin['type'], plugin['name']) for plugin in blacklist
if not (plugin['name'] == self.classname and plugin['type'] == self.__type__)]
c = 1
l = len(blacklisted_plugins)
for idx, plugin in enumerate(updatelist):
if c > l:
break
plugin_name = plugin['name']
plugin_type = plugin['type']
for t, n in blacklisted_plugins:
if n != plugin_name or t != plugin_type:
continue
updatelist.pop(idx)
c += 1
break
for t, n in self.remove_plugins(blacklisted_plugins):
self.log_info(_("Removed blacklisted plugin: %(type)s %(name)s") %
{'type': t.upper(),
'name': n,})
for plugin in updatelist:
plugin_name = plugin['name']
plugin_type = plugin['type']
plugin_version = plugin['version']
plugins = getattr(self.pyload.pluginManager,
"%sPlugins" % plugin_type.rstrip('s')) # @TODO: Remove rstrip in 0.4.10
oldver = float(plugins[plugin_name]['v']) if plugin_name in plugins else None
try:
newver = float(plugin_version)
except ValueError:
self.log_error(_("Error updating plugin: %s %s") % (plugin_type.rstrip('s').upper(), plugin_name),
_("Bad version number on the server"))
continue
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.log_info(_(msg) % {'type': plugin_type.rstrip('s').upper(), # @TODO: Remove rstrip in 0.4.10
'name': plugin_name,
'oldver': oldver,
'newver': newver})
try:
content = self.load(url % plugin + ".py", decode=False, req=req)
if req.code == 404:
raise Exception(_("URL not found"))
m = self._VERSION.search(content)
if m and m.group(2) == plugin_version:
with open(fsjoin("userplugins", plugin_type, plugin_name + ".py"), "wb") as f:
f.write(encode(content))
updated.append((plugin_type, plugin_name))
else:
raise Exception(_("Version mismatch"))
except Exception, e:
self.log_error(_("Error updating plugin: %s %s") %
(plugin_type.rstrip('s').upper(), plugin_name),
e) # @TODO: Remove rstrip in 0.4.10
return updated
#: Deprecated method, use `remove_plugins` instead
@Expose
def removePlugins(self, *args, **kwargs):
"""
See `remove_plugins`
"""
return self.remove_plugins(*args, **kwargs)
@Expose
def remove_plugins(self, plugin_ids):
"""
Delete plugins from disk
"""
if not plugin_ids:
return
removed = set()
self.log_debug("Requested deletion of plugins: %s" % plugin_ids)
for plugin_type, plugin_name in plugin_ids:
rootplugins = os.path.join(pypath, "module", "plugins")
for basedir in ("userplugins", rootplugins):
py_filename = fsjoin(basedir, plugin_type, plugin_name + ".py")
pyc_filename = py_filename + "c"
if plugin_type == "hook":
try:
self.manager.deactivateHook(plugin_name)
except Exception, e:
self.log_debug(e, trace=True)
for filename in (py_filename, pyc_filename):
if not exists(filename):
continue
try:
os.remove(filename)
except OSError, e:
self.log_warning(_("Error removing `%s`") % filename, e)
else:
plugin_id = (plugin_type, plugin_name)
removed.add(plugin_id)
#: Return a list of the plugins successfully removed
return list(removed)