-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_updater.py
More file actions
76 lines (61 loc) · 2.02 KB
/
auto_updater.py
File metadata and controls
76 lines (61 loc) · 2.02 KB
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
import os
import time
import git
from logs import log
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
class AutoUpdater:
def __init__(self, is_on_gcp=False):
self.is_on_gcp = is_on_gcp
self.last_update_check_time = None
if not self.is_on_gcp:
log.info('Not pulling latest on non-gcp machines, assuming you are '
'in dev')
def updated(self) -> bool:
"""
:return: Whether or not we updated our local repo
"""
now = time.time()
if not self.is_on_gcp:
ret = False
elif self.last_update_check_time is not None:
log.debug('Checking for source changes')
if now - self.last_update_check_time > 3:
ret = self.pull_latest(now)
self.last_update_check_time = time.time()
else:
ret = False
else:
ret = self.pull_latest(now)
self.last_update_check_time = time.time()
return ret
def pull_latest(self, now):
log.debug('Pulling latest from github..')
self.last_update_check_time = now
if pull_latest():
log.success('Pulled new changes')
ret = True
else:
ret = False
return ret
def pull_latest(check_first=False, remote_branch='production'):
ret = False
repo = git.Repo(ROOT_DIR)
if check_first:
origin = [r for r in repo.remotes if r.name == 'origin'][0]
# Fetch all origin changes
origin.update()
head = repo.head.commit.hexsha
prod = [b for b in origin.refs
if b.name == f'origin/{remote_branch}'][0]
prod_head = prod.commit.hexsha
should_pull = head != prod_head
else:
should_pull = True
if should_pull:
pull_result = repo.git.pull('origin', remote_branch)
if pull_result != 'Already up to date.':
ret = True
log.info(pull_result)
return ret
if __name__ == '__main__':
pull_latest()