Move updater to core plugins

This commit is contained in:
Ruud
2012-01-26 23:51:56 +01:00
parent 4246009399
commit 1dfdf195bd
3 changed files with 0 additions and 0 deletions
@@ -1,41 +0,0 @@
from .main import Updater
def start():
return Updater()
config = [{
'name': 'updater',
'groups': [
{
'tab': 'general',
'name': 'updater',
'label': 'Updater',
'git_only': True,
'description': 'Enable periodic update checking',
'options': [
{
'name': 'enabled',
'default': True,
'type': 'enabler',
},
{
'name': 'notification',
'type': 'bool',
'default': True,
'description': 'Send a notification if an update is available.',
},
{
'name': 'automatic',
'default': True,
'type': 'bool',
'description': 'Automatically update when update is available',
},
{
'name': 'git_command',
'default': 'git',
'advanced': True
},
],
},
],
}]
-135
View File
@@ -1,135 +0,0 @@
from couchpotato.api import addApiView
from couchpotato.core.event import addEvent, fireEvent
from couchpotato.core.helpers.request import jsonified
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from couchpotato.environment import Env
from git.repository import LocalRepository
import os
import time
import traceback
log = CPLog(__name__)
class Updater(Plugin):
repo_name = 'RuudBurger/CouchPotatoServer'
running = False
version = None
update_failed = False
update_version = None
last_check = 0
def __init__(self):
self.repo = LocalRepository(Env.get('app_dir'), command = self.conf('git_command', default = 'git'))
fireEvent('schedule.interval', 'updater.check', self.check, hours = 6)
addEvent('app.load', self.check)
addApiView('updater.info', self.getInfo)
addApiView('updater.update', self.doUpdateView)
def getInfo(self):
return jsonified({
'repo_name': self.repo_name,
'last_check': self.last_check,
'update_version': self.update_version,
'version': self.getVersion()
})
def getVersion(self):
if not self.version:
try:
output = self.repo.getHead() # Yes, please
log.debug('Git version output: %s' % output.hash)
self.version = {
'hash': output.hash[:8],
'date': output.getDate(),
}
except Exception, e:
log.error('Failed using GIT updater, running from source, you need to have GIT installed. %s' % e)
return 'No GIT'
return self.version
def check(self):
if self.update_version or self.isDisabled():
return
log.info('Checking for new version on github for %s' % self.repo_name)
if not Env.setting('development'):
self.repo.fetch()
current_branch = self.repo.getCurrentBranch().name
for branch in self.repo.getRemoteByName('origin').getBranches():
if current_branch == branch.name:
local = self.repo.getHead()
remote = branch.getHead()
log.info('Versions, local:%s, remote:%s' % (local.hash[:8], remote.hash[:8]))
if local.getDate() < remote.getDate():
if self.conf('automatic') and not self.update_failed:
if self.doUpdate():
fireEvent('app.crappy_restart')
else:
self.update_version = {
'hash': remote.hash[:8],
'date': remote.getDate(),
}
if self.conf('notification'):
fireEvent('updater.available', message = 'A new update is available', data = self.getVersion())
self.last_check = time.time()
def doUpdateView(self):
return jsonified({
'success': self.doUpdate()
})
def doUpdate(self):
try:
log.debug('Stashing local changes')
self.repo.saveStash()
log.info('Updating to latest version')
self.repo.pull()
# Delete leftover .pyc files
self.deletePyc()
return True
except:
log.error('Failed updating via GIT: %s' % traceback.format_exc())
self.update_failed = True
return False
def deletePyc(self):
for root, dirs, files in os.walk(Env.get('app_dir')):
pyc_files = filter(lambda filename: filename.endswith('.pyc'), files)
py_files = set(filter(lambda filename: filename.endswith('.py'), files))
excess_pyc_files = filter(lambda pyc_filename: pyc_filename[:-1] not in py_files, pyc_files)
for excess_pyc_file in excess_pyc_files:
full_path = os.path.join(root, excess_pyc_file)
log.debug('Removing old PYC file: %s' % full_path)
try:
os.remove(full_path)
except:
log.error('Couldn\'t remove %s: %s' % (full_path, traceback.format_exc()))
def isEnabled(self):
return Plugin.isEnabled(self) and Env.get('uses_git')
@@ -1,102 +0,0 @@
var UpdaterBase = new Class({
Implements: [Events],
initialize: function(){
var self = this;
App.addEvent('load', self.info.bind(self, 1000))
App.addEvent('unload', function(){
if(self.timer)
clearTimeout(self.timer);
});
},
info: function(timeout){
var self = this;
if(self.timer) clearTimeout(self.timer);
self.timer = setTimeout(function(){
Api.request('updater.info', {
'onComplete': function(json){
self.json = json;
self.fireEvent('loaded', [json]);
if(json.update_version){
self.createMessage(json);
}
else {
if(self.message)
self.message.destroy();
}
}
})
}, (timeout || 0))
},
getInfo: function(){
return this.json;
},
createMessage: function(data){
var self = this;
self.message = new Element('div.message.update').adopt(
new Element('span', {
'text': 'A new version is available'
}),
new Element('a', {
'href': 'https://github.com/'+data.repo_name+'/compare/'+data.version.hash+'...'+data.update_version.hash,
'text': 'see what has changed',
'target': '_blank'
}),
new Element('span[text=or]'),
new Element('a', {
'text': 'just update, gogogo!',
'events': {
'click': self.doUpdate.bind(self)
}
})
).inject($(document.body).getElement('.header'))
},
doUpdate: function(){
var self = this;
Api.request('updater.update', {
'onComplete': function(json){
if(json.success){
App.restart();
$(document.body).set('spin', {
'message': 'Updating'
});
$(document.body).spin();
var checks = 0;
var interval = 0;
interval = setInterval(function(){
Api.request('', {
'onSuccess': function(){
if(checks > 2){
clearInterval(interval);
$(document.body).unspin();
self.info();
}
}
});
checks++;
}, 500)
}
}
});
}
});
var Updater = new UpdaterBase();