forked from lineageos-infra/updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_common.py
196 lines (147 loc) · 5.33 KB
/
api_common.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json
import os
import arrow
import requests
from flask import jsonify
from changelog import get_timestamp
from config import Config
from custom_exceptions import UpstreamApiException, DeviceNotFoundException
import extensions
@extensions.cache.memoize()
def get_builds():
try:
req = requests.get(Config.UPSTREAM_URL, timeout=60)
if req.status_code != 200:
raise UpstreamApiException('Unable to contact upstream API')
return json.loads(req.text)
except Exception as e:
print(e)
raise UpstreamApiException('Unable to contact upstream API')
def get_devices_with_builds():
return get_builds().keys()
@extensions.cache.memoize()
def get_device_builds(device):
builds = get_builds()
if device not in builds:
return []
device_builds = builds[device]
device_builds.sort(key=lambda b: b['datetime'], reverse=True)
def sorting_key(item):
filename = item['filename']
if filename.endswith(".zip"):
return 1, filename
elif filename.endswith(".img"):
return 2, filename
else:
return 3, filename
for build in device_builds:
build['files'] = sorted(build['files'], key=sorting_key)
return device_builds
@extensions.cache.memoize()
def get_build_roster():
devices = []
if os.path.isfile(Config.LINEAGE_BUILD_TARGETS_PATH):
with open(Config.LINEAGE_BUILD_TARGETS_PATH) as f:
for line in f.readlines():
if line and not line.startswith('#'):
devices.append(line.split()[0])
elif Config.OFFICIAL_LINEAGE_BUILD_TARGETS_URL:
for line in requests.get(Config.OFFICIAL_LINEAGE_BUILD_TARGETS_URL, timeout=60).text.splitlines():
if line and not line.startswith('#'):
devices.append(line.split()[0])
return devices
@extensions.cache.memoize()
def get_devices_data():
devices_data = []
if os.path.isfile(Config.DEVICES_JSON_PATH):
with open(Config.DEVICES_JSON_PATH) as f:
devices_data += json.loads(f.read())
else:
devices_data += requests.get(Config.OFFICIAL_DEVICES_JSON_URL, timeout=60).json()
if os.path.isfile(Config.DEVICES_LOCAL_JSON_PATH):
with open(Config.DEVICES_LOCAL_JSON_PATH) as f:
devices_data += json.loads(f.read())
build_roster = get_build_roster()
devices_with_builds = get_devices_with_builds()
devices = []
for device_data in devices_data:
if device_data['model'] in devices_with_builds or device_data['model'] in build_roster:
devices.append(device_data)
return devices
@extensions.cache.memoize()
def get_device_data(device):
devices_data = get_devices_data()
for device_data in devices_data:
if device_data['model'] == device:
return device_data
raise DeviceNotFoundException('This device does not exist')
@extensions.cache.memoize()
def get_oems():
devices_data = get_devices_data()
oems = {}
for device_data in devices_data:
oems.setdefault(device_data['oem'], []).append(device_data)
return oems
@extensions.cache.memoize()
def get_build_types(device, romtype, after, version):
roms = get_device_builds(device)
roms = [x for x in roms if x['type'] == romtype]
for rom in roms:
rom['date'] = arrow.get(rom['date']).datetime
if after:
after = arrow.get(after).datetime
roms = [x for x in roms if x['date'] > after]
if version:
roms = [x for x in roms if x['version'] == version]
data = []
for rom in roms:
data.append({
'id': rom['files'][0]['sha256'],
'url': '%s%s' % (Config.DOWNLOAD_BASE_URL, rom['files'][0]['filepath']),
'romtype': rom['type'],
'datetime': rom['datetime'],
'version': rom['version'],
'filename': rom['files'][0]['filename'],
'size': rom['files'][0]['size'],
})
return jsonify({'response': data})
@extensions.cache.memoize()
def get_device_version(device):
if device == 'all':
return None
return get_device_builds(device)[-1]['version']
@extensions.cache.memoize()
def get_device_versions(device):
roms = get_device_builds(device)
versions = set()
for rom in roms:
versions.add(rom['version'])
return list(versions)
@extensions.cache.memoize()
def group_changes_by_build(changes, builds, versions):
builds_changes = []
builds.sort(key=lambda b: b['datetime'])
for build in builds:
build_changes = {
'build': build,
'items': []
}
for change in changes:
submit_timestamp = get_timestamp(change.submitted)
if submit_timestamp <= build['datetime'] and build['version'] in change.branch:
build_changes['items'].append(change)
builds_changes.insert(0, build_changes)
changes = [c for c in changes if c not in build_changes['items']]
for version in versions:
build_changes = {
'build': {
'filename': 'next',
'version': version,
},
'items': []
}
for change in changes:
if version in change.branch:
build_changes['items'].append(change)
builds_changes.insert(0, build_changes)
return builds_changes