-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.py
308 lines (256 loc) · 12.2 KB
/
database.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
Database related functions
"""
from typing import Optional, Union
from sqlalchemy import or_
from sqlalchemy.engine import result
from sqlalchemy.sql.functions import concat, func
from . import session
from .models.device import Device
from .models.miui_update import Update
def get_mi_website_ids() -> result:
"""
SELECT mi_website_id as id, region FROM devices WHERE mi_website_id IS NOT NULL AND eol != 1 GROUP BY mi_website_id
:return: list of tuples of (id, region)
"""
return session.query(Device.mi_website_id, Device.region).filter(Device.mi_website_id != None).filter(
Device.eol != 1).group_by(Device.mi_website_id)
def get_fastboot_codenames() -> result:
"""
SELECT codename, region FROM devices WHERE mi_website_id IS NOT NULL AND eol != 1 ORDER BY codename
:return: list of codenames
"""
return session.query(Device.codename, Device.region).filter(Device.mi_website_id != None).filter(
Device.eol != 1).order_by(Device.codename).all()
def get_current_devices() -> result:
"""
SELECT codename from devices WHERE eol = 0 AND miui_code != "" AND (LENGTH(miui_code) = 4 or miui_code like '%RF' or miui_code like '%FK')
:return: list of codenames
"""
return session.query(
Device.codename).filter(Device.eol == "0").filter(Device.miui_code != "").filter(
or_(func.length(Device.miui_code) == 4,
Device.miui_code.endswith("RF"), Device.miui_code.endswith("FK"))).all()
def get_devices() -> result:
"""
SELECT codename, CONCAT(name, ' ', region) as name, miui_name
from devices
WHERE miui_code != ""
AND LENGTH(miui_code) = 4
GROUP BY codename
ORDER BY codename
"""
return session.query(
Device.codename, concat(Device.name, ' ', Device.region).label('name'), Device.miui_name
).filter(Device.miui_code != "").filter(or_(
func.length(Device.miui_code) == 4, Device.miui_code.endswith("RF"),
Device.miui_code.endswith("FK"))).order_by(Device.codename).all()
def get_device_latest_version(codename) -> result:
"""
SELECT codename, version, android from updates WHERE codename = 'codename' AND updates.branch like "Stable%" AND updates.type = "Full" ORDER BY date DESC LIMIT 1
:param codename: device codename
:return: codename, version, android object
"""
return session.query(Update.codename, Update.version, Update.android).filter(Update.codename == codename).filter(
Update.branch.startswith("Stable")).filter(Update.type == "Full").order_by(Update.date.desc()).limit(1).first()
def get_latest_versions(branch: str = "Stable") -> result:
"""
SELECT latest.*
FROM devices,
(
SELECT all_latest.*
FROM (SELECT codename, version, android
from updates
WHERE updates.branch like "Stable%"
AND updates.type = "Full"
ORDER BY updates.date DESC
LIMIT 99999) as all_latest
GROUP BY all_latest.codename) as latest
WHERE latest.codename = devices.codename
AND devices.eol = 0
AND devices.miui_code != ""
AND (LENGTH(miui_code) = 4 or miui_code like '%RF' or miui_code like '%FK')
"""
all_latest = session.query(Update.codename, Update.version, Update.android).filter(
Update.branch.startswith(branch)).filter(Update.type == "Full").order_by(Update.date.desc()).limit(
99999).subquery()
latest = session.query(all_latest).group_by(all_latest.c.codename).subquery()
updates = session.query(latest).filter(latest.c.codename == Device.codename).filter(
Device.eol == '0').filter(Device.miui_code != "").filter(
or_(func.length(Device.miui_code) == 4, Device.miui_code.endswith("RF"), Device.miui_code.endswith("FK"))).all()
return updates
def get_latest_updates(branch: str = "Stable") -> result:
"""
SELECT CONCAT(devices.name, ' ', devices.region) as name, latest.*
FROM devices,
(
SELECT all_latest.*
FROM (SELECT codename, version, branch, method, size, md5, link, changelog, date
from updates
WHERE updates.branch = "Stable"
AND updates.type = "Full"
ORDER BY updates.date DESC
LIMIT 99999) as all_latest
GROUP BY all_latest.codename, all_latest.method) as latest
WHERE latest.codename = devices.codename
AND devices.miui_code != ""
AND (LENGTH(miui_code) = 4 or miui_code like '%RF' or miui_code like '%FK')
ORDER BY date desc
"""
all_latest = session.query(
Update.codename, Update.version, Update.android, Update.branch,
Update.method, Update.size, Update.md5, Update.link, Update.changelog, Update.date).filter(
Update.branch == branch).filter(Update.type == "Full").order_by(Update.date.desc()).limit(99999).subquery()
latest = session.query(all_latest).group_by(all_latest.c.codename).group_by(all_latest.c.method).subquery()
updates = session.query(Device.name, concat(Device.name, ' ', Device.region).label('fullname'), latest).filter(
latest.c.codename == Device.codename).filter(Device.miui_code != "").filter(
or_(func.length(Device.miui_code) == 4, Device.miui_code.endswith("RF"),
Device.miui_code.endswith("FK"))).order_by(latest.c.date.desc()).all()
return updates
def get_all_latest_updates():
return get_latest_updates(branch="Stable Beta") + get_latest_updates() + get_latest_updates(
branch="Weekly") + get_latest_updates("Public Beta")
def get_device_latest(codename) -> result:
"""
SELECT CONCAT(devices.name, ' ', devices.region) as name, latest.*
FROM devices,
(
SELECT all_latest.*
FROM (SELECT codename, version, android, branch, method, filename, size, md5, link, changelog, date
from updates
WHERE codename like 'whyred%'
AND (updates.branch like "Stable%" OR updates.branch = "Weekly" OR updates.branch = "Public Beta")
AND updates.type = "Full"
ORDER BY updates.date DESC
LIMIT 99999) as all_latest
GROUP BY all_latest.codename, all_latest.method, all_latest.branch) as latest
WHERE devices.codename = latest.codename
AND (LENGTH(miui_code) = 4 or miui_code like '%RF' or miui_code like '%FK')
"""
all_latest = session.query(
Update.codename, Update.version, Update.android, Update.branch,
Update.method, Update.filename, Update.size, Update.md5, Update.link, Update.changelog, Update.date).filter(
Update.codename.startswith(codename)).filter(
or_(Update.branch.startswith("Stable"), Update.branch == "Weekly", Update.branch == "Public Beta")).filter(
Update.type == "Full").order_by(Update.date.desc()).limit(99999).subquery()
latest = session.query(all_latest).group_by(all_latest.c.codename).group_by(
all_latest.c.method).group_by(all_latest.c.branch).subquery()
updates = session.query(concat(Device.name, ' ', Device.region).label('name'), latest).filter(
Device.codename == latest.c.codename).filter(
or_(func.length(Device.miui_code) == 4, Device.miui_code.endswith("RF"),
Device.miui_code.endswith("FK"))).all()
return updates
def get_device_roms(codename) -> result:
"""
SELECT CONCAT(devices.name, ' ', devices.region) as name, all_updates.*
FROM devices,
(
SELECT codename, version, android, branch, method, filename, size, md5, link, changelog, date
from updates
WHERE codename like 'cmi%'
AND (updates.branch like "Stable%" OR updates.branch = "Weekly" OR updates.branch = "Public Beta")
AND updates.type = "Full"
ORDER BY updates.date DESC
LIMIT 99999) as all_updates
WHERE devices.codename = all_updates.codename
AND (LENGTH(miui_code) = 4 or miui_code like '%RF' or miui_code like '%FK')
"""
all_updates = session.query(
Update.codename, Update.version, Update.android, Update.branch, Update.method, Update.filename, Update.size,
Update.md5, Update.link, Update.changelog, Update.date).filter(
Update.codename.startswith(codename)).filter(
or_(Update.branch.startswith("Stable"), Update.branch == "Weekly", Update.branch == "Public Beta")).filter(
Update.type == "Full").order_by(Update.date.desc()).limit(99999).subquery()
updates = session.query(concat(Device.name, ' ', Device.region).label('name'), all_updates).filter(
Device.codename == all_updates.c.codename).filter(
or_(func.length(Device.miui_code) == 4, Device.miui_code.endswith("RF"),
Device.miui_code.endswith("FK"))).all()
return updates
def get_codename(miui_name: str) -> result:
if miui_name.endswith("PRE"):
miui_name = miui_name.replace("PRE", "")
codename = session.query(Device).filter(Device.miui_name == miui_name).first()
return codename.codename if codename else None
def get_codename_from_miui_code(miui_code) -> result:
codename = session.query(Device).filter(Device.miui_code == miui_code).first()
return codename.codename if codename else None
def get_full_name(codename: str) -> Optional[str]:
full_name = session.query(concat(Device.name, ' ', Device.region)).filter(Device.codename == codename).first()
return full_name[0] if full_name else None
def get_device_name(codename: str) -> Optional[str]:
name = session.query(Device.name).filter(Device.codename == codename).first()
return name[0] if name else None
def get_device_info(codename: str) -> result:
return session.query(Device).filter(Device.codename == codename).first()
def get_incremental(version: str) -> Update:
"""
Get incremental update information of a version
:type version: str
:param version: Xiaomi software version
"""
return session.query(Update).filter(
Update.version == version).filter(Update.type == "Incremental").first()
def get_version(codename: str, branch: str) -> str:
"""
Get device version example
:param codename: device codename
:type codename: str
:param branch: Update branch
"""
query = session.query(Update).filter(Update.codename == codename).filter(Update.branch == branch).first()
return query.version if query else None
def add_to_db(update: Union[Update, Device], exists=False):
"""Adds an update to the database"""
if not exists:
session.add(update)
session.commit()
def update_in_db(filename) -> bool:
"""
Check if an update is already in the database
:param filename: Update file name
:return: True if the update is already in the database
"""
return bool(session.query(Update).filter_by(filename=filename).count() >= 1)
def device_in_db(codename) -> bool:
"""
Check if a device is already in the database
:param codename: Device codename
:return: True if the device is already in the database
"""
return bool(session.query(Device).filter_by(codename=codename).first())
def get_update(filename) -> Update:
"""
Get an update from the database
:param filename: update filename
:return: update object
"""
return session.query(Update).filter(Update.filename == filename).first()
def get_update_by_version(version, method: str = "Recovery") -> Update:
"""
Get a recovery update from the database
:param method: Recovery/Fastboot
:param version: update version
:return: update object
"""
return session.query(Update).filter(Update.version == version).filter(
Update.method == method).filter(Update.type == "Full").first()
def device_in_db(codename) -> bool:
"""
Check if a device is already in the database
:param codename: Device codename
:return: True if the update is already in the database
"""
return bool(session.query(Device).filter_by(codename=codename).count() >= 1)
def update_stable_beta(recovery_update: Update):
"""
recovery_update: Update object
"""
if recovery_update:
if recovery_update.branch == "Stable Beta":
recovery_update.branch = "Stable"
commit_changes()
def commit_changes():
"""
commit database changes
"""
session.commit()