Git Updater
This commit is contained in:
@@ -23,6 +23,8 @@ def cmd_couchpotato(base_path, args):
|
||||
dest = 'quiet', help = "Don't log to console")
|
||||
parser.add_argument('-d', '--daemon', action = 'store_true',
|
||||
dest = 'daemonize', help = 'Daemonize the app')
|
||||
parser.add_argument('-g', '--nogit', action = 'store_true',
|
||||
dest = 'git', help = 'Running from git')
|
||||
|
||||
options = parser.parse_args(args)
|
||||
|
||||
@@ -46,6 +48,7 @@ def cmd_couchpotato(base_path, args):
|
||||
# Register environment settings
|
||||
from couchpotato.environment import Env
|
||||
Env.get('settings').setFile(os.path.join(options.data_dir, 'settings.conf'))
|
||||
Env.set('uses_git', not options.git)
|
||||
Env.set('app_dir', base_path)
|
||||
Env.set('data_dir', options.data_dir)
|
||||
Env.set('log_path', os.path.join(log_dir, 'CouchPotato.log'))
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
from .main import Updater
|
||||
|
||||
def start():
|
||||
return Updater()
|
||||
|
||||
config = [{
|
||||
'name': 'updater',
|
||||
'groups': [
|
||||
{
|
||||
'tab': 'general',
|
||||
'name': 'updater',
|
||||
'label': 'Updater',
|
||||
'git_only': True,
|
||||
'options': [
|
||||
{
|
||||
'name': 'enabled',
|
||||
'default': True,
|
||||
'type': 'enabler',
|
||||
'description': 'Enable periodic update checking',
|
||||
},
|
||||
{
|
||||
'name': 'automatic',
|
||||
'default': False,
|
||||
'type': 'enabler',
|
||||
'description': 'Automaticly update when update is available',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}]
|
||||
@@ -0,0 +1,79 @@
|
||||
from couchpotato.core.event import addEvent, fireEvent
|
||||
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):
|
||||
|
||||
git = 'git://github.com/CouchPotato/CouchPotato.git'
|
||||
|
||||
running = False
|
||||
version = None
|
||||
updateFailed = False
|
||||
updateAvailable = False
|
||||
updateVersion = None
|
||||
lastCheck = 0
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.repo = LocalRepository(Env.get('app_dir'))
|
||||
|
||||
fireEvent('schedule.interval', 'updater.check', self.check, hours = 6)
|
||||
|
||||
addEvent('app.load', self.check)
|
||||
|
||||
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.updateAvailable or self.isDisabled():
|
||||
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()
|
||||
|
||||
if local.getDate() < remote.getDate():
|
||||
if self.conf('automatic') and not self.updateFailed:
|
||||
self.doUpdate()
|
||||
else:
|
||||
self.updateAvailable = True
|
||||
self.updateVersion = remote.hash
|
||||
|
||||
self.lastCheck = time.time()
|
||||
|
||||
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.updateFailed = True
|
||||
|
||||
return False
|
||||
|
||||
def isEnabled(self):
|
||||
return Plugin.isEnabled(self) and Env.get('uses_git')
|
||||
|
||||
@@ -4,6 +4,7 @@ from couchpotato.core.settings import Settings
|
||||
class Env:
|
||||
|
||||
''' Environment variables '''
|
||||
_uses_git = False
|
||||
_debug = False
|
||||
_settings = Settings()
|
||||
_loader = Loader()
|
||||
|
||||
Reference in New Issue
Block a user