Skip to content

Commit 50ceb7f

Browse files
committed
Merge branch 'mpflash/download_pico'
2 parents d81f7f9 + 7e8dd6e commit 50ceb7f

File tree

9 files changed

+73
-14
lines changed

9 files changed

+73
-14
lines changed

src/mpflash/mpflash/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FWInfo:
4747
firmware: str = field(default="") # url or path to original firmware image
4848
variant: str = field(default="") # MicroPython variant
4949
preview: bool = field(default=False) # True if the firmware is a preview version
50-
version: str = field(default="") # MicroPython version
50+
version: str = field(default="") # MicroPython version (NO v prefix)
5151
url: str = field(default="") # url to the firmware image download folder
5252
build: str = field(default="0") # The build = number of commits since the last release
5353
ext: str = field(default="") # the file extension of the firmware

src/mpflash/mpflash/download.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from mpflash.common import PORT_FWTYPES, FWInfo
2222
from mpflash.errors import MPFlashError
2323
from mpflash.mpboard_id import get_known_ports
24+
from mpflash.vendor.versions import clean_version
2425

2526
# avoid conflict with the ujson used by MicroPython
2627
jsonlines.ujson = None # type: ignore
@@ -157,7 +158,7 @@ def get_boards(ports: List[str], boards: List[str], clean: bool) -> List[FWInfo]
157158
# board["firmware"] = _url
158159
# board["preview"] = "preview" in _url # type: ignore
159160
if ver_match := re.search(RE_VERSION_PREVIEW, _url):
160-
fw_info.version = ver_match.group(1)
161+
fw_info.version = clean_version(ver_match.group(1))
161162
fw_info.build = ver_match.group(2) or "0"
162163
fw_info.preview = fw_info.build != "0"
163164
# # else:
@@ -194,8 +195,9 @@ def download_firmwares(
194195
clean: bool = True,
195196
) -> int:
196197
skipped = downloaded = 0
197-
if versions is None:
198-
versions = []
198+
versions = [] if versions is None else [clean_version(v) for v in versions]
199+
# handle renamed boards
200+
boards = add_renamed_boards(boards)
199201

200202
unique_boards = get_firmware_list(ports, boards, versions, clean)
201203

@@ -305,6 +307,7 @@ def download(
305307
if not boards:
306308
log.critical("No boards found, please connect a board or specify boards to download firmware for.")
307309
raise MPFlashError("No boards found")
310+
308311
# versions = [clean_version(v, drop_v=True) for v in versions] # remove leading v from version
309312
try:
310313
destination.mkdir(exist_ok=True, parents=True)
@@ -318,3 +321,28 @@ def download(
318321
raise MPFlashError("Could not connect to micropython.org") from e
319322

320323
return result
324+
325+
326+
def add_renamed_boards(boards: List[str]) -> List[str]:
327+
"""
328+
Adds the renamed boards to the list of boards.
329+
330+
Args:
331+
boards : The list of boards to add the renamed boards to.
332+
333+
Returns:
334+
List[str]: The list of boards with the renamed boards added.
335+
336+
"""
337+
renamed = {
338+
"PICO": ["RPI_PICO"],
339+
"PICO_W": ["RPI_PICO_W"],
340+
"GENERIC": ["ESP32_GENERIC", "ESP8266_GENERIC"], # just add both of them
341+
}
342+
_boards = boards.copy()
343+
for board in boards:
344+
if board in renamed and renamed[board] not in boards:
345+
_boards.extend(renamed[board])
346+
if board != board.upper() and board.upper() not in boards:
347+
_boards.append(board.upper())
348+
return _boards

src/mpflash/mpflash/downloaded.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def downloaded_firmwares(fw_folder: Path) -> List[FWInfo]:
2727
def find_downloaded_firmware(
2828
*,
2929
board_id: str,
30-
version: str = "",
30+
version: str = "", # v1.2.3
3131
port: str = "",
3232
variants: bool = False,
3333
fw_folder: Optional[Path] = None,
@@ -43,16 +43,16 @@ def find_downloaded_firmware(
4343
log.error("No firmware files found. Please download the firmware first.")
4444
return []
4545
# filter by version
46-
version = clean_version(version, drop_v=True)
46+
version = clean_version(version)
4747
fw_list = filter_downloaded_fwlist(fw_list, board_id, version, port, variants, selector)
4848

4949
if not fw_list and trie < 3:
5050
log.info(f"Try ({trie+1}) to find a firmware for the board {board_id}")
5151
if trie == 1:
52-
# ESP board naming conventions have changed by adding a PORT refix
52+
# ESP board naming conventions have changed by adding a PORT prefix
5353
if port.startswith("esp") and not board_id.startswith(port.upper()):
5454
board_id = f"{port.upper()}_{board_id}"
55-
# RP2 board naming conventions have changed by adding a _RPIprefix
55+
# RP2 board naming conventions have changed by adding a _RPI prefix
5656
if port == "rp2" and not board_id.startswith("RPI_"):
5757
board_id = f"RPI_{board_id}"
5858
elif trie == 2:
@@ -75,7 +75,7 @@ def find_downloaded_firmware(
7575
def filter_downloaded_fwlist(
7676
fw_list: List[FWInfo],
7777
board_id: str,
78-
version: str,
78+
version: str, # v1.2.3
7979
port: str,
8080
# preview: bool,
8181
variants: bool,
@@ -86,7 +86,9 @@ def filter_downloaded_fwlist(
8686
# never get a preview for an older version
8787
fw_list = [fw for fw in fw_list if fw.preview]
8888
else:
89-
fw_list = [fw for fw in fw_list if fw.version == version]
89+
# FWInfo version has no v1.2.3 prefix
90+
_version = clean_version(version, drop_v=True)
91+
fw_list = [fw for fw in fw_list if fw.version == _version]
9092

9193
# filter by port
9294
if port:

src/mpflash/mpflash/mpboard_id/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ def known_stored_boards(port: str, versions: Optional[List[str]] = None) -> List
7777
@lru_cache(maxsize=20)
7878
def find_known_board(board_id: str) -> Board:
7979
"""Find the board for the given BOARD_ID or 'board description' and return the board info as a Board object"""
80+
# FIXME : functional overlap with:
81+
# mpboard_id\board_id.py _find_board_id_by_description
8082
info = read_known_boardinfo()
8183
for board_info in info:
8284
if board_id in (board_info.board_id, board_info.description):
8385
if not board_info.cpu:
86+
# safeguard for older board_info.json files
87+
print(f"Board {board_id} has no CPU info, using port as CPU")
8488
if " with " in board_info.description:
8589
board_info.cpu = board_info.description.split(" with ")[-1]
8690
else:

src/mpflash/mpflash/mpboard_id/board_id.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,23 @@ def _find_board_id_by_description(
4444
"""
4545
Find the MicroPython BOARD_ID based on the description in the firmware
4646
using the pre-built board_info.json file
47+
48+
Parameters:
49+
descr: str
50+
Description of the board
51+
short_descr: str
52+
Short description of the board (optional)
53+
version: str
54+
Version of the MicroPython firmware
55+
board_info: Path
56+
Path to the board_info.json file (optional)
57+
4758
"""
59+
# FIXME: functional overlap with
60+
# src\mpflash\mpflash\mpboard_id\__init__.py find_known_board
61+
62+
if not short_descr and " with " in descr:
63+
short_descr = descr.split(" with ")[0]
4864

4965
candidate_boards = read_known_boardinfo(board_info)
5066

@@ -58,10 +74,10 @@ def _find_board_id_by_description(
5874
# FIXME if latest stable is newer than the last version in the boardlist this will fail
5975
log.trace(f"Version {version} not found in board info, using latest known version {known_versions[-1]}")
6076
version = known_versions[-1]
61-
version_matches = [b for b in candidate_boards if b.version.startswith(version)]
62-
if not version_matches:
77+
if version_matches := [b for b in candidate_boards if b.version.startswith(version)]:
78+
candidate_boards = version_matches
79+
else:
6380
raise MPFlashError(f"No board info found for version {version}")
64-
candidate_boards = version_matches
6581
matches = [b for b in candidate_boards if b.description == descr]
6682
if not matches and short_descr:
6783
matches = [b for b in candidate_boards if b.description == short_descr]

src/mpflash/mpflash/mpboard_id/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414

1515
def write_boardinfo_json(board_list: List[Board], *, folder: Path):
16-
"""Writes the board information to JSON and CSV files.
16+
"""Writes the board information to a JSON file.
1717
1818
Args:
1919
board_list (List[Board]): The list of Board objects.
20+
folder (Path): The folder where the compressed JSON file will be saved.
2021
"""
2122
import zipfile
2223

src/mpflash/tests/data/firmware/firmware.jsonl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
{"board": "RPI_PICO_W", "url": "https://micropython.org/download/RPI_PICO_W", "port": "rp2", "firmware": "https://micropython.org/resources/firmware/RPI_PICO_W-20240316-v1.23.0-preview.236.gcd8eea2ae.uf2", "preview": true, "version": "1.23.0-preview.236", "build": "236", "filename": "rp2\\RPI_PICO_W-v1.23.0-preview.236.uf2", "ext": ".uf2", "variant": "RPI_PICO_W"}
1616
{"board": "RPI_PICO_W", "url": "https://micropython.org/download/RPI_PICO_W", "port": "rp2", "firmware": "https://micropython.org/resources/firmware/RPI_PICO_W-20240222-v1.22.2.uf2", "preview": false, "version": "1.22.2", "build": "0", "filename": "rp2\\RPI_PICO_W-v1.22.2.uf2", "ext": ".uf2", "variant": "RPI_PICO_W"}
1717
{"board": "RPI_PICO", "url": "https://micropython.org/download/RPI_PICO", "port": "rp2", "firmware": "https://micropython.org/resources/firmware/RPI_PICO-20240222-v1.22.2.uf2", "preview": false, "version": "1.22.2", "build": "0", "filename": "rp2\\RPI_PICO-v1.22.2.uf2", "ext": ".uf2", "variant": "RPI_PICO"}
18+
{"board": "RPI_PICO", "url": "https://micropython.org/download/RPI_PICO", "port": "rp2", "firmware": "https://micropython.org/resources/firmware/RPI_PICO-20240222-v1.19.1.uf2", "preview": false, "version": "1.19.1", "build": "0", "filename": "rp2\\RPI_PICO-v1.19.1.uf2", "ext": ".uf2", "variant": "RPI_PICO"}

src/mpflash/tests/mpboard_id/test_find_board_id.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
("esp32_v1.22.0-a", "v1.22.0", "ESP32 module with ESP32", None, "UNKNOWN_BOARD"),
3838
("esp32_v1.21.0-a", None, "ESP32 module with ESP32", None, "GENERIC"),
3939
("esp32_v1.22.0-a", None, "ESP32 module with ESP32", None, "GENERIC"),
40+
# PICO
41+
("pico_v1.19.1-old", "v1.19.1", "Raspberry Pi Pico with RP2040", "Raspberry Pi Pico", "PICO"),
42+
("pico_v1.23.0", "v1.23.0", "Raspberry Pi Pico with RP2040", "Raspberry Pi Pico", "RPI_PICO"),
4043
# Error cases
4144
("error-1", "stable", "Board X", "X", None),
4245
("error-2", "stable", "Board A", "A", None),

src/mpflash/tests/test_downloaded.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ def test_downloaded_firmwares(mocker: MockerFixture, test_fw_path):
2323
[
2424
("esp32", "ESP32_GENERIC", "preview", True),
2525
("esp32", "GENERIC", "preview", True),
26+
# Old and new names for PICO
2627
("rp2", "RPI_PICO", "1.22.2", True),
2728
("rp2", "PICO", "1.22.2", True),
29+
# old name for PICO
30+
("rp2", "PICO", "1.19.1", True),
31+
# old and new name for PICO_W
2832
("rp2", "RPI_PICO_W", "1.22.2", True),
2933
("rp2", "PICO_W", "1.22.2", True),
3034
("fake", "NO_BOARD", "1.22.2", False),

0 commit comments

Comments
 (0)