forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.py
executable file
·405 lines (318 loc) · 13 KB
/
support.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
from __future__ import annotations
import json
import os
import re
import sys
import click
import coin_info
SUPPORT_INFO = coin_info.get_support_data()
VERSION_RE = re.compile(r"\d+.\d+.\d+")
ERC20_DUPLICATE_KEY = "(AUTO) duplicate key"
def write_support_info():
with open(os.path.join(coin_info.DEFS_DIR, "support.json"), "w") as f:
json.dump(SUPPORT_INFO, f, indent=2, sort_keys=True)
f.write("\n")
def support_dicts(device):
return SUPPORT_INFO[device]["supported"], SUPPORT_INFO[device]["unsupported"]
def all_support_dicts():
for device in SUPPORT_INFO:
yield (device, *support_dicts(device))
def clear_support(device, key):
supported, unsupported = support_dicts(device)
supported.pop(key, None)
unsupported.pop(key, None)
def support_setdefault(device, key, value, reason=None):
"""Set value only if no other value is set"""
supported, unsupported = support_dicts(device)
if value is not False and key not in unsupported:
supported.setdefault(key, value)
if value is False:
if reason is None:
raise ValueError("reason must be given for unsupported keys")
if key not in supported:
unsupported[key] = reason
def set_supported(device, key, value):
clear_support(device, key)
supported, _ = support_dicts(device)
supported[key] = value
def set_unsupported(device, key, value):
clear_support(device, key)
_, unsupported = support_dicts(device)
unsupported[key] = value
def print_support(coin):
def support_value(where, key, missing_means_no=False):
if "supported" in where and key in where["supported"]:
val = where["supported"][key]
if val is True:
return "YES"
elif VERSION_RE.match(val):
return f"YES since {val}"
else:
return f"BAD VALUE {val}"
elif "unsupported" in where and key in where["unsupported"]:
val = where["unsupported"][key]
return f"NO (reason: {val})"
elif missing_means_no:
return "NO"
else:
return "support info missing"
key, name, shortcut = coin["key"], coin["name"], coin["shortcut"]
print(f"{key} - {name} ({shortcut})")
if coin.get("duplicate"):
print(" * DUPLICATE SYMBOL")
for dev, where in SUPPORT_INFO.items():
missing_means_no = dev in coin_info.MISSING_SUPPORT_MEANS_NO
print(" *", dev, ":", support_value(where, key, missing_means_no))
# ====== validation functions ====== #
def check_support_values():
def _check_value_version_soon(value):
if not isinstance(value, str):
raise ValueError(f"non-str value: {value}")
if not VERSION_RE.match(value):
raise ValueError(f"expected version, found '{value}'")
errors = []
for device, values in SUPPORT_INFO.items():
supported = values.get("supported")
unsupported = values.get("unsupported")
if not isinstance(supported, dict):
errors.append(f"Missing 'supported' dict for {device}")
else:
for key, value in supported.items():
try:
if device in coin_info.VERSIONED_SUPPORT_INFO:
_check_value_version_soon(value)
else:
if value is not True:
raise ValueError(f"only allowed is True, but found {value}")
if key in unsupported:
raise ValueError(f"{key} is both supported and unsupported")
except Exception as e:
errors.append(f"{device}.supported.{key}: {e}")
if not isinstance(unsupported, dict):
errors.append(f"Missing 'supported' dict for {device}")
else:
for key, value in unsupported.items():
if not isinstance(value, str) or not value:
errors.append(f"{device}.unsupported.{key}: missing reason")
return errors
def find_unsupported_coins(coins_dict):
result = {}
for device in coin_info.VERSIONED_SUPPORT_INFO:
supported, unsupported = support_dicts(device)
support_set = set(supported.keys())
support_set.update(unsupported.keys())
result[device] = []
for key, coin in coins_dict.items():
if key not in support_set:
result[device].append(coin)
return result
def find_orphaned_support_keys(coins_dict):
orphans = set()
for _, supported, unsupported in all_support_dicts():
orphans.update(key for key in supported if key not in coins_dict)
orphans.update(key for key in unsupported if key not in coins_dict)
return orphans
@click.group()
def cli():
pass
@cli.command()
@click.option("-n", "--dry-run", is_flag=True, help="Do not write changes")
def fix(dry_run):
"""Fix expected problems.
Currently only prunes orphaned keys.
"""
all_coins = coin_info.coin_info()
coins_dict = all_coins.as_dict()
orphaned = find_orphaned_support_keys(coins_dict)
for orphan in orphaned:
print(f"pruning orphan {orphan}")
for device in SUPPORT_INFO:
clear_support(device, orphan)
if not dry_run:
write_support_info()
@cli.command()
# fmt: off
@click.option("-m", "--ignore-missing", is_flag=True, help="Do not fail on missing supportinfo")
# fmt: on
def check(ignore_missing):
"""Check validity of support information.
Ensures that `support.json` data is well formed, there are no keys without
corresponding coins, and there are no coins without corresponding keys.
If `--ignore-missing` is specified, the check will display coins with missing
support info, but will not fail when missing coins are found. This is
useful in Travis.
"""
all_coins = coin_info.coin_info()
coins_dict = all_coins.as_dict()
checks_ok = True
errors = check_support_values()
if errors:
for error in errors:
print(error)
checks_ok = False
orphaned = find_orphaned_support_keys(coins_dict)
for orphan in orphaned:
print(f"orphaned key {orphan}")
checks_ok = False
missing = find_unsupported_coins(coins_dict)
for device, values in missing.items():
if values:
if not ignore_missing:
checks_ok = False
print(f"Device {device} has missing support infos:")
for coin in values:
print(f"{coin['key']} - {coin['name']}")
if not checks_ok:
print("Some checks have failed")
sys.exit(1)
@cli.command()
# fmt: off
@click.option("-r", '--releases', multiple=True, type=str, help='Key-value pairs of model and version. E.g. "T2B1=2.6.1"')
@click.option("-n", "--dry-run", is_flag=True, help="Do not write changes")
@click.option("-f", "--force", is_flag=True, help="Proceed even with bad version/device info")
@click.option("--skip-testnets/--no-skip-testnets", default=True, help="Automatically exclude testnets")
# fmt: on
@click.pass_context
def release(
ctx,
releases: list[str],
dry_run: bool,
force: bool,
skip_testnets: bool,
):
"""Release a new Trezor firmware.
Update support infos so that all coins have a clear support status.
By default, marks duplicate tokens and testnets as unsupported, and all coins that
don't have support info are set to the released firmware version.
The tool will ask you to confirm each added coin.
"""
# Transforming the user release input into a dict and validating
user_releases_dict = {
key: val for key, val in (release.split("=") for release in releases)
}
for key in user_releases_dict:
if key not in coin_info.VERSIONED_SUPPORT_INFO:
raise click.ClickException(
f"Unknown device: {key} - allowed are: {coin_info.VERSIONED_SUPPORT_INFO}"
)
def bump_version(version_tuple: tuple[int]) -> str:
version_list = list(version_tuple)
version_list[-1] += 1
return ".".join(str(n) for n in version_list)
latest_releases = coin_info.latest_releases()
# Take version either from user or guess it from latest releases info
device_release_version: dict[str, str] = {}
for device in coin_info.VERSIONED_SUPPORT_INFO:
if device in user_releases_dict:
device_release_version[device] = user_releases_dict[device]
else:
device_release_version[device] = bump_version(latest_releases[device])
for device, version in device_release_version.items():
version_starting_num = device[1] # "T1B1" -> "1", "T2B1" -> "2"
if not force and not version.startswith(version_starting_num + "."):
raise click.ClickException(
f"Device {device} should not be version {version}. "
"Use --force to proceed anyway."
)
print(f"Releasing {device} firmware version {version}")
defs, _ = coin_info.coin_info_with_duplicates()
coins_dict = defs.as_dict()
# Invoke data fixup as dry-run. That will modify data internally but won't write
# changes. We will write changes at the end based on our own `dry_run` value.
print("Fixing up data...")
ctx.invoke(fix, dry_run=True)
def maybe_add(coin):
add = click.confirm(
f"Add missing coin {coin['key']} ({coin['name']})?", default=True
)
if not add:
unsupport_reason = click.prompt(
"Enter reason for not supporting (blank to skip)",
default="",
show_default=False,
)
if not unsupport_reason:
return
for device, version in device_release_version.items():
if add:
support_setdefault(device, coin["key"], version)
else:
support_setdefault(device, coin["key"], False, unsupport_reason)
# process missing (not listed) supportinfos
missing_list = []
unsupported = find_unsupported_coins(coins_dict)
for val in unsupported.values():
for coin in val:
if coin not in missing_list:
missing_list.append(coin)
for coin in missing_list:
if skip_testnets and coin["is_testnet"]:
for device, version in device_release_version.items():
support_setdefault(device, coin["key"], False, "(AUTO) exclude testnet")
else:
maybe_add(coin)
if not dry_run:
write_support_info()
else:
print("No changes written")
@cli.command()
@click.argument("keyword", nargs=-1, required=True)
def show(keyword):
"""Show support status of specified coins.
Keywords match against key, name or shortcut (ticker symbol) of coin.
"""
defs, _ = coin_info.coin_info_with_duplicates()
for kw in keyword:
for coin in coin_info.search(defs, kw):
print_support(coin)
@cli.command(name="set")
# fmt: off
@click.argument("key", required=True)
@click.argument("entries", nargs=-1, required=True, metavar="entry=value [entry=value]...")
@click.option("-r", "--reason", help="Reason for not supporting")
# fmt: on
def set_support_value(key, entries, reason):
"""Set a support info variable.
Examples:
support.py set coin:BTC T1B1=1.10.5 T2T1=2.4.7 suite=yes connect=no
support.py set coin:LTC T1B1=yes connect=
Setting a variable to "yes", "true" or "1" sets support to true.
Setting a variable to "no", "false" or "0" sets support to false.
(or null, in case of T1B1/T2T1)
Setting variable to empty ("T1B1=") will set to null, or clear the entry.
Setting a variable to a particular version string (e.g., "2.4.7") will set that
particular version.
"""
defs, _ = coin_info.coin_info_with_duplicates()
coins = defs.as_dict()
if key not in coins:
click.echo(f"Failed to find key {key}")
click.echo("Use 'support.py show' to search for the right one.")
sys.exit(1)
for entry in entries:
try:
device, value = entry.split("=", maxsplit=1)
except ValueError:
click.echo(f"Invalid entry: {entry}")
sys.exit(2)
if device not in SUPPORT_INFO:
raise click.ClickException(f"unknown device: {device}")
if value in ("yes", "true", "1"):
set_supported(device, key, True)
elif value in ("no", "false", "0"):
if device in coin_info.MISSING_SUPPORT_MEANS_NO:
click.echo(f"Setting explicitly unsupported for {device}.")
click.echo(f"Perhaps you meant removing support, i.e., '{device}=' ?")
if not reason:
reason = click.prompt(f"Enter reason for not supporting on {device}:")
set_unsupported(device, key, reason)
elif value == "":
clear_support(device, key)
else:
# arbitrary string
set_supported(device, key, value)
print_support(coins[key])
write_support_info()
if __name__ == "__main__":
cli()