Skip to content

Commit b78369f

Browse files
committed
mpflash: Clean list of downloaded firmware files to avoid duplicate entries
1 parent 50ceb7f commit b78369f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/mpflash/mpflash/download.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from rich.progress import track
2020

2121
from mpflash.common import PORT_FWTYPES, FWInfo
22+
from mpflash.downloaded import clean_downloaded_firmwares
2223
from mpflash.errors import MPFlashError
2324
from mpflash.mpboard_id import get_known_ports
2425
from mpflash.vendor.versions import clean_version
@@ -194,6 +195,17 @@ def download_firmwares(
194195
force: bool = False,
195196
clean: bool = True,
196197
) -> int:
198+
"""
199+
Downloads firmware files based on the specified firmware folder, ports, boards, versions, force flag, and clean flag.
200+
201+
Args:
202+
firmware_folder : The folder to save the downloaded firmware files.
203+
ports : The list of ports to check for firmware.
204+
boards : The list of boards to download firmware for.
205+
versions : The list of versions to download firmware for.
206+
force : A flag indicating whether to force the download even if the firmware file already exists.
207+
clean : A flag indicating to clean the date from the firmware filename.
208+
"""
197209
skipped = downloaded = 0
198210
versions = [] if versions is None else [clean_version(v) for v in versions]
199211
# handle renamed boards
@@ -234,6 +246,8 @@ def download_firmwares(
234246
continue
235247
writer.write(board.to_dict())
236248
downloaded += 1
249+
if downloaded > 0:
250+
clean_downloaded_firmwares(firmware_folder)
237251
log.success(f"Downloaded {downloaded} firmwares, skipped {skipped} existing files.")
238252
return downloaded + skipped
239253

@@ -295,7 +309,7 @@ def download(
295309
boards : The list of boards to download firmware for.
296310
versions : The list of versions to download firmware for.
297311
force : A flag indicating whether to force the download even if the firmware file already exists.
298-
clean : A flag indicating whether to perform a clean download.
312+
clean : A flag indicating whether to clean the date from the firmware filename.
299313
300314
Returns:
301315
int: The number of downloaded firmware files.
@@ -308,7 +322,6 @@ def download(
308322
log.critical("No boards found, please connect a board or specify boards to download firmware for.")
309323
raise MPFlashError("No boards found")
310324

311-
# versions = [clean_version(v, drop_v=True) for v in versions] # remove leading v from version
312325
try:
313326
destination.mkdir(exist_ok=True, parents=True)
314327
except (PermissionError, FileNotFoundError) as e:

src/mpflash/mpflash/downloaded.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ def downloaded_firmwares(fw_folder: Path) -> List[FWInfo]:
2424
return firmwares
2525

2626

27+
def clean_downloaded_firmwares(fw_folder: Path) -> None:
28+
"""
29+
Remove duplicate entries from the firmware.jsonl file, keeping the latest one
30+
uniqueness is based on the filename
31+
"""
32+
firmwares = downloaded_firmwares(fw_folder)
33+
if not firmwares:
34+
return
35+
# keep the latest entry
36+
unique_fw = {fw.filename: fw for fw in firmwares}
37+
with jsonlines.open(fw_folder / "firmware.jsonl", "w") as writer:
38+
for fw in unique_fw.values():
39+
writer.write(fw.to_dict())
40+
log.info(f"Removed duplicate entries from firmware.jsonl in {fw_folder}")
41+
2742
def find_downloaded_firmware(
2843
*,
2944
board_id: str,

0 commit comments

Comments
 (0)