1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-01 11:58:28 +00:00
trezor-firmware/core/tools/build_mocks
matejcik 482b4569f5 build: fix and auto-generate mock files
Squashed commit of the following:

commit 84d3486f59bda063f06521c8b695ea4b07781ec6
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 17 14:17:15 2019 +0200

    mocks: complete

commit d538133a6d0fb4af06c7c81f80b8675869fb5908
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 17 14:12:26 2019 +0200

    mocks part 3

commit 9f0b868d41dafaf487df6fc844db7f3368eabe1b
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 17 14:09:20 2019 +0200

    mocks: update generated mocks

commit 5d80c18a7824ed16fc11cde4cdb8ebca7ed33400
Author: matejcik <ja@matejcik.cz>
Date:   Thu May 16 15:49:40 2019 +0200

    mocks wip 2

commit 4b576eb796136a61eb88cb0d281fa4e21eadada8
Author: matejcik <ja@matejcik.cz>
Date:   Tue May 7 17:02:51 2019 +0200

    WIP mocks part 1

commit cf3f0d4471ab74b478d2970b0bb178feae7c86a3
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 3 17:07:53 2019 +0200

    core: add package to secp256k1_zkp for mocking

commit 8a12f26c8c0d99363c8df96012426abbbb3ff6cb
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 3 17:04:05 2019 +0200

    core: blackify extmod docstring quotes

commit b6f239676dde8b60b001fcae4e5de80a71dbacf2
Author: matejcik <ja@matejcik.cz>
Date:   Fri May 3 16:52:27 2019 +0200

    core: make build_mocks directory agnostic

mocks: detect bad packages

mocks: revert noqa in favor of setup.cfg

mocks: fix broken comment formatting
2019-05-17 15:45:47 +02:00

136 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
import glob
import os
import re
import shutil
import subprocess
import sys
import tempfile
CORE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
EXTMOD_PATH = os.path.join(CORE_DIR, "embed", "extmod")
MOCKS_PATH = os.path.join(CORE_DIR, "mocks", "generated")
COMMENT_PREFIX = "/// "
current_indent = 0
current_class = None
current_method = None
current_package = None
def split_to_parts(line, mod_desc=None):
global current_indent
global current_class
global current_method
global current_package
if line.startswith("package: "):
current_package = line[9:].strip()
return
if line.startswith("mock:global"):
current_indent = 0
current_class = None
return
if line.startswith("class "):
current_class = line[6:].split("(")[0].strip(":")
current_indent = 0
yield (current_package, "\n\n")
yield (current_package, "# " + mod_desc + "\n")
elif line.startswith("def "):
current_method = line[4:].split("(")[0]
if current_class is None:
yield (current_package, "\n\n")
yield (current_package, "# " + mod_desc + "\n")
else:
yield (current_package, "\n")
current_indent = 4
line = current_indent * " " + line
yield (current_package, line)
def store_to_file(dest, parts):
for package, line in parts:
package = package.replace(".", "/")
dirpath = os.path.join(dest, os.path.dirname(package))
filename = os.path.basename(package) + ".py"
filepath = os.path.join(dirpath, filename)
os.makedirs(dirpath, exist_ok=True)
if os.path.isdir(os.path.join(dest, package)):
if not line.strip():
continue
print("Package exists: {}".format(package))
print("You should set 'package:' in {}".format(line.strip()))
sys.exit(1)
if not os.path.exists(filepath):
with open(filepath, "a") as f:
f.write("from typing import *\n")
with open(filepath, "a") as f:
f.write(line)
def build_module(mod_file, dest):
global current_indent
global current_class
global current_package
filename = os.path.basename(mod_file)
assert filename.startswith("mod")
assert filename.endswith(".c") or filename.endswith(".h")
# modfoobar-xyz.h -> foobar-xyz
name = filename[3:-2]
current_indent = 0
current_class = None
current_package = name.split("-")[0]
mod_desc = re.sub(r"^.*/embed/", "", mod_file)
for l in open(mod_file):
if not l.startswith(COMMENT_PREFIX):
continue
l = l[len(COMMENT_PREFIX) :] # .strip()
store_to_file(dest, split_to_parts(l, mod_desc))
def build_directory(src, dest):
for modfile in sorted(glob.glob(os.path.join(src, "**", "mod*.[ch]"))):
build_module(modfile, dest)
def do_check():
with tempfile.TemporaryDirectory() as tmpdir:
build_directory(EXTMOD_PATH, tmpdir)
diff_out = subprocess.run(
["diff", "-ur", MOCKS_PATH, tmpdir],
stdout=subprocess.PIPE,
universal_newlines=True,
).stdout
if diff_out.strip():
print(diff_out, end="")
sys.exit(1)
def do_generate():
shutil.rmtree(MOCKS_PATH)
build_directory(EXTMOD_PATH, MOCKS_PATH)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--check":
do_check()
else:
do_generate()