-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoupdater.py
103 lines (84 loc) · 3.09 KB
/
autoupdater.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import time
import json
import urllib2
import threading
import version
class Updater(threading.Thread):
def __init__(self, app, callback, githubuser, repo, branch = "master"):
super(Updater, self).__init__()
self.callback = callback
self._app = app
self.user = githubuser
self.repo = repo
self.branch = branch
self.version = [str(sub) for sub in json.loads(version.version)]
self.patch_notes = ""
self._next_check = 15
self.remote_version = [0]
def run(self):
while self._app.alive:
if self._next_check <= 0: #<= instead of == saves locking
if self.update_available():
self.callback(self.version,
self.remote_version,
self.patch_notes)
self._next_check = 60*60*5
else:
self._next_check -= 1
time.sleep(1)
def _get_github_stub(self):
skele = "https://raw.githubusercontent.com/%s/%s/%s/"
url = skele % (self.user, self.repo, self.branch)
return url
def _get_github_file(self, fname, readlines = False):
url = self._get_github_stub() + fname
try:
res = urllib2.urlopen(url)
except urllib2.HTTPError:
return [] if readlines else ""
if readlines:
remote = res.readlines()
else:
remote = res.read()
return remote
def get_remote_version(self):
remote = self._get_github_file("version.py", readlines = True)
remote = [line.strip() for line in remote]
version = [line for line in remote if line.startswith("version")]
if not version:
#All version tuples are greater than this, therefore an update is
# not available or unavailable
return 0
version = json.loads(version[0].split("=")[-1].strip("\"\' "))
return [str(sub) for sub in version]
def update_patch_notes(self):
self.patch_notes = self._get_github_file("Patch%20Notes.txt")
def update_available(self):
self.remote_version = self.get_remote_version()
if self.remote_version > self.version:
self.update_patch_notes()
return True
else:
return False
def ignore_remote_version(self):
self.version = self.get_remote_version()
def check_now(self):
self._next_check = 0
def update(updater_instance, pid, restart_file):
with file("__patch notes.tmp", "w") as f:
f.write(updater_instance.patch_notes)
zip_skele = "https://github.com/%s/%s/archive/%s.zip"
zip = zip_skele % (
updater_instance.user,
updater_instance.repo,
updater_instance.branch)
args = (pid, "__patch notes.tmp", zip, restart_file)
out = []
for arg in args:
str_arg = str(arg)
escaped = str_arg.replace("\"","\\\"")
enclosed = "\"" + escaped + "\""
out.append(enclosed)
args = " ".join(out)
os.system("start upgrade.py" + " " + args)