mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 04:18:10 +00:00
build(core): rewrite build_mocks with pathlib, add symlinks to ../mocks
This commit is contained in:
parent
bda1fa9912
commit
1bfaec10e9
1
core/mocks/generated/gc.pyi
Symbolic link
1
core/mocks/generated/gc.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../gc.pyi
|
1
core/mocks/generated/micropython.pyi
Symbolic link
1
core/mocks/generated/micropython.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../micropython.pyi
|
1
core/mocks/generated/ubinascii.pyi
Symbolic link
1
core/mocks/generated/ubinascii.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../ubinascii.pyi
|
1
core/mocks/generated/uctypes.pyi
Symbolic link
1
core/mocks/generated/uctypes.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../uctypes.pyi
|
1
core/mocks/generated/uio.pyi
Symbolic link
1
core/mocks/generated/uio.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../uio.pyi
|
1
core/mocks/generated/uos.pyi
Symbolic link
1
core/mocks/generated/uos.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../uos.pyi
|
1
core/mocks/generated/ustruct.pyi
Symbolic link
1
core/mocks/generated/ustruct.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../ustruct.pyi
|
1
core/mocks/generated/utime.pyi
Symbolic link
1
core/mocks/generated/utime.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../utime.pyi
|
1
core/mocks/generated/utimeq.pyi
Symbolic link
1
core/mocks/generated/utimeq.pyi
Symbolic link
@ -0,0 +1 @@
|
||||
../utimeq.pyi
|
@ -6,10 +6,11 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
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")
|
||||
CORE_DIR = Path(__file__).parent.parent.resolve()
|
||||
EXTMOD_PATH = CORE_DIR / "embed" / "extmod"
|
||||
MOCKS_PATH = CORE_DIR / "mocks" / "generated"
|
||||
|
||||
COMMENT_PREFIX = "/// "
|
||||
|
||||
@ -59,22 +60,21 @@ def split_to_parts(line, mod_desc=None):
|
||||
def store_to_file(dest, parts):
|
||||
for package, line in parts:
|
||||
package = package.replace(".", "/")
|
||||
dirpath = os.path.join(dest, os.path.dirname(package))
|
||||
dirpath = dest / os.path.dirname(package)
|
||||
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():
|
||||
continue
|
||||
print(f"Package exists: {package}")
|
||||
print(f"You should set 'package:' in {line.strip()}")
|
||||
sys.exit(1)
|
||||
|
||||
if not os.path.exists(filepath):
|
||||
with open(filepath, "a") as f:
|
||||
f.write("from typing import *\n")
|
||||
if not filepath.exists():
|
||||
filepath.write_text("from typing import *\n")
|
||||
|
||||
with open(filepath, "a") as f:
|
||||
f.write(line)
|
||||
@ -85,16 +85,15 @@ def build_module(mod_file, dest):
|
||||
global current_class
|
||||
global current_package
|
||||
|
||||
filename = os.path.basename(mod_file)
|
||||
assert filename.startswith("mod")
|
||||
assert filename.endswith(".c") or filename.endswith(".h")
|
||||
assert mod_file.name.startswith("mod")
|
||||
assert mod_file.suffix in (".c", ".h")
|
||||
# modfoobar-xyz.h -> foobar-xyz
|
||||
name = filename[3:-2]
|
||||
name = mod_file.name[3:-2]
|
||||
|
||||
current_indent = 0
|
||||
current_class = None
|
||||
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):
|
||||
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))
|
||||
|
||||
|
||||
def build_directory(src, dest):
|
||||
for modfile in sorted(glob.glob(os.path.join(src, "**", "mod*.[ch]"))):
|
||||
def place_symlinks(dest):
|
||||
# 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)
|
||||
place_symlinks(dest)
|
||||
|
||||
|
||||
def do_check():
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
build_directory(EXTMOD_PATH, tmpdir)
|
||||
build_directory(Path(tmpdir))
|
||||
diff_out = subprocess.run(
|
||||
["diff", "-ur", MOCKS_PATH, tmpdir],
|
||||
["diff", "-ur", str(MOCKS_PATH), tmpdir],
|
||||
stdout=subprocess.PIPE,
|
||||
universal_newlines=True,
|
||||
).stdout
|
||||
@ -124,8 +131,7 @@ def do_check():
|
||||
|
||||
def do_generate():
|
||||
shutil.rmtree(MOCKS_PATH)
|
||||
build_directory(EXTMOD_PATH, MOCKS_PATH)
|
||||
|
||||
build_directory(MOCKS_PATH)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "--check":
|
||||
|
Loading…
Reference in New Issue
Block a user