diff --git a/core/mocks/generated/gc.pyi b/core/mocks/generated/gc.pyi new file mode 120000 index 0000000000..c856cd4053 --- /dev/null +++ b/core/mocks/generated/gc.pyi @@ -0,0 +1 @@ +../gc.pyi \ No newline at end of file diff --git a/core/mocks/generated/micropython.pyi b/core/mocks/generated/micropython.pyi new file mode 120000 index 0000000000..efdb2d7d33 --- /dev/null +++ b/core/mocks/generated/micropython.pyi @@ -0,0 +1 @@ +../micropython.pyi \ No newline at end of file diff --git a/core/mocks/generated/ubinascii.pyi b/core/mocks/generated/ubinascii.pyi new file mode 120000 index 0000000000..b129e0cecd --- /dev/null +++ b/core/mocks/generated/ubinascii.pyi @@ -0,0 +1 @@ +../ubinascii.pyi \ No newline at end of file diff --git a/core/mocks/generated/uctypes.pyi b/core/mocks/generated/uctypes.pyi new file mode 120000 index 0000000000..f18c254154 --- /dev/null +++ b/core/mocks/generated/uctypes.pyi @@ -0,0 +1 @@ +../uctypes.pyi \ No newline at end of file diff --git a/core/mocks/generated/uio.pyi b/core/mocks/generated/uio.pyi new file mode 120000 index 0000000000..ac036f48a5 --- /dev/null +++ b/core/mocks/generated/uio.pyi @@ -0,0 +1 @@ +../uio.pyi \ No newline at end of file diff --git a/core/mocks/generated/uos.pyi b/core/mocks/generated/uos.pyi new file mode 120000 index 0000000000..c5cae7aaa7 --- /dev/null +++ b/core/mocks/generated/uos.pyi @@ -0,0 +1 @@ +../uos.pyi \ No newline at end of file diff --git a/core/mocks/generated/ustruct.pyi b/core/mocks/generated/ustruct.pyi new file mode 120000 index 0000000000..a3dbad3ef4 --- /dev/null +++ b/core/mocks/generated/ustruct.pyi @@ -0,0 +1 @@ +../ustruct.pyi \ No newline at end of file diff --git a/core/mocks/generated/utime.pyi b/core/mocks/generated/utime.pyi new file mode 120000 index 0000000000..08e0d69280 --- /dev/null +++ b/core/mocks/generated/utime.pyi @@ -0,0 +1 @@ +../utime.pyi \ No newline at end of file diff --git a/core/mocks/generated/utimeq.pyi b/core/mocks/generated/utimeq.pyi new file mode 120000 index 0000000000..248c18120a --- /dev/null +++ b/core/mocks/generated/utimeq.pyi @@ -0,0 +1 @@ +../utimeq.pyi \ No newline at end of file diff --git a/core/tools/build_mocks b/core/tools/build_mocks index 871958f659..3e7fd914a4 100755 --- a/core/tools/build_mocks +++ b/core/tools/build_mocks @@ -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":