Skip to content

Commit f6c3c7f

Browse files
committed
feat: improve build and version detection
now can provide error when version is not published
1 parent 580b42c commit f6c3c7f

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/mpflash/mpflash/download.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@
3232
# Regexes to remove dates and hashes in the filename that just get in the way
3333
RE_DATE = r"(-\d{8}-)"
3434
RE_HASH = r"(.g[0-9a-f]+\.)"
35-
# regex to extract the version from the firmware filename
36-
RE_VERSION_PREVIEW = r"(\d+\.\d+(\.\d+)?(-\w+.\d+)?)"
35+
# regex to extract the version and the build from the firmware filename
36+
# group 1 is the version, group 2 is the build
37+
RE_VERSION_PREVIEW = r"v([\d\.]+)-?(?:preview\.)?(\d+)?\."
38+
# 'RPI_PICO_W-v1.23.uf2'
39+
# 'RPI_PICO_W-v1.23.0.uf2'
40+
# 'RPI_PICO_W-v1.23.0-406.uf2'
41+
# 'RPI_PICO_W-v1.23.0-preview.406.uf2'
42+
# 'RPI_PICO_W-v1.23.0-preview.4.uf2'
43+
# 'RPI_PICO_W-v1.23.0.uf2'
44+
# 'https://micropython.org/resources/firmware/RPI_PICO_W-20240531-v1.24.0-preview.10.gc1a6b95bf.uf2'
45+
# 'https://micropython.org/resources/firmware/RPI_PICO_W-20240531-v1.24.0-preview.10.uf2'
46+
# 'RPI_PICO_W-v1.24.0-preview.10.gc1a6b95bf.uf2'
3747

3848

3949
# use functools.lru_cache to avoid needing to download pages multiple times
@@ -98,6 +108,7 @@ def board_firmware_urls(board_url: str, base_url: str, ext: str) -> List[str]:
98108
# The first run takes ~60 seconds to run for 4 ports , all boards
99109
# so it makes sense to cache the results and skip boards as soon as possible
100110
def get_boards(ports: List[str], boards: List[str], clean: bool) -> List[FWInfo]:
111+
# sourcery skip: use-getitem-for-re-match-groups
101112
"""
102113
Retrieves a list of firmware information for the specified ports and boards.
103114
@@ -146,13 +157,15 @@ def get_boards(ports: List[str], boards: List[str], clean: bool) -> List[FWInfo]
146157
# board["firmware"] = _url
147158
# board["preview"] = "preview" in _url # type: ignore
148159
if ver_match := re.search(RE_VERSION_PREVIEW, _url):
149-
fw_info.version = ver_match[1]
160+
fw_info.version = ver_match.group(1)
161+
fw_info.build = ver_match.group(2) or "0"
162+
fw_info.preview = fw_info.build != "0"
163+
# # else:
164+
# # board.$1= ""
165+
# if "preview." in fw_info.version:
166+
# fw_info.build = fw_info.version.split("preview.")[-1]
150167
# else:
151-
# board.$1= ""
152-
if "preview." in fw_info.version:
153-
fw_info.build = fw_info.version.split("preview.")[-1]
154-
else:
155-
fw_info.build = "0"
168+
# fw_info.build = "0"
156169

157170
fw_info.ext = Path(fw_info.firmware).suffix
158171
fw_info.variant = fw_info.filename.split("-v")[0] if "-v" in fw_info.filename else ""
@@ -191,6 +204,11 @@ def download_firmwares(
191204
# relevant
192205

193206
log.info(f"Found {len(unique_boards)} relevant unique firmwares")
207+
if not unique_boards:
208+
log.error("No relevant firmwares could be found on https://micropython.org/download")
209+
log.info(f"{versions=} {ports=} {boards=}")
210+
log.info("Please check the website for the latest firmware files or try the preview version.")
211+
return 0
194212

195213
firmware_folder.mkdir(exist_ok=True)
196214

@@ -238,12 +256,15 @@ def get_firmware_list(ports: List[str], boards: List[str], versions: List[str],
238256
board_urls = sorted(get_boards(ports, boards, clean), key=key_fw_ver_pre_ext_bld)
239257

240258
log.debug(f"Total {len(board_urls)} firmwares")
259+
241260
relevant = [
242261
board
243262
for board in board_urls
244-
if board.board in boards and (board.version in versions or board.preview and preview)
245-
# and b["port"] in ["esp32", "rp2"]
263+
if board.version in versions and board.build == "0" and board.board in boards and not board.preview
246264
]
265+
266+
if preview:
267+
relevant.extend([board for board in board_urls if board.board in boards and board.preview])
247268
log.debug(f"Matching firmwares: {len(relevant)}")
248269
# select the unique boards
249270
unique_boards: List[FWInfo] = []

0 commit comments

Comments
 (0)