Skip to content

Commit

Permalink
stubber: use list_mcu logic from mpflash
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Jul 2, 2024
1 parent 07a505b commit b35d028
Show file tree
Hide file tree
Showing 45 changed files with 355 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"python.analysis.autoSearchPaths": true,
"python.analysis.diagnosticMode": "workspace",
"python.analysis.diagnosticMode": "openFilesOnly",
// "python.analysis.logLevel": "Trace",
//////////////////////////////////////////////////////////////
"python.testing.pytestEnabled": true,
Expand Down
84 changes: 41 additions & 43 deletions src/stubber/bulk/mcu_stubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
from tempfile import mkdtemp
from typing import List, Optional, Tuple

from loguru import logger as log

from rich.console import Console
from rich.table import Table
from tenacity import retry, stop_after_attempt, wait_fixed

from mpflash.mpremoteboard import ERROR, OK, MPRemoteBoard
from mpflash.connected import list_mcus
from mpflash.list import show_mcus
from mpflash.logger import log
from stubber import utils
from stubber.publish.merge_docstubs import merge_all_docstubs
from stubber.publish.pathnames import board_folder_name
Expand Down Expand Up @@ -145,7 +148,9 @@ def run_createstubs(dest: Path, mcu: MPRemoteBoard, variant: Variant = Variant.d
mcu.run_command("reset", timeout=5)
time.sleep(2)

log.info(f"Running createstubs {variant.value} on {mcu.serialport} {mcu.description} using temp path: {dest}")
log.info(
f"Running createstubs {variant.value} on {mcu.serialport} {mcu.description} using temp path: {dest}"
)
cmd = build_cmd(dest, variant)
log.info(f"Running : mpremote {' '.join(cmd)}")
mcu.run_command.retry.wait = wait_fixed(15)
Expand All @@ -155,7 +160,13 @@ def run_createstubs(dest: Path, mcu: MPRemoteBoard, variant: Variant = Variant.d
timeout = 90 if mcu.port == "esp8266" else 6 * 60 # type: ignore
rc, out = mcu.run_command(cmd, timeout=timeout)
# check last line for exception or error and raise that if found
if rc != OK and out and ":" in out[-1] and not out[-1].startswith("INFO") and not out[-1].startswith("WARN"):
if (
rc != OK
and out
and ":" in out[-1]
and not out[-1].startswith("INFO")
and not out[-1].startswith("WARN")
):
log.warning(f"createstubs: {out[-1]}")
raise RuntimeError(out[-1]) from eval(out[-1].split(":")[0])

Expand Down Expand Up @@ -250,7 +261,10 @@ def copy_boardname_to_board(mcu: MPRemoteBoard):
None
"""
if mcu.board:
cmd = ["exec", f"with open('lib/boardname.py', 'w') as f: f.write('BOARDNAME=\"{mcu.board}\"')"]
cmd = [
"exec",
f"with open('lib/boardname.py', 'w') as f: f.write('BOARDNAME=\"{mcu.board}\"')",
]
log.info(f"Writing BOARDNAME='{mcu.board}' to boardname.py")
else:
cmd = ["rm", "boardname.py"]
Expand Down Expand Up @@ -285,31 +299,12 @@ def copy_scripts_to_board(mcu: MPRemoteBoard, variant: Variant, form: Form):

def get_stubfolder(out: List[str]):
return (
lines[-1].split("/remote/")[-1].strip() if (lines := [l for l in out if l.startswith("INFO : Path: ")]) else ""
lines[-1].split("/remote/")[-1].strip()
if (lines := [l for l in out if l.startswith("INFO : Path: ")])
else ""
)


def scan_boards(optimistic: bool = False) -> List[MPRemoteBoard]:
"""
This function scans for boards and returns a list of MPRemoteBoard objects.
:return: list of MPRemoteBoard objects
"""
boards = []
for mpr_port in MPRemoteBoard.connected_boards():
board = MPRemoteBoard(mpr_port)
log.debug(f"Attempt to connect to: {board.serialport}")
try:
board.get_mcu_info()
log.success(f"Detected board {board.description} {board.version}")
boards.append(board)
except Exception:
log.error(f"Failed to get mcu_info for {board.serialport}")
if optimistic:
boards.append(board)
continue
return boards


def set_loglevel(verbose: int) -> str:
"""Set log level based on verbose level
Get the level from the verbose setting (0=INFO, 1=DEBUG, 2=TRACE)
Expand All @@ -324,7 +319,9 @@ def set_loglevel(verbose: int) -> str:
else:
format_str = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>|<level>{level: <8}</level>|<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

log.add(sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str)
log.add(
sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str
)
# log.info(f"micropython-stubber {__version__}")
return level

Expand All @@ -347,7 +344,14 @@ def copy_to_repo(source: Path, fw: dict) -> Optional[Path]:
return None


def stub_connected_mcus(variant: str, format: str, debug: bool) -> int:
def stub_connected_mcus(
variant: str,
format: str,
debug: bool,
serial: List[str],
ignore: List[str],
bluetooth: bool,
) -> int:
"""
Runs the stubber to generate stubs for connected MicroPython boards.
Expand All @@ -372,24 +376,16 @@ def stub_connected_mcus(variant: str, format: str, debug: bool) -> int:
all_built = []

# scan boards and just work with the ones that respond with understandable data
connected_boards = scan_boards(True)
if not connected_boards:
connected_mcus = list_mcus(ignore=ignore, include=serial, bluetooth=bluetooth)

if not connected_mcus:
log.error("No micropython boards were found")
return ERROR

table = Table(show_header=True, header_style="bold magenta")
table.add_column("Serial Port")
table.add_column("Port")
table.add_column("Description")
table.add_column("Version")

for b in connected_boards:
table.add_row(b.serialport, b.port, b.description, b.version)
console = Console()
console.print(table)
show_mcus(connected_mcus, refresh=False)

# scan boards and generate stubs
for board in connected_boards:
for board in connected_mcus:
log.info(
f"Connecting using {board.serialport} to {board.port} {board.board} {board.version}: {board.description}"
)
Expand Down Expand Up @@ -428,14 +424,16 @@ def stub_connected_mcus(variant: str, format: str, debug: bool) -> int:
all_built.extend(built)

if all_built:
print_result_table(all_built, console)
print_result_table(all_built)
log.success("Done")
return OK
log.error(f"Failed to generate stubs for {board.serialport}")
return ERROR


def print_result_table(all_built: List, console: Console):
def print_result_table(all_built: List, console: Optional[Console] = None):
if not console:
console = Console()
# create a rich table of the results and print it'
table = Table(title="Results")

Expand Down
2 changes: 1 addition & 1 deletion src/stubber/codemod/enrich.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from libcst.codemod import CodemodContext, diff_code, exec_transform_with_prettyprint
from libcst.tool import _default_config # type: ignore
from loguru import logger as log
from mpflash.logger import log

import stubber.codemod.merge_docstub as merge_docstub
from stubber.utils.post import run_black
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/codemod/merge_docstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor, GatherImportsVisitor, ImportItem
from libcst.helpers.module import insert_header_comments
from loguru import logger as log
from mpflash.logger import log

from stubber.cst_transformer import (
MODULE_KEY,
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Union

import rich_click as click
from loguru import logger as log
from mpflash.logger import log
from tabulate import tabulate

from stubber.commands.cli import stubber_cli
Expand Down
16 changes: 5 additions & 11 deletions src/stubber/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import sys

from mpflash.vendor.click_aliases import ClickAliasedGroup
import rich_click as click
from loguru import logger as log
from mpflash.logger import log, set_loglevel as mpf_set_loglevel
from stubber import __version__


@click.group(chain=True)
@click.group(chain=True, cls=ClickAliasedGroup)
@click.version_option(package_name="micropython-stubber", prog_name="micropython-stubber✏️ ")
@click.option(
"-V",
Expand Down Expand Up @@ -41,15 +42,8 @@ def set_loglevel(verbose: int) -> str:
Add the handler to the logger, with the level and format string.
Return the level
"""
log.remove()
level = {0: "INFO", 1: "DEBUG", 2: "TRACE"}.get(verbose, "TRACE")
if level == "INFO":
format_str = "<green>{time:HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{module: <18}</cyan> - <level>{message}</level>"
else:
format_str = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"

log.add(
sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str
)
# reuse mpflash logger
mpf_set_loglevel(level)
log.info(f"micropython-stubber {__version__}")
return level
2 changes: 1 addition & 1 deletion src/stubber/commands/clone_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import rich_click as click
import mpflash.basicgit as git
from loguru import logger as log
from mpflash.logger import log
from stubber.utils.config import CONFIG

from .cli import stubber_cli
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/config_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# pragma: no cover

from loguru import logger as log
from mpflash.logger import log

from stubber.utils.config import CONFIG

Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/enrich_folder_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Union

import rich_click as click
from loguru import logger as log
from mpflash.logger import log

from stubber.codemod.enrich import enrich_folder
from stubber.utils.config import CONFIG
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/get_core_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import List

import rich_click as click
from loguru import logger as log
from mpflash.logger import log

import stubber.get_cpython as get_cpython
import stubber.utils as utils
Expand Down
7 changes: 5 additions & 2 deletions src/stubber/commands/get_docstubs_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Optional

import rich_click as click
from loguru import logger as log
from mpflash.logger import log

import mpflash.basicgit as git
import stubber.utils as utils
Expand All @@ -22,7 +22,10 @@
#########################################################################################


@stubber_cli.command(name="get-docstubs")
@stubber_cli.command(
name="get-docstubs",
aliases=["get-doc-stubs", "docstubs"],
)
@click.option(
"--path",
"-p",
Expand Down
7 changes: 5 additions & 2 deletions src/stubber/commands/get_frozen_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List, Optional

import rich_click as click
from loguru import logger as log
from mpflash.logger import log

import stubber.utils as utils
from stubber.codemod.enrich import enrich_folder
Expand All @@ -20,7 +20,10 @@
##########################################################################################


@stubber_cli.command(name="get-frozen")
@stubber_cli.command(
name="get-frozen",
aliases=["get-frozen-stubs", "frozen"],
)
@click.option(
"--stub-folder",
"-stubs",
Expand Down
Loading

0 comments on commit b35d028

Please sign in to comment.