@ -11,14 +11,9 @@ import json
from get_args import args
from config import settings
bazarr_version = ' 7.2.0 '
LATEST_VERSION = None
INSTALL_TYPE = None
UPDATE_AVAILABLE = None
COMMITS_BEHIND = None
LATEST_RELEASE = None
PREV_RELEASE = bazarr_version
# from main import bazarr_version
class FakeLock ( object ) :
@ -63,7 +58,20 @@ def check_releases():
json . dump ( releases , f )
def runGit ( args ) :
class Updater ( object ) :
def __init__ ( self ) :
self . bazarr_version = ' 7.2.0 '
self . LATEST_VERSION = ' '
self . INSTALL_TYPE = ' '
self . UPDATE_AVAILABLE = ' '
self . COMMITS_BEHIND = ' '
self . LATEST_RELEASE = ' '
self . CURRENT_VERSION = ' '
self . PREV_RELEASE = self . bazarr_version
self . CURRENT_VERSION , self . GIT_REMOTE , self . GIT_BRANCH = self . getVersion ( )
def runGit ( self , args ) :
git_locations = [ ' git ' ]
if platform . system ( ) . lower ( ) == ' darwin ' :
@ -96,12 +104,11 @@ def runGit(args):
return ( output , err )
def getVersion ( self ) :
if os . path . isdir ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' .git ' ) ) and not args . release_update :
def getVersion ( ) :
if os . path . isdir ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' .git ' ) ) :
INSTALL_TYPE = ' git '
output , err = runGit ( ' rev-parse HEAD ' )
self . INSTALL_TYPE = ' git '
output , err = self . runGit ( ' rev-parse HEAD ' )
if not output :
logging . error ( ' Could not find latest installed version. ' )
@ -117,7 +124,7 @@ def getVersion():
branch_name = settings . general . branch
else :
remote_branch , err = runGit ( ' rev-parse --abbrev-ref --symbolic-full-name @ {u} ' )
remote_branch , err = self . runGit ( ' rev-parse --abbrev-ref --symbolic-full-name @ {u} ' )
remote_branch = remote_branch . rsplit ( ' / ' , 1 ) if remote_branch else [ ]
if len ( remote_branch ) == 2 :
remote_name , branch_name = remote_branch
@ -135,32 +142,28 @@ def getVersion():
return cur_commit_hash , ' origin ' , branch_name
else :
INSTALL_TYPE = ' source '
self . INSTALL_TYPE = ' source '
if CURRENT_VERSION :
return CURRENT_VERSION , ' origin ' , settings . general . branch
if self . CURRENT_VERSION :
return self . CURRENT_VERSION , ' origin ' , settings . general . branch
else :
return None , ' origin ' , settings . general . branch
CURRENT_VERSION , GIT_REMOTE , GIT_BRANCH = getVersion ( )
def check_updates ( ) :
check_github ( )
if not CURRENT_VERSION :
UPDATE_AVAILABLE = None
elif COMMITS_BEHIND > 0 and settings . general . branch in ( ' master ' ) and \
( ' V ' + bazarr_version ) != LATEST_RELEASE :
UPDATE_AVAILABLE = ' release '
elif COMMITS_BEHIND > 0 and CURRENT_VERSION != LATEST_VERSION :
UPDATE_AVAILABLE = ' commit '
def check_updates ( self ) :
self . check_github ( )
if not self . CURRENT_VERSION :
self . UPDATE_AVAILABLE = None
elif self . COMMITS_BEHIND > 0 and settings . general . branch in ( ' master ' ) and \
( ' v ' + self . bazarr_version ) != self . LATEST_RELEASE :
self . UPDATE_AVAILABLE = ' release '
elif self . COMMITS_BEHIND > 0 and self . CURRENT_VERSION != self . LATEST_VERSION :
self . UPDATE_AVAILABLE = ' commit '
else :
UPDATE_AVAILABLE = False
self . UPDATE_AVAILABLE = False
print self . UPDATE_AVAILABLE
def check_github ( ) :
COMMITS_BEHIND = 0
def check_github ( self ) :
self . COMMITS_BEHIND = 0
# Get the latest version available from github
logging . info ( ' Retrieving latest version information from GitHub ' )
@ -169,61 +172,60 @@ def check_github():
if version is None :
logging . warn ( ' Could not get the latest version from GitHub. Are you running a local development version? ' )
return CURRENT_VERSION
return self . CURRENT_VERSION
LATEST_VERSION = version [ ' sha ' ]
logging . debug ( " Latest version is %s " , LATEST_VERSION )
self . LATEST_VERSION = version [ ' sha ' ]
logging . debug ( " Latest version is %s " , self . LATEST_VERSION )
# See how many commits behind we are
if not CURRENT_VERSION :
if not self . CURRENT_VERSION :
logging . info ( ' You are running an unknown version of Bazarr. Run the updater to identify your version ' )
return LATEST_VERSION
return self . LATEST_VERSION
if LATEST_VERSION == CURRENT_VERSION :
if self . LATEST_VERSION == self . CURRENT_VERSION :
logging . info ( ' Bazarr is up to date ' )
return LATEST_VERSION
return self . LATEST_VERSION
logging . info ( ' Comparing currently installed version with latest GitHub version ' )
url = ' https://api.github.com/repos/morpheus65535/bazarr/compare/ %s ... %s ' % ( LATEST_VERSION ,
CURRENT_VERSION )
url = ' https://api.github.com/repos/morpheus65535/bazarr/compare/ %s ... %s ' % ( self . LATEST_VERSION ,
self . CURRENT_VERSION )
commits = request_json ( url , timeout = 20 , whitelist_status_code = 404 , validator = lambda x : type ( x ) == dict )
if commits is None :
logging . warn ( ' Could not get commits behind from GitHub. ' )
return LATEST_VERSION
return self . LATEST_VERSION
try :
COMMITS_BEHIND = int ( commits [ ' behind_by ' ] )
logging . debug ( " In total, %d commits behind " , COMMITS_BEHIND )
self . COMMITS_BEHIND = int ( commits [ ' behind_by ' ] )
logging . debug ( " In total, %d commits behind " , self . COMMITS_BEHIND )
except KeyError :
logging . info ( ' Cannot compare versions. Are you running a local development version? ' )
COMMITS_BEHIND = 0
self . COMMITS_BEHIND = 0
if COMMITS_BEHIND > 0 :
logging . info ( ' New version is available. You are %s commits behind ' % COMMITS_BEHIND )
if self . COMMITS_BEHIND > 0 :
logging . info ( ' New version is available. You are %s commits behind ' % self . COMMITS_BEHIND )
url = ' https://api.github.com/repos/morpheus65535/bazarr/releases '
releases = request_json ( url , timeout = 20 , whitelist_status_code = 404 , validator = lambda x : type ( x ) == list )
if releases is None :
logging . warn ( ' Could not get releases from GitHub. ' )
return LATEST_VERSION
return self . LATEST_VERSION
if settings . general . branch == ' master ' :
release = next ( ( r for r in releases if not r [ ' prerelease ' ] ) , releases [ 0 ] )
else :
release = releases [ 0 ]
LATEST_RELEASE = release [ ' tag_name ' ]
self . LATEST_RELEASE = release [ ' tag_name ' ]
elif COMMITS_BEHIND == 0 :
elif self . COMMITS_BEHIND == 0 :
logging . info ( ' Bazarr is up to date ' )
return LATEST_VERSION
return self . LATEST_VERSION
def update ( ) :
if INSTALL_TYPE == ' git ' :
output , err = runGit ( ' pull ' + ' origin ' + ' ' + settings . general . branch )
def update ( self ) :
if self . INSTALL_TYPE == ' git ' and not args . release_update :
output , err = self . runGit ( ' pull ' + ' origin ' + ' ' + settings . general . branch )
if not output :
logging . error ( ' Unable to download latest version ' )
@ -284,11 +286,10 @@ def update():
os . remove ( new_path )
os . renames ( old_path , new_path )
def checkout_git_branch ( ) :
if INSTALL_TYPE == ' git ' :
output , err = runGit ( ' fetch origin ' )
output , err = runGit ( ' checkout %s ' % settings . general . branch )
def checkout_git_branch ( self ) :
if self . INSTALL_TYPE == ' git ' and not args . release_update :
output , err = self . runGit ( ' fetch origin ' )
output , err = self . runGit ( ' checkout %s ' % settings . general . branch )
if not output :
logging . error ( ' Unable to change git branch. ' )
@ -299,7 +300,7 @@ def checkout_git_branch():
logging . error ( ' Unable to checkout from git: ' + line )
logging . info ( ' Output: ' + str ( output ) )
output , err = runGit ( ' pull origin %s ' % settings . general . branch )
output , err = self . runGit ( ' pull origin %s ' % settings . general . branch )
def request_content ( url , * * kwargs ) :