@ -1,3 +1,5 @@
# coding=utf-8
import os
import os
import platform
import platform
import re
import re
@ -12,31 +14,6 @@ from get_args import args
from config import settings
from config import settings
# from main import bazarr_version
class FakeLock ( object ) :
"""
If no locking or request throttling is needed , use this
"""
def __enter__ ( self ) :
"""
Do nothing on enter
"""
pass
def __exit__ ( self , type , value , traceback ) :
"""
Do nothing on exit
"""
pass
fake_lock = FakeLock ( )
def check_releases ( ) :
def check_releases ( ) :
releases = [ ]
releases = [ ]
url_releases = ' https://api.github.com/repos/morpheus65535/Bazarr/releases '
url_releases = ' https://api.github.com/repos/morpheus65535/Bazarr/releases '
@ -58,20 +35,7 @@ def check_releases():
json . dump ( releases , f )
json . dump ( releases , f )
class Updater ( object ) :
def run_git ( args ) :
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 ' ]
git_locations = [ ' git ' ]
if platform . system ( ) . lower ( ) == ' darwin ' :
if platform . system ( ) . lower ( ) == ' darwin ' :
@ -102,68 +66,12 @@ class Updater(object):
elif output :
elif output :
break
break
return ( output , err )
return output , err
def getVersion ( self ) :
if os . path . isdir ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' .git ' ) ) and not args . release_update :
self . INSTALL_TYPE = ' git '
output , err = self . runGit ( ' rev-parse HEAD ' )
if not output :
logging . error ( ' Could not find latest installed version. ' )
cur_commit_hash = None
cur_commit_hash = str ( output )
def check_updates ( ) :
commits_behind = 0
if not re . match ( ' ^[a-z0-9]+$ ' , cur_commit_hash ) :
current_version = get_version ( )
logging . error ( ' Output does not look like a hash, not using it. ' )
cur_commit_hash = None
if settings . general . branch :
branch_name = settings . general . branch
else :
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
else :
remote_name = branch_name = None
if not remote_name and settings . general . branch :
logging . error ( ' Could not retrieve remote name from git. Defaulting to origin. ' )
branch_name = settings . general . branch
if not branch_name :
logging . error ( ' Could not retrieve branch name from git. Defaulting to master. ' )
branch_name = ' master '
return cur_commit_hash , ' origin ' , branch_name
else :
self . INSTALL_TYPE = ' source '
if self . CURRENT_VERSION :
return self . CURRENT_VERSION , ' origin ' , settings . general . branch
else :
return None , ' origin ' , settings . general . branch
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 :
self . UPDATE_AVAILABLE = False
print self . UPDATE_AVAILABLE
def check_github ( self ) :
self . COMMITS_BEHIND = 0
# Get the latest version available from github
# Get the latest version available from github
logging . info ( ' Retrieving latest version information from GitHub ' )
logging . info ( ' Retrieving latest version information from GitHub ' )
@ -172,60 +80,83 @@ class Updater(object):
if version is None :
if version is None :
logging . warn ( ' Could not get the latest version from GitHub. Are you running a local development version? ' )
logging . warn ( ' Could not get the latest version from GitHub. Are you running a local development version? ' )
return self . CURRENT_VERSION
return current_version
self . LATEST_VERSION = version [ ' sha ' ]
latest_version = version [ ' sha ' ]
logging . debug ( " Latest version is %s " , self . LATEST_VERSION )
logging . debug ( " Latest version is %s " , latest_version )
# See how many commits behind we are
# See how many commits behind we are
if not self . CURRENT_VERSION :
if not current_version :
logging . info ( ' You are running an unknown version of Bazarr. Run the updater to identify your version ' )
logging . info ( ' You are running an unknown version of Bazarr. Run the updater to identify your version ' )
return self . LATEST_VERSION
return latest_version
if self . LATEST_VERSION == self . CURRENT_VERSION :
if latest_version == current_version :
logging . info ( ' Bazarr is up to date ' )
logging . info ( ' Bazarr is up to date ' )
return self . LATEST_VERSION
return latest_version
logging . info ( ' Comparing currently installed version with latest GitHub version ' )
logging . info ( ' Comparing currently installed version with latest GitHub version ' )
url = ' https://api.github.com/repos/morpheus65535/bazarr/compare/ %s ... %s ' % ( self . LATEST_VERSION ,
url = ' https://api.github.com/repos/morpheus65535/bazarr/compare/ %s ... %s ' % ( latest_version ,
self . CURRENT_VERSION )
current_version )
commits = request_json ( url , timeout = 20 , whitelist_status_code = 404 , validator = lambda x : type ( x ) == dict )
commits = request_json ( url , timeout = 20 , whitelist_status_code = 404 , validator = lambda x : type ( x ) == dict )
if commits is None :
if commits is None :
logging . warn ( ' Could not get commits behind from GitHub. ' )
logging . warn ( ' Could not get commits behind from GitHub. ' )
return self . LATEST_VERSION
return latest_version
try :
try :
self . COMMITS_BEHIND = int ( commits [ ' behind_by ' ] )
commits_behind = int ( commits [ ' behind_by ' ] )
logging . debug ( " In total, %d commits behind " , self . COMMITS_BEHIND )
logging . debug ( " In total, %d commits behind " , commits_behind )
except KeyError :
except KeyError :
logging . info ( ' Cannot compare versions. Are you running a local development version? ' )
logging . info ( ' Cannot compare versions. Are you running a local development version? ' )
self . COMMITS_BEHIND = 0
commits_behind = 0
if self . COMMITS_BEHIND > 0 :
if commits_behind > 0 :
logging . info ( ' New version is available. You are %s commits behind ' % self . COMMITS_BEHIND )
logging . info ( ' New version is available. You are %s commits behind ' % commits_behind )
update ( )
url = ' https://api.github.com/repos/morpheus65535/bazarr/releases '
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 )
releases = request_json ( url , timeout = 20 , whitelist_status_code = 404 , validator = lambda x : type ( x ) == list )
if releases is None :
if releases is None :
logging . warn ( ' Could not get releases from GitHub. ' )
logging . warn ( ' Could not get releases from GitHub. ' )
return self . LATEST_VERSION
return latest_version
if settings . general . branch == ' master ' :
release = next ( ( r for r in releases if not r [ ' prerelease ' ] ) , releases [ 0 ] )
else :
else :
release = releases [ 0 ]
release = releases [ 0 ]
self . LATEST_RELEASE = release [ ' tag_name ' ]
latest_release = release [ ' tag_name ' ]
if ( ' v ' + current_version ) != latest_release and args . release_update :
update ( )
elif self . COMMITS_BEHIND == 0 :
elif commits_behind == 0 :
logging . info ( ' Bazarr is up to date ' )
logging . info ( ' Bazarr is up to date ' )
return self . LATEST_VERSION
return latest_version
def get_version ( ) :
if os . path . isdir ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' .git ' ) ) :
output , err = run_git ( ' rev-parse HEAD ' )
if not output :
logging . error ( ' Could not find latest installed version. ' )
cur_commit_hash = None
else :
cur_commit_hash = str ( output )
if not re . match ( ' ^[a-z0-9]+$ ' , cur_commit_hash ) :
logging . error ( ' Output does not look like a hash, not using it. ' )
cur_commit_hash = None
return cur_commit_hash
else :
return os . environ [ " BAZARR_VERSION " ]
def update ( self ) :
def update ( ) :
if self . INSTALL_TYPE == ' git ' and not args . release_update :
if not args . release_update :
output , err = self . runGit ( ' pull ' + ' origin ' + ' ' + settings . general . branch )
output , err = run_g it( ' pull ' + ' origin ' + ' ' + settings . general . branch )
if not output :
if not output :
logging . error ( ' Unable to download latest version ' )
logging . error ( ' Unable to download latest version ' )
@ -239,7 +170,7 @@ class Updater(object):
elif line . endswith ( ( ' Aborting ' , ' Aborting. ' ) ) :
elif line . endswith ( ( ' Aborting ' , ' Aborting. ' ) ) :
logging . error ( ' Unable to update from git: ' + line )
logging . error ( ' Unable to update from git: ' + line )
logging . info ( ' Output: ' + str ( output ) )
logging . info ( ' Output: ' + str ( output ) )
updated ( )
else :
else :
tar_download_url = ' https://github.com/morpheus65535/bazarr/tarball/ {} ' . format ( settings . general . branch )
tar_download_url = ' https://github.com/morpheus65535/bazarr/tarball/ {} ' . format ( settings . general . branch )
update_dir = os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' update ' )
update_dir = os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' update ' )
@ -285,22 +216,28 @@ class Updater(object):
if os . path . isfile ( new_path ) :
if os . path . isfile ( new_path ) :
os . remove ( new_path )
os . remove ( new_path )
os . renames ( old_path , new_path )
os . renames ( old_path , new_path )
updated ( )
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 :
class FakeLock ( object ) :
logging . error ( ' Unable to change git branch. ' )
"""
return
If no locking or request throttling is needed , use this
"""
for line in output . split ( ' \n ' ) :
def __enter__ ( self ) :
if line . endswith ( ( ' Aborting ' , ' Aborting. ' ) ) :
"""
logging . error ( ' Unable to checkout from git: ' + line )
Do nothing on enter
logging . info ( ' Output: ' + str ( output ) )
"""
pass
output , err = self . runGit ( ' pull origin %s ' % settings . general . branch )
def __exit__ ( self , type , value , traceback ) :
"""
Do nothing on exit
"""
pass
fake_lock = FakeLock ( )
def request_content ( url , * * kwargs ) :
def request_content ( url , * * kwargs ) :