-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdate.py
62 lines (53 loc) · 2.25 KB
/
update.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
import urllib.request, urllib.error, os.path, sys
class updateManager():
def __init__(self, url, version, filename='VERSION'):
self.url = url.replace('github', 'raw.githubusercontent') + '/master/' + filename
self.version = version
self.filename = filename
self.create_version_file()
self.update_local_file()
self.update = False
self.update_check()
def update_check(self):
if getattr(sys, 'frozen', False) and sys.platform == 'darwin':
os.environ['SSL_CERT_FILE'] = os.path.join(sys._MEIPASS, 'lib', 'cert.pem')
if self.check_for_update(self.check_remote_version(), self.check_local_version()):
self.update = True
else:
self.update = False
@staticmethod
def check_for_update(remote, local):
remote = remote.split('.')
local = local.split('.')
if len(remote) > len(local):
for x in range(len(local)):
if int(remote[x]) < int(local[x]):
return False
return True
elif len(remote) <= len(local):
if remote == local:
return False
for x in range(len(remote)):
if int(remote[x]) < int(local[x]):
return False
return True
def create_version_file(self):
if not os.path.isfile(self.filename):
open(self.filename, 'w').write(self.version)
def update_local_file(self):
if self.check_for_update(self.version, self.check_local_version()):
open(self.filename, 'w').write(self.version)
elif self.check_for_update(self.check_local_version(), self.version):
print('ERROR: VERSION file higher than script version')
def check_remote_version(self):
try:
req = urllib.request.urlopen(self.url)
return req.read().decode()
except urllib.error.HTTPError:
print('ERROR: remote VERSION file not accessible')
exit(1)
def check_local_version(self):
if not os.path.isfile(self.filename):
print('Local version file not found')
return None
return open(self.filename, 'r').read()