From 4d5839215991043e98f31726d893bbd38e462452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sat, 11 Nov 2017 00:54:19 -0500 Subject: [PATCH] Updater development --- bazarr.py | 4 ++-- check_update.py | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bazarr.py b/bazarr.py index d7ff06423..8a4e4fce9 100644 --- a/bazarr.py +++ b/bazarr.py @@ -299,10 +299,10 @@ def save_settings(): redirect(ref) @route(base_url + 'check_update') -def manual_update(): +def check_update(): ref = request.environ['HTTP_REFERER'] - root.warning(check_and_apply_update()) + check_and_apply_update() redirect(ref) diff --git a/check_update.py b/check_update.py index b6c3a1894..1b39ec551 100644 --- a/check_update.py +++ b/check_update.py @@ -1,13 +1,27 @@ from get_general_settings import * import os -import subprocess +import pygit2 -def check_and_apply_update(): - result = subprocess.check_output(["git", "pull", '--dry-run', 'origin', branch], stderr=subprocess.STDOUT, shell=True, cwd=os.path.join(os.path.dirname(__file__))).split('\n') +current_working_directory = os.getcwd() +repository_path = pygit2.discover_repository(current_working_directory) +local_repo = pygit2.Repository(repository_path) - if result[2] is not '': - subprocess.check_output(["git", "pull", 'origin', branch], shell=True, cwd=os.path.join(os.path.dirname(__file__))) - os.execlp('python', 'python ' + os.path.join(os.path.dirname(__file__), 'bazarr.py')) - - return result +def check_and_apply_update(repo=local_repo, remote_name='origin'): + for remote in repo.remotes: + if remote.name == remote_name: + remote.fetch() + remote_id = repo.lookup_reference('refs/remotes/origin/' + branch).target + merge_result, _ = repo.merge_analysis(remote_id) + # Up to date, do nothing + if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE: + print 'Up to date' + return + # We can just fastforward + elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD: + repo.checkout_tree(repo.get(remote_id)) + master_ref = repo.lookup_reference('refs/heads/master') + master_ref.set_target(remote_id) + repo.head.set_target(remote_id) + else: + raise AssertionError('Unknown merge analysis result')