-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdater.py
66 lines (49 loc) · 1.87 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
import json
import os
import sys
import requests
from PySide2 import QtCore
def get_git_rev():
return open('.git/' + (open('.git/HEAD').read().strip().split(': ')[1])).read()[:7] # noqa
def get_bundle_rev():
return open(os.path.join(sys._MEIPASS, 'build_id.txt')).read().decode('utf-8').strip() # noqa pylint: disable=no-member,protected-access
def check_updates(rev, target_mime='application/x-ms-dos-executable'):
tags = requests.get('https://api.github.com/repos/nuke3/ChainSign/tags').json()
rev_tag = next((t['name'] for t in tags if t['commit']['sha'].startswith(rev)), None)
if not rev_tag:
return None
releases = requests.get('https://api.github.com/repos/nuke3/ChainSign/releases').json()
rel = next((r for r in releases if r['tag_name'] == rev_tag), None)
if not rel:
return None
if rel['prerelease']:
latest = releases[0]
else:
latest = next((r for r in releases if not r['prerelease']), rel)
if latest['id'] != rel['id']:
binary_url = next((
a['browser_download_url'] for a in latest['assets']
if a['content_type'] == target_mime
), None)
return {
'latest': latest,
'current': rel,
'binary_url': binary_url,
}
return {
'latest': latest,
'current': rel,
}
class UpdateCheckerThread(QtCore.QThread):
response = QtCore.Signal(str) # FIXME: this shouldn't throw json...
def run(self):
try:
try:
rev = get_bundle_rev()
except Exception as exc: # pylint: disable=broad-except
print(str(exc))
rev = get_git_rev()
resp = check_updates(rev)
except Exception as exc: # pylint: disable=broad-except
resp = {"error": str(exc)}
self.response.emit(json.dumps(resp))