Skip to content

Commit

Permalink
Merge pull request #634 from Josverl/mpflash-fix-macports
Browse files Browse the repository at this point in the history
mpflash: Fix macOS specific ports scanning issues

Fixes : #633
  • Loading branch information
Josverl authored Dec 17, 2024
2 parents a2a55d1 + 4a57ddb commit 10ef266
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 11 deletions.
31 changes: 25 additions & 6 deletions src/mpflash/mpflash/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import glob
import os
import platform
import sys
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
Expand Down Expand Up @@ -30,7 +29,11 @@

# Token with no permissions to avoid throttling
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#getting-a-higher-rate-limit
PAT_NO_ACCESS = "github_pat_"+"11AAHPVFQ0G4NTaQ73Bw5J"+"_fAp7K9sZ1qL8VFnI9g78eUlCdmOXHB3WzSdj2jtEYb4XF3N7PDJBl32qIxq"
PAT_NO_ACCESS = (
"github_pat_"
+ "11AAHPVFQ0G4NTaQ73Bw5J"
+ "_fAp7K9sZ1qL8VFnI9g78eUlCdmOXHB3WzSdj2jtEYb4XF3N7PDJBl32qIxq"
)

PAT = os.environ.get("GITHUB_TOKEN") or PAT_NO_ACCESS
GH_CLIENT = Github(auth=Auth.Token(PAT))
Expand Down Expand Up @@ -135,12 +138,23 @@ def filtered_comports(
elif not isinstance(include, list): # type: ignore
include = list(include)

if ignore == [] and platform.system() == "Darwin":
# By default ignore some of the irrelevant ports on macOS
ignore = [
"/dev/*.debug-console",
]

# remove ports that are to be ignored
log.trace(f"{include=}, {ignore=}, {bluetooth=}")

comports = [
p for p in list_ports.comports() if not any(fnmatch.fnmatch(p.device, i) for i in ignore)
]

if False:
import jsons
print(jsons.dumps(comports).replace('{"description":', '\n{"description":'))

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
Expand All @@ -161,16 +175,21 @@ def filtered_comports(
else:
# if there are ports to ignore, add the explicit list to the filtered list
comports = list(set(explicit) | set(comports))
if platform.system() == "Darwin":
# Failsafe: filter out debug-console ports
comports = [p for p in comports if not p.description.endswith(".debug-console")]

if not bluetooth:
# filter out bluetooth ports
comports = [p for p in comports if "bluetooth" not in p.description.lower()]
comports = [p for p in comports if "BTHENUM" not in p.hwid]
if sys.platform == "darwin":
if platform.system() == "Darwin":
comports = [p for p in comports if ".Bluetooth" not in p.device]
log.trace(f"no Bluetooth: {[p.device for p in comports]}")
# filter out ports with no hwid
comports = [p for p in comports if p.hwid != "n/a"]
log.debug(f"filtered_comports: {[p.device for p in comports]}")
# sort
if sys.platform == "win32":
if platform.system() == "Windows":
# Windows sort of comports by number - but fallback to device name
return sorted(
comports,
Expand All @@ -184,7 +203,7 @@ def find_serial_by_path(target_port: str):
"""Find the symbolic link path of a serial port by its device path."""
# sourcery skip: use-next

if os.name == "nt":
if platform.system() == "Windows":
return None
# List all available serial ports
available_ports = list_ports.comports()
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.24.2"
version = "1.24.4"
description = "Flash and download tool for MicroPython firmwares"
authors = ["Jos Verlinde <[email protected]>"]
license = "MIT"
Expand Down
13 changes: 13 additions & 0 deletions src/mpflash/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Shared Pytest configuration and fixtures for mpflash tests."""

import sys
from pathlib import Path

import pytest
Expand All @@ -9,3 +10,15 @@
def test_fw_path():
"""Return the path to the test firmware folder."""
return Path(__file__).parent / "data" / "firmware"


# --------------------------------------
# https://docs.pytest.org/en/stable/example/markers.html#marking-platform-specific-tests-with-pytest
ALL_OS = set("win32 linux darwin".split())


def pytest_runtest_setup(item):
supported_platforms = ALL_OS.intersection(mark.name for mark in item.iter_markers())
platform = sys.platform
if supported_platforms and platform not in supported_platforms:
pytest.skip("cannot run on platform {}".format(platform))
Loading

0 comments on commit 10ef266

Please sign in to comment.