1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-25 17:17:12 +00:00
bazarr/check_update.py

60 lines
2.6 KiB
Python
Raw Normal View History

2017-10-28 02:18:16 +00:00
from get_general_settings import *
2017-11-07 04:53:31 +00:00
import os
2017-11-11 05:54:19 +00:00
import pygit2
2017-12-06 04:07:37 +00:00
import logging
import sqlite3
2017-11-07 04:53:31 +00:00
2017-11-14 14:38:34 +00:00
current_working_directory = os.path.dirname(__file__)
2017-11-11 05:54:19 +00:00
repository_path = pygit2.discover_repository(current_working_directory)
local_repo = pygit2.Repository(repository_path)
2017-11-07 05:11:42 +00:00
2017-11-11 05:54:19 +00:00
def check_and_apply_update(repo=local_repo, remote_name='origin'):
2017-12-05 20:02:08 +00:00
repo.config['remote.origin.fetch'] = '+refs/heads/*:refs/remotes/origin/*'
2017-12-05 20:29:41 +00:00
repo.remotes[remote_name].fetch()
2017-12-05 20:02:08 +00:00
repo.config['user.name'] = 'Bazarr user'
2017-12-05 19:21:19 +00:00
repo.config['user.email'] ='bazarr@fakeuser.com'
2017-12-05 20:02:08 +00:00
2017-11-11 05:54:19 +00:00
for remote in repo.remotes:
if remote.name == remote_name:
remote.fetch()
2017-11-27 00:54:59 +00:00
remote_id = repo.lookup_reference('refs/remotes/origin/' + str(branch)).target
2017-11-11 05:54:19 +00:00
merge_result, _ = repo.merge_analysis(remote_id)
# Up to date, do nothing
if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
2017-12-06 04:07:37 +00:00
logging.info('No new version of Bazarr available.')
2017-11-15 01:20:44 +00:00
pass
2017-11-11 05:54:19 +00:00
# We can just fastforward
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
repo.checkout_tree(repo.get(remote_id))
2017-12-05 19:14:51 +00:00
master_ref = repo.lookup_reference('refs/remotes/origin/' + str(branch))
2017-11-11 05:54:19 +00:00
master_ref.set_target(remote_id)
repo.head.set_target(remote_id)
2017-12-16 03:31:50 +00:00
logging.info('Bazarr updated to latest version and need to be restarted.')
updated()
2017-11-22 11:37:17 +00:00
# We can just do it normally
2017-11-21 21:09:33 +00:00
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
repo.merge(remote_id)
print repo.index.conflicts
assert repo.index.conflicts is None, 'Conflicts, ahhhh!'
user = repo.default_signature
tree = repo.index.write_tree()
commit = repo.create_commit('HEAD',
user,
user,
'Merge!',
tree,
2017-11-21 21:10:20 +00:00
[repo.head.target, remote_id])
2017-11-21 21:09:33 +00:00
repo.state_cleanup()
2017-12-06 04:07:37 +00:00
logging.error('Conflict detected when trying to update.')
2017-11-22 11:37:17 +00:00
# We can't do it
2017-11-11 05:54:19 +00:00
else:
2017-12-06 04:07:37 +00:00
logging.error('Bazarr cannot be updated: Unknown merge analysis result')
def updated():
conn = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c = conn.cursor()
c.execute("UPDATE table_settings_general SET updated = 1")
conn.commit()
c.close()