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.

135 lines
4.3 KiB

from couchpotato.api import addApiView
from couchpotato.core.event import addEvent, fireEvent, fireEventAsync
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 os
14 years ago
import time
import traceback
14 years ago
log = CPLog(__name__)
class Updater(Plugin):
repo_name = 'RuudBurger/CouchPotatoServer'
14 years ago
version = None
update_failed = False
update_version = None
last_check = 0
14 years ago
def __init__(self):
self.repo = LocalRepository(Env.get('app_dir'), command = self.conf('git_command', default = 'git'))
14 years ago
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 = {
'hash': output.hash[:8],
'date': output.getDate(),
}
14 years ago
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
log.info('Checking for new version on github for %s' % self.repo_name)
if not Env.setting('development'):
self.repo.fetch()
14 years ago
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]))
14 years ago
if local.getDate() < remote.getDate():
if self.conf('automatic') and not self.update_failed:
14 years ago
if self.doUpdate():
fireEventAsync('app.crappy_restart')
14 years ago
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()
14 years ago
def doUpdateView(self):
return jsonified({
'success': self.doUpdate()
})
14 years ago
def doUpdate(self):
try:
log.debug('Stashing local changes')
self.repo.saveStash()
log.info('Updating to latest version')
14 years ago
self.repo.pull()
# Delete leftover .pyc files
self.deletePyc()
14 years ago
return True
14 years ago
except:
log.error('Failed updating via GIT: %s' % traceback.format_exc())
14 years ago
self.update_failed = True
14 years ago
return False
def deletePyc(self):
for root, dirs, files in os.walk(Env.get('app_dir')):
14 years ago
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)
14 years ago
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()))
14 years ago
def isEnabled(self):
return super(Updater, self).isEnabled() and Env.get('uses_git')