-
Notifications
You must be signed in to change notification settings - Fork 9
/
updater.py
86 lines (56 loc) · 1.94 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import logging
import sys
try:
import git
except ImportError:
sys.exit("You need to install the GitPython requirement, e.g. sudo pip3.5 install GitPython")
logger = logging.getLogger("GIT")
logger.setLevel(logging.DEBUG)
############################################################
# UPDATER STUFF
############################################################
repo = git.Repo.init()
def active_branch():
global repo
try:
branch = repo.active_branch.name
return branch
except Exception as ex:
logger.exception("Exception retrieving current branch")
return 'Unknown'
def latest_version():
global repo
try:
fetch_info = repo.remotes.origin.fetch()
return fetch_info[0].commit
except Exception as ex:
logger.exception("Exception while checking for the latest version commit id")
return 'Unknown'
def current_version():
global repo
try:
result = repo.active_branch.commit
return result
except Exception as ex:
logger.exception("Exception while retrieving current version commit id")
return 'Unknown'
def update():
global repo
current = current_version()
latest = latest_version()
if current == 'Unknown' or latest == 'Unknown':
logger.debug("Aborting update because could not determine current / latest version")
return False
if current != latest:
logger.info("Updating to the latest version")
try:
pull_info = repo.remotes.origin.pull()
if pull_info[0].commit == latest:
logger.info("Successfully updated to version: %s", latest)
return True
except Exception as ex:
logger.exception("Exception while pulling the latest version from git, branch: %s - commit: %s",
active_branch(), latest)
else:
logger.info("Already using the latest version!")
return False