Skip to content

Commit

Permalink
feat(app): expose compile, module-defaults flags, integrity in stubs …
Browse files Browse the repository at this point in the history
…create command.

Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
  • Loading branch information
BradenM committed Apr 17, 2023
1 parent f0bba8d commit 885e9d3
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions micropy/app/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def __new__(cls, value: str, backend: Type[MetaPyDeviceBackend]):
return obj


def create_changeset(value: Optional[List[str]]) -> Optional[ListChangeSet]:
def create_changeset(
value: Optional[List[str]], *, replace: bool = False
) -> Optional[ListChangeSet]:
if value is None:
return value
return ListChangeSet.from_strings(add=value)
return ListChangeSet.from_strings(add=value, replace=replace)


@stubs_app.command(name="create")
Expand All @@ -83,17 +85,27 @@ def stubs_create(
module: Optional[List[str]] = typer.Option(
None, "-m", "--module", help="Modules to add.", rich_help_panel="Stubs"
),
module_defaults: bool = typer.Option(
True, help="Include createstubs default modules.", rich_help_panel="Stubs"
),
exclude: Optional[List[str]] = typer.Option(
None, "-e", "--exclude", help="Modules to exclude.", rich_help_panel="Stubs"
),
compile: bool = typer.Option(
True,
"-c",
"--compile",
help="Cross compile to .mpy via mpy-cross.",
rich_help_panel="Stubs",
),
):
"""Create stubs from micropython-enabled devices.
Utilize Josverl's [micropython-stubber](https://github.com/josverl/micropython-stubber/)
to generate stubs from your own micropython-enabled device.
"""
mp: MicroPy = ctx.find_object(MicroPy)
mp: MicroPy = ctx.ensure_object(MicroPy)
log = mp.log
log.title(f"Connecting to Pyboard @ $[{port}]")
pyb_log = Log.add_logger("Pyboard", "bright_white")
Expand All @@ -118,27 +130,40 @@ def _get_desc(name: str, cfg: dict):
return None

log.success("Connected!")
if module or exclude:
log.title("Preparing createstubs for:")
log.info(f"Modules: {', '.join(module or [])}")
log.info(f"Exclude: {', '.join(exclude or [])}")
create_stubs = prepare_create_stubs(
variant=variant,
modules_set=create_changeset(module),
modules_set=create_changeset(module, replace=not module_defaults),
exclude_set=create_changeset(exclude),
compile=compile,
)
dev_path = DevicePath("createstubs.mpy") if compile else DevicePath("createstubs.py")
log.info("Executing stubber on pyboard...")
try:
pyb.run_script(create_stubs.getvalue(), DevicePath("createstubs.py"))
pyb.run_script(create_stubs, DevicePath(dev_path))
except Exception as e:
# TODO: Handle more usage cases
log.error(f"Failed to execute script: {str(e)}", exception=e)
raise
log.success("Done!")
log.info("Copying stubs...")
with tempfile.TemporaryDirectory() as tmpdir:
pyb.copy_from(DevicePath("/stubs"), tmpdir)
pyb.copy_from(
DevicePath("/stubs"),
tmpdir,
verify_integrity=True,
# exclude due to ps1 var possibly different.
exclude_integrity={"sys.py", "usys.py"},
)
out_dir = Path(tmpdir)
stub_path = next(out_dir.iterdir())
log.info(f"Copied Stubs: $[{stub_path.name}]")
stub_path = mp.stubs.from_stubber(stub_path, out_dir)
stub = mp.stubs.add(str(stub_path))
pyb.remove(dev_path)
pyb.disconnect()
log.success(f"Added {stub.name} to stubs!")
return stub
Expand Down

0 comments on commit 885e9d3

Please sign in to comment.