bazarr/bazarr/check_update.py

395 lines
15 KiB
Python
Raw Normal View History

2019-02-16 15:58:35 +00:00
# coding=utf-8
2017-11-07 04:53:31 +00:00
import os
2019-02-15 20:38:10 +00:00
import platform
import re
import subprocess
import tarfile
2017-12-06 04:07:37 +00:00
import logging
2019-02-15 20:38:10 +00:00
import requests
import sqlite3
2018-11-02 19:08:07 +00:00
import json
2017-11-07 04:53:31 +00:00
from get_args import args
2019-02-17 19:07:21 +00:00
from config import settings, bazarr_url
2019-02-24 18:31:18 +00:00
from queueconfig import notifications
2019-01-01 20:31:01 +00:00
2018-11-02 19:08:07 +00:00
def check_releases():
releases = []
url_releases = 'https://api.github.com/repos/morpheus65535/Bazarr/releases'
try:
r = requests.get(url_releases, timeout=15)
r.raise_for_status()
except requests.exceptions.HTTPError as errh:
logging.exception("Error trying to get releases from Github. Http error.")
except requests.exceptions.ConnectionError as errc:
logging.exception("Error trying to get releases from Github. Connection Error.")
except requests.exceptions.Timeout as errt:
logging.exception("Error trying to get releases from Github. Timeout Error.")
except requests.exceptions.RequestException as err:
logging.exception("Error trying to get releases from Github.")
else:
for release in r.json():
releases.append([release['name'], release['body']])
2018-11-28 11:53:37 +00:00
with open(os.path.join(args.config_dir, 'config', 'releases.txt'), 'w') as f:
2018-11-02 19:08:07 +00:00
json.dump(releases, f)
2019-02-16 15:58:35 +00:00
def run_git(args):
git_locations = ['git']
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if platform.system().lower() == 'darwin':
git_locations.append('/usr/local/git/bin/git')
output = err = None
for cur_git in git_locations:
cmd = cur_git + ' ' + args
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
try:
2019-03-05 17:41:06 +00:00
logging.debug('BAZZAR Trying to execute: "' + cmd + '"')
2019-02-16 15:58:35 +00:00
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
output, err = p.communicate()
output = output.strip()
2019-02-17 22:40:10 +00:00
logging.debug('BAZZAR Git output: ' + output)
2019-02-16 15:58:35 +00:00
except OSError:
2019-02-17 22:40:10 +00:00
logging.debug('BAZZAR Command failed: %s', cmd)
2019-02-16 15:58:35 +00:00
continue
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if 'not found' in output or "not recognized as an internal or external command" in output:
2019-02-17 22:40:10 +00:00
logging.debug('BAZZAR Unable to find git with command ' + cmd)
2019-02-16 15:58:35 +00:00
output = None
elif 'fatal:' in output or err:
2019-02-17 22:40:10 +00:00
logging.error('BAZZAR Git returned bad info. Are you sure this is a git installation?')
2019-02-16 15:58:35 +00:00
output = None
elif output:
break
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
return output, err
def check_updates():
commits_behind = 0
2019-03-05 17:41:06 +00:00
current_version, source = get_version()
2019-03-09 08:37:47 +00:00
check_releases()
2019-03-05 17:41:06 +00:00
if source == 'git':
# Get the latest version available from github
logging.info('BAZZAR Retrieving latest version information from GitHub')
url = 'https://api.github.com/repos/morpheus65535/bazarr/commits/%s' % settings.general.branch
version = request_json(url, timeout=20, validator=lambda x: type(x) == dict)
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if version is None:
logging.warn(
'BAZZAR Could not get the latest version from GitHub.')
return
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
latest_version = version['sha']
logging.debug("BAZZAR Latest version is %s", latest_version)
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
# See how many commits behind we are
if not current_version:
logging.info(
'BAZARR You are running an unknown version of Bazarr. Run the updater to identify your version')
return
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if latest_version == current_version:
notifications.write(msg='BAZARR is up to date', queue='check_update')
logging.info('BAZARR is up to date')
return
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
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)
commits = request_json(url, timeout=20, whitelist_status_code=404, validator=lambda x: type(x) == dict)
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if commits is None:
logging.warn('BAZARR Could not get commits behind from GitHub.')
return
2019-02-15 20:38:10 +00:00
2019-03-05 17:41:06 +00:00
try:
commits_behind = int(commits['behind_by'])
logging.debug("BAZARR In total, %d commits behind", commits_behind)
except KeyError:
logging.info('BAZARR Cannot compare versions. Are you running a local development version?')
commits_behind = 0
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if commits_behind > 0:
logging.info('BAZARR New version is available. You are %s commits behind' % commits_behind)
notifications.write(msg='BAZARR New version is available. You are %s commits behind' % commits_behind,
queue='check_update')
update(source, restart=True if settings.general.getboolean('update_restart') else False)
2019-02-17 22:40:10 +00:00
2019-03-05 17:41:06 +00:00
else:
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)
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if releases is None:
logging.warn('BAZARR Could not get releases from GitHub.')
return
else:
release = releases[0]
latest_release = release['tag_name']
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
if ('v' + current_version) != latest_release and settings.general.branch == 'master':
update(source, restart=True if settings.general.getboolean('update_restart') else False)
elif settings.general.branch != 'master':
notifications.write(msg="BAZZAR Can't update development branch from source", queue='check_update') # fixme
logging.info("BAZZAR Can't update development branch from source") # fixme
else:
notifications.write(msg='BAZZAR is up to date', queue='check_update')
logging.info('BAZZAR is up to date')
2019-02-16 15:58:35 +00:00
def get_version():
2019-03-05 17:41:06 +00:00
if os.path.isdir(os.path.join(os.path.dirname(__file__), '..', '.git')) and not args.release_update:
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
output, err = run_git('rev-parse HEAD')
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if not output:
2019-02-17 22:40:10 +00:00
logging.error('BAZZAR Could not find latest installed version.')
2019-02-16 15:58:35 +00:00
cur_commit_hash = None
else:
cur_commit_hash = str(output)
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if not re.match('^[a-z0-9]+$', cur_commit_hash):
2019-02-17 22:40:10 +00:00
logging.error('BAZZAR Output does not look like a hash, not using it.')
2019-02-16 15:58:35 +00:00
cur_commit_hash = None
2019-03-05 17:41:06 +00:00
return cur_commit_hash, 'git'
2019-02-16 15:58:35 +00:00
else:
2019-03-05 17:41:06 +00:00
return os.environ["BAZARR_VERSION"], 'source'
2019-02-16 15:58:35 +00:00
2019-03-05 17:41:06 +00:00
def update(source, restart=True):
if source == 'git':
2019-02-16 15:58:35 +00:00
output, err = run_git('pull ' + 'origin' + ' ' + settings.general.branch)
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if not output:
2019-02-17 22:40:10 +00:00
logging.error('BAZZAR Unable to download latest version')
2019-02-16 15:58:35 +00:00
return
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
for line in output.split('\n'):
if 'Already up-to-date.' in line:
2019-02-17 22:40:10 +00:00
logging.info('BAZZAR No update available, not updating')
logging.info('BAZZAR Output: ' + str(output))
2019-02-16 15:58:35 +00:00
elif line.endswith(('Aborting', 'Aborting.')):
2019-02-17 22:40:10 +00:00
logging.error('BAZZAR Unable to update from git: ' + line)
logging.info('BAZZAR Output: ' + str(output))
2019-03-05 17:41:06 +00:00
updated(restart)
2019-02-16 15:58:35 +00:00
else:
tar_download_url = 'https://github.com/morpheus65535/bazarr/tarball/{}'.format(settings.general.branch)
update_dir = os.path.join(os.path.dirname(__file__), '..', 'update')
2019-02-17 22:40:10 +00:00
logging.info('BAZZAR Downloading update from: ' + tar_download_url)
2019-03-05 17:41:06 +00:00
notifications.write(msg='BAZZAR Downloading update from: ' + tar_download_url)
2019-02-16 15:58:35 +00:00
data = request_content(tar_download_url)
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
if not data:
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR Unable to retrieve new version from '%s', can't update", tar_download_url)
2019-03-05 17:41:06 +00:00
notifications.write(msg=("BAZZAR Unable to retrieve new version from '%s', can't update", tar_download_url),
type='error')
2019-02-16 15:58:35 +00:00
return
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
download_name = settings.general.branch + '-github'
tar_download_path = os.path.join(os.path.dirname(__file__), '..', download_name)
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
# Save tar to disk
with open(tar_download_path, 'wb') as f:
f.write(data)
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
# Extract the tar to update folder
2019-02-17 22:40:10 +00:00
logging.info('BAZZAR Extracting file: ' + tar_download_path)
2019-03-05 17:41:06 +00:00
notifications.write(msg='BAZZAR Extracting file: ' + tar_download_path)
2019-02-16 15:58:35 +00:00
tar = tarfile.open(tar_download_path)
tar.extractall(update_dir)
tar.close()
2019-02-15 20:38:10 +00:00
2019-02-16 15:58:35 +00:00
# Delete the tar.gz
2019-02-17 22:40:10 +00:00
logging.info('BAZZAR Deleting file: ' + tar_download_path)
2019-03-05 17:41:06 +00:00
notifications.write(msg='BAZZAR Deleting file: ' + tar_download_path)
2019-02-16 15:58:35 +00:00
os.remove(tar_download_path)
# Find update dir name
update_dir_contents = [x for x in os.listdir(update_dir) if os.path.isdir(os.path.join(update_dir, x))]
if len(update_dir_contents) != 1:
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR Invalid update data, update failed: " + str(update_dir_contents))
2019-03-05 17:41:06 +00:00
notifications.write(msg="BAZZAR Invalid update data, update failed: " + str(update_dir_contents),
type='error')
2019-02-16 15:58:35 +00:00
return
2019-03-05 17:41:06 +00:00
2019-02-16 15:58:35 +00:00
content_dir = os.path.join(update_dir, update_dir_contents[0])
# walk temp folder and move files to main folder
for dirname, dirnames, filenames in os.walk(content_dir):
dirname = dirname[len(content_dir) + 1:]
for curfile in filenames:
old_path = os.path.join(content_dir, dirname, curfile)
new_path = os.path.join(os.path.dirname(__file__), '..', dirname, curfile)
if os.path.isfile(new_path):
os.remove(new_path)
os.renames(old_path, new_path)
2019-03-05 17:41:06 +00:00
updated(restart)
2019-02-16 15:58:35 +00:00
class FakeLock(object):
"""
If no locking or request throttling is needed, use this
"""
2019-02-15 23:21:31 +00:00
2019-02-16 15:58:35 +00:00
def __enter__(self):
"""
Do nothing on enter
"""
pass
def __exit__(self, type, value, traceback):
"""
Do nothing on exit
"""
pass
fake_lock = FakeLock()
2019-02-15 20:38:10 +00:00
def request_content(url, **kwargs):
"""
Wrapper for `request_response', which will return the raw content.
"""
response = request_response(url, **kwargs)
if response is not None:
return response.content
def request_response(url, method="get", auto_raise=True,
whitelist_status_code=None, lock=fake_lock, **kwargs):
"""
Convenient wrapper for `requests.get', which will capture the exceptions
and log them. On success, the Response object is returned. In case of a
exception, None is returned.
Additionally, there is support for rate limiting. To use this feature,
supply a tuple of (lock, request_limit). The lock is used to make sure no
other request with the same lock is executed. The request limit is the
minimal time between two requests (and so 1/request_limit is the number of
requests per seconds).
"""
# Convert whitelist_status_code to a list if needed
if whitelist_status_code and type(whitelist_status_code) != list:
whitelist_status_code = [whitelist_status_code]
# Disable verification of SSL certificates if requested. Note: this could
# pose a security issue!
kwargs["verify"] = True
# Map method to the request.XXX method. This is a simple hack, but it
# allows requests to apply more magic per method. See lib/requests/api.py.
request_method = getattr(requests, method.lower())
try:
# Request URL and wait for response
with lock:
logging.debug(
2019-02-17 22:40:10 +00:00
"BAZZAR Requesting URL via %s method: %s", method.upper(), url)
2019-02-15 20:38:10 +00:00
response = request_method(url, **kwargs)
# If status code != OK, then raise exception, except if the status code
# is white listed.
if whitelist_status_code and auto_raise:
if response.status_code not in whitelist_status_code:
try:
response.raise_for_status()
except:
logging.debug(
2019-02-17 22:40:10 +00:00
"BAZZAR Response status code %d is not white "
2019-02-15 20:38:10 +00:00
"listed, raised exception", response.status_code)
raise
elif auto_raise:
response.raise_for_status()
return response
except requests.exceptions.SSLError as e:
if kwargs["verify"]:
logging.error(
2019-02-17 22:40:10 +00:00
"BAZZAR Unable to connect to remote host because of a SSL error. "
2019-02-15 20:38:10 +00:00
"It is likely that your system cannot verify the validity"
"of the certificate. The remote certificate is either "
"self-signed, or the remote server uses SNI. See the wiki for "
"more information on this topic.")
else:
logging.error(
2019-02-17 22:40:10 +00:00
"BAZZAR SSL error raised during connection, with certificate "
2019-02-15 20:38:10 +00:00
"verification turned off: %s", e)
except requests.ConnectionError:
logging.error(
2019-02-17 22:40:10 +00:00
"BAZZAR Unable to connect to remote host. Check if the remote "
2019-02-15 20:38:10 +00:00
"host is up and running.")
except requests.Timeout:
logging.error(
2019-02-17 22:40:10 +00:00
"BAZZAR Request timed out. The remote host did not respond timely.")
2019-02-15 20:38:10 +00:00
except requests.HTTPError as e:
if e.response is not None:
if e.response.status_code >= 500:
cause = "remote server error"
elif e.response.status_code >= 400:
cause = "local client error"
else:
# I don't think we will end up here, but for completeness
cause = "unknown"
logging.error(
2019-02-17 22:40:10 +00:00
"BAZZAR Request raise HTTP error with status code %d (%s).",
2019-02-15 20:38:10 +00:00
e.response.status_code, cause)
else:
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR Request raised HTTP error.")
2019-02-15 20:38:10 +00:00
except requests.RequestException as e:
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR Request raised exception: %s", e)
2019-02-15 20:38:10 +00:00
def request_json(url, **kwargs):
"""
Wrapper for `request_response', which will decode the response as JSON
object and return the result, if no exceptions are raised.
As an option, a validator callback can be given, which should return True
if the result is valid.
"""
validator = kwargs.pop("validator", None)
response = request_response(url, **kwargs)
if response is not None:
try:
result = response.json()
if validator and not validator(result):
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR JSON validation result failed")
2019-02-15 20:38:10 +00:00
else:
return result
except ValueError:
2019-02-17 22:40:10 +00:00
logging.error("BAZZAR Response returned invalid JSON data")
2019-02-15 20:38:10 +00:00
2019-02-17 22:40:10 +00:00
def updated(restart=False):
if restart:
try:
requests.get(bazarr_url + 'restart')
except requests.ConnectionError:
logging.info('BAZARR Restart failed, please restart Bazarr manualy')
updated(restart=False)
else:
conn = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
c = conn.cursor()
c.execute("UPDATE system SET updated = 1")
conn.commit()
c.close()