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.

118 lines
3.4 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 os
14 years ago
import time
import traceback
14 years ago
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()
# Delete leftover .pyc files
self.deletePyc()
14 years ago
return True
except Exception, e:
log.error('Failed updating via GIT: %s' % e)
self.update_failed = True
14 years ago
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:", 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 Plugin.isEnabled(self) and Env.get('uses_git')