You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

from couchpotato.api import addApiView
14 years ago
from couchpotato.core.event import addEvent, fireEvent
from couchpotato.core.helpers.request import jsonified
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from couchpotato.environment import Env
from git.repository import LocalRepository
import time
log = CPLog(__name__)
class Updater(Plugin):
repo_name = 'RuudBurger/CouchPotatoServer'
14 years ago
running = False
version = None
update_failed = False
update_version = None
last_check = 0
14 years ago
def __init__(self):
self.repo = LocalRepository(Env.get('app_dir'))
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(),
})
14 years ago
def getVersion(self):
if not self.version:
try:
output = self.repo.getHead() # Yes, please
log.debug('Git version output: %s' % output.hash)
self.version = output.hash
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():
14 years ago
return
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()
14 years ago
if local.getDate() < remote.getDate():
if self.conf('automatic') and not self.update_failed:
14 years ago
self.doUpdate()
else:
self.update_version = remote.hash
self.last_check = time.time()
14 years ago
def doUpdateView(self):
return jsonified({
'success': self.doUpdate()
})
14 years ago
def doUpdate(self):
try:
log.info('Updating to latest version');
self.repo.pull()
return True
except Exception, e:
log.error('Failed updating via GIT: %s' % e)
self.update_failed = True
14 years ago
return False
def isEnabled(self):
return Plugin.isEnabled(self) and Env.get('uses_git')