1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00
trezor-firmware/tools/res_collect

68 lines
1.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
import os
import io
resources = {}
2016-10-03 09:56:24 +00:00
resources_size = 0
os.chdir(os.path.dirname(__file__))
2018-07-31 09:35:09 +00:00
os.chdir("../src/")
def process_file(name):
2018-07-31 09:35:09 +00:00
if name.endswith(".gitignore"):
return
2018-07-31 09:35:09 +00:00
if name.endswith(".py"):
return
2018-07-31 09:35:09 +00:00
if os.path.basename(name).startswith("."):
return
2018-07-31 09:35:09 +00:00
with open(name, "rb") as f:
2016-10-03 09:56:24 +00:00
data = f.read()
resources[name] = data
2018-07-31 09:35:09 +00:00
print("processing file %s (%d bytes)" % (name, len(data)))
2016-10-03 09:56:24 +00:00
global resources_size
resources_size += len(data)
def process_dir_rec(dir):
for name in os.listdir(dir):
path = os.path.join(dir, name)
if os.path.isfile(path):
process_file(path)
elif os.path.isdir(path):
process_dir_rec(path)
2018-07-31 09:35:09 +00:00
process_dir_rec("trezor/res/")
for name in os.listdir("apps/"):
path = os.path.join("apps/", name, "res/")
if os.path.isdir(path):
process_dir_rec(path)
2018-07-31 09:35:09 +00:00
resfile = "trezor/res/resources.py"
bio = io.StringIO()
2018-07-31 09:35:09 +00:00
bio.write("# fmt: off\n")
bio.write("resdata = {\n")
for k in sorted(resources.keys()):
bio.write(" '%s': %s,\n" % (k, resources[k]))
2018-07-31 09:35:09 +00:00
bio.write("}\n")
try:
2018-07-31 09:35:09 +00:00
with open(resfile, "r") as f:
stale = f.read()
2018-07-31 09:35:09 +00:00
except FileNotFoundError:
stale = None
fresh = bio.getvalue()
if stale != fresh:
2018-07-31 09:35:09 +00:00
with open(resfile, "wt") as f:
f.write(fresh)
2018-07-31 09:35:09 +00:00
print(
"written %s with %d entries (total %d bytes)"
% (resfile, len(resources), resources_size)
)
else:
2018-07-31 09:35:09 +00:00
print("continuing with %s, no changes detected" % (resfile))