Skip to content

Commit

Permalink
mpflash: fix device filtering on Windows
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 8, 2024
1 parent b76d592 commit 7aef15e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/mpflash/mpflash/cli_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@
show_default=True,
metavar="SERIALPORT",
)
@click.option(
"--bluetooth/--no-bluetooth",
"-b/-nb",
is_flag=True,
default=False,
show_default=True,
help="""Include bluetooth ports in the list""",
)
@click.option(
"--port",
"-p",
Expand Down Expand Up @@ -133,7 +141,7 @@ def cli_flash_board(**kwargs) -> int:
all_boards: List[MPRemoteBoard] = []
if not params.boards:
# nothing specified - detect connected boards
params.ports, params.boards, all_boards = connected_ports_boards(include=params.ports, ignore=params.ignore)
params.ports, params.boards, all_boards = connected_ports_boards(include=params.ports, ignore=params.ignore, bluetooth=params.bluetooth)
if params.boards == []:
# No MicroPython boards detected, but it could be unflashed or in bootloader mode
# Ask for serial port and board_id to flash
Expand Down
13 changes: 9 additions & 4 deletions src/mpflash/mpflash/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fnmatch
import glob
import os
import platform
import sys
from dataclasses import dataclass, field
from enum import Enum
Expand Down Expand Up @@ -84,6 +85,7 @@ class Params:
fw_folder: Path = Path()
serial: List[str] = field(default_factory=list)
ignore: List[str] = field(default_factory=list)
bluetooth: bool = False


@dataclass
Expand Down Expand Up @@ -138,12 +140,15 @@ def filtered_comports(

# remove ports that are to be ignored
log.trace(f"{include=}, {ignore=}, {bluetooth=}")
# use p.location to filter out the bogus ports on newer Linux kernels

comports = [
p
for p in list_ports.comports()
if p.location and not any(fnmatch.fnmatch(p.device, i) for i in ignore)
p for p in list_ports.comports() if not any(fnmatch.fnmatch(p.device, i) for i in ignore)
]
if platform.system() == "Linux":
# use p.location to filter out the bogus ports on newer Linux kernels
# filter out the bogus ports on newer Linux kernels
comports = [p for p in comports if p.location]

log.trace(f"comports: {[p.device for p in comports]}")
# remove bluetooth ports

Expand Down
35 changes: 28 additions & 7 deletions src/mpflash/mpflash/connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Column

from mpflash.common import filtered_comports, find_serial_by_path
from mpflash.common import filtered_comports
from mpflash.mpremoteboard import MPRemoteBoard

import os

def connected_ports_boards(*, include: List[str], ignore: List[str]) -> Tuple[List[str], List[str], List[MPRemoteBoard]]:
if os.name == "linux":
from mpflash.common import find_serial_by_path # type: ignore
else:

def find_serial_by_path(path: str) -> str:
# log.warning(f"find_serial_by_path not implemented for {os.name}")
return path


def connected_ports_boards(
*, include: List[str], ignore: List[str], bluetooth: bool = False
) -> Tuple[List[str], List[str], List[MPRemoteBoard]]:
"""
Returns a tuple containing lists of unique ports and boards from the connected MCUs.
Boards that are physically connected, but give no tangible response are ignored.
Expand All @@ -19,10 +31,17 @@ def connected_ports_boards(*, include: List[str], ignore: List[str]) -> Tuple[Li
- A list of unique board names of the connected MCUs.
- A list of MPRemoteBoard instances of the connected MCUs.
"""
mpr_boards = [b for b in list_mcus(include=include, ignore=ignore) if b.connected]
ports = list({b.port for b in mpr_boards})
boards = list({b.board for b in mpr_boards})
return (ports, boards, mpr_boards)
conn_mcus = [
b for b in list_mcus(include=include, ignore=ignore, bluetooth=bluetooth) if b.connected
]
# ignore boards that have the [micropython-stubber] ignore flag set
conn_mcus = [
item for item in conn_mcus if not (item.toml.get("mpflash", {}).get("ignore", False))
]

ports = list({b.port for b in conn_mcus})
boards = list({b.board for b in conn_mcus})
return (ports, boards, conn_mcus)


# #########################################################################################################
Expand All @@ -31,7 +50,9 @@ def connected_ports_boards(*, include: List[str], ignore: List[str]) -> Tuple[Li
rp_bar = BarColumn(bar_width=None, table_column=Column())


def list_mcus(*, ignore: List[str], include: List[str], bluetooth: bool = False) -> List[MPRemoteBoard]:
def list_mcus(
*, ignore: List[str], include: List[str], bluetooth: bool = False
) -> List[MPRemoteBoard]:
"""
Retrieves information about connected microcontroller boards.
Expand Down
2 changes: 1 addition & 1 deletion src/mpflash/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mpflash"
version = "1.0.1"
version = "1.0.2"
description = "Flash and download tool for MicroPython firmwares"
authors = ["Jos Verlinde <jos_verlinde@hotmail.com>"]
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions src/mpflash/tests/test_filtered_comports.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def test_skip_bogus_comports_linux(mocker):
ListPortInfo(device="/dev/tty001", skip_link_detection=True),
ListPortInfo(device="/dev/tty002", skip_link_detection=True),
]

def platform_system():
return "Linux"

mocker.patch("mpflash.common.platform.system", platform_system)

linux_ports[0].location = f"1-1.1:x.0"
mocker.patch("mpflash.common.list_ports.comports", return_value=linux_ports)
result = filtered_comports(include=["*"], ignore=[], bluetooth=False)
Expand Down

0 comments on commit 7aef15e

Please sign in to comment.