1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 20:38:10 +00:00

build(core): rewrite build_mocks with pathlib, add symlinks to ../mocks

This commit is contained in:
matejcik 2021-11-18 12:30:27 +01:00 committed by matejcik
parent bda1fa9912
commit 1bfaec10e9
10 changed files with 36 additions and 21 deletions

1
core/mocks/generated/gc.pyi Symbolic link
View File

@ -0,0 +1 @@
../gc.pyi

View File

@ -0,0 +1 @@
../micropython.pyi

View File

@ -0,0 +1 @@
../ubinascii.pyi

View File

@ -0,0 +1 @@
../uctypes.pyi

View File

@ -0,0 +1 @@
../uio.pyi

View File

@ -0,0 +1 @@
../uos.pyi

View File

@ -0,0 +1 @@
../ustruct.pyi

View File

@ -0,0 +1 @@
../utime.pyi

View File

@ -0,0 +1 @@
../utimeq.pyi

View File

@ -6,10 +6,11 @@ import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
from pathlib import Path
CORE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) CORE_DIR = Path(__file__).parent.parent.resolve()
EXTMOD_PATH = os.path.join(CORE_DIR, "embed", "extmod") EXTMOD_PATH = CORE_DIR / "embed" / "extmod"
MOCKS_PATH = os.path.join(CORE_DIR, "mocks", "generated") MOCKS_PATH = CORE_DIR / "mocks" / "generated"
COMMENT_PREFIX = "/// " COMMENT_PREFIX = "/// "
@ -59,22 +60,21 @@ def split_to_parts(line, mod_desc=None):
def store_to_file(dest, parts): def store_to_file(dest, parts):
for package, line in parts: for package, line in parts:
package = package.replace(".", "/") package = package.replace(".", "/")
dirpath = os.path.join(dest, os.path.dirname(package)) dirpath = dest / os.path.dirname(package)
filename = os.path.basename(package) + ".pyi" filename = os.path.basename(package) + ".pyi"
filepath = os.path.join(dirpath, filename) filepath = dirpath / filename
os.makedirs(dirpath, exist_ok=True) dirpath.mkdir(parents=True, exist_ok=True)
if os.path.isdir(os.path.join(dest, package)): if (dest / package).is_dir():
if not line.strip(): if not line.strip():
continue continue
print(f"Package exists: {package}") print(f"Package exists: {package}")
print(f"You should set 'package:' in {line.strip()}") print(f"You should set 'package:' in {line.strip()}")
sys.exit(1) sys.exit(1)
if not os.path.exists(filepath): if not filepath.exists():
with open(filepath, "a") as f: filepath.write_text("from typing import *\n")
f.write("from typing import *\n")
with open(filepath, "a") as f: with open(filepath, "a") as f:
f.write(line) f.write(line)
@ -85,16 +85,15 @@ def build_module(mod_file, dest):
global current_class global current_class
global current_package global current_package
filename = os.path.basename(mod_file) assert mod_file.name.startswith("mod")
assert filename.startswith("mod") assert mod_file.suffix in (".c", ".h")
assert filename.endswith(".c") or filename.endswith(".h")
# modfoobar-xyz.h -> foobar-xyz # modfoobar-xyz.h -> foobar-xyz
name = filename[3:-2] name = mod_file.name[3:-2]
current_indent = 0 current_indent = 0
current_class = None current_class = None
current_package = name.split("-")[0] current_package = name.split("-")[0]
mod_desc = re.sub(r"^.*/embed/", "", mod_file) mod_desc = str(mod_file.relative_to(CORE_DIR / "embed"))
for l in open(mod_file): for l in open(mod_file):
if not l.startswith(COMMENT_PREFIX): if not l.startswith(COMMENT_PREFIX):
@ -104,16 +103,24 @@ def build_module(mod_file, dest):
store_to_file(dest, split_to_parts(l, mod_desc)) store_to_file(dest, split_to_parts(l, mod_desc))
def build_directory(src, dest): def place_symlinks(dest):
for modfile in sorted(glob.glob(os.path.join(src, "**", "mod*.[ch]"))): # make symlinks for the non-generated files
for pyi in MOCKS_PATH.glob("../*.pyi"):
dest_file = dest / pyi.name
dest_file.symlink_to(os.path.relpath(pyi.resolve(), dest))
def build_directory(dest):
for modfile in sorted(EXTMOD_PATH.glob("**/mod*.[ch]")):
build_module(modfile, dest) build_module(modfile, dest)
place_symlinks(dest)
def do_check(): def do_check():
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
build_directory(EXTMOD_PATH, tmpdir) build_directory(Path(tmpdir))
diff_out = subprocess.run( diff_out = subprocess.run(
["diff", "-ur", MOCKS_PATH, tmpdir], ["diff", "-ur", str(MOCKS_PATH), tmpdir],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
).stdout ).stdout
@ -124,8 +131,7 @@ def do_check():
def do_generate(): def do_generate():
shutil.rmtree(MOCKS_PATH) shutil.rmtree(MOCKS_PATH)
build_directory(EXTMOD_PATH, MOCKS_PATH) build_directory(MOCKS_PATH)
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--check": if len(sys.argv) > 1 and sys.argv[1] == "--check":