diff --git a/core/Makefile b/core/Makefile index fbc28565db..580c5b3934 100644 --- a/core/Makefile +++ b/core/Makefile @@ -62,8 +62,7 @@ help: ## show this help vendor: ## update git submodules git submodule update --init --recursive --force -res: ## update resources - ./tools/res_collect +res: templates ## update resources ## emulator commands: diff --git a/core/src/trezor/res/__init__.py b/core/src/trezor/res/__init__.py index ff3fd67829..bc321d2f04 100644 --- a/core/src/trezor/res/__init__.py +++ b/core/src/trezor/res/__init__.py @@ -1,14 +1,14 @@ try: - from .resources import resdata + from .resources import load_resource except ImportError: - resdata = {} + raise RuntimeError("Please regenerate resources via 'make res'") def load(name: str) -> bytes: """ Loads resource of a given name as bytes. """ - return resdata[name] + return load_resource(name) def gettext(message: str) -> str: diff --git a/core/src/trezor/res/resources.py.mako b/core/src/trezor/res/resources.py.mako new file mode 100644 index 0000000000..a180cdeec0 --- /dev/null +++ b/core/src/trezor/res/resources.py.mako @@ -0,0 +1,28 @@ +# generated from resources.py.mako +# do not edit manually! +# flake8: noqa +# fmt: off +<% +from pathlib import Path +from itertools import chain + +THIS = Path(local.filename).resolve() +SRCDIR = THIS.parent.parent.parent + +PATTERNS = ( + "trezor/res/**/*.toif", + "apps/*/res/**/*.toif", +) + +resfiles = chain.from_iterable(SRCDIR.glob(p) for p in PATTERNS) +%>\ + +def load_resource(name: str) -> bytes: + if False: + raise RuntimeError +% for resfile in resfiles: + elif name == "${resfile.relative_to(SRCDIR)}": + return ${repr(resfile.read_bytes())} +% endfor + else: + return bytes() diff --git a/core/tools/build_icons.py b/core/tools/build_icons.py index d0a33a855d..8a05c11ae1 100755 --- a/core/tools/build_icons.py +++ b/core/tools/build_icons.py @@ -8,7 +8,7 @@ from PIL import Image from trezorlib._internal import toif -HERE = Path(__file__).parent +HERE = Path(__file__).parent.resolve() ROOT = HERE.parent.parent ICON_SIZE = (64, 64) diff --git a/core/tools/build_templates b/core/tools/build_templates index 378c911088..714b17baad 100755 --- a/core/tools/build_templates +++ b/core/tools/build_templates @@ -9,6 +9,8 @@ FIND_TEMPLATES="find $CWD/../src -name *.mako" check_results() { CHECK_FAIL=0 for filename in $($FIND_TEMPLATES); do + # ignore resources.py + if echo $filename | grep -q "resources.py.mako$"; then continue; fi TMP=`mktemp` TARGET="${filename%%.mako}" $RENDER "$filename" -o $TMP diff --git a/core/tools/res_collect b/core/tools/res_collect deleted file mode 100755 index 7b3397460f..0000000000 --- a/core/tools/res_collect +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python3 -import os -import io - -resources = {} -resources_size = 0 - -os.chdir(os.path.dirname(__file__)) -os.chdir("../src/") - - -def process_file(name): - if name.endswith(".gitignore"): - return - if name.endswith(".py"): - return - if os.path.basename(name).startswith("."): - return - with open(name, "rb") as f: - data = f.read() - resources[name] = data - print("processing file %s (%d bytes)" % (name, len(data))) - 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) - - -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) - -resfile = "trezor/res/resources.py" - -bio = io.StringIO() -bio.write("# fmt: off\n") -bio.write("resdata = {\n") -for k in sorted(resources.keys()): - bio.write(" '%s': %s,\n" % (k, resources[k])) -bio.write("}\n") - -try: - with open(resfile, "r") as f: - stale = f.read() -except FileNotFoundError: - stale = None - -fresh = bio.getvalue() - -if stale != fresh: - with open(resfile, "wt") as f: - f.write(fresh) - print( - "written %s with %d entries (total %d bytes)" - % (resfile, len(resources), resources_size) - ) -else: - print("continuing with %s, no changes detected" % (resfile))