From 81976c3465f372f6a6c5c9a8249d44e218efd5bf Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 15 Mar 2024 16:16:51 -0400 Subject: [PATCH] Add command for caching latest firmware version --- circfirm/cli/cache.py | 28 +++++++++++++++++++++++++++- tests/cli/test_cli_cache.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/circfirm/cli/cache.py b/circfirm/cli/cache.py index 08fddf5..300bbbe 100644 --- a/circfirm/cli/cache.py +++ b/circfirm/cli/cache.py @@ -17,6 +17,7 @@ import circfirm import circfirm.backend.cache +import circfirm.backend.s3 import circfirm.cli import circfirm.startup @@ -88,7 +89,7 @@ def cache_list(board_id: Optional[str]) -> None: @click.argument("version") @click.option("-l", "--language", default="en_US", help="CircuitPython language/locale") def cache_save(board_id: str, version: str, language: str) -> None: - """Install a version of CircuitPython to cache.""" + """Download a version of CircuitPython to the cache.""" try: circfirm.cli.announce_and_await( f"Caching firmware version {version} for {board_id}", @@ -97,3 +98,28 @@ def cache_save(board_id: str, version: str, language: str) -> None: ) except ConnectionError as err: raise click.exceptions.ClickException(err.args[0]) + + +@cli.command(name="latest") +@click.argument("board-id") +@click.option("-l", "--language", default="en_US", help="CircuitPython language/locale") +@click.option( + "-p", + "--pre-release", + is_flag=True, + default=False, + help="Whether pre-release versions should be considered", +) +def cache_latest(board_id: str, language: str, pre_release: bool) -> None: + """Download the latest version of CircuitPython to the cache.""" + try: + version = circfirm.backend.s3.get_latest_board_version( + board_id, language, pre_release + ) + circfirm.cli.announce_and_await( + f"Caching firmware version {version} for {board_id}", + circfirm.backend.cache.download_uf2, + args=(board_id, version, language), + ) + except ConnectionError as err: + raise click.exceptions.ClickException(err.args[0]) diff --git a/tests/cli/test_cli_cache.py b/tests/cli/test_cli_cache.py index 49e746e..643b71f 100644 --- a/tests/cli/test_cli_cache.py +++ b/tests/cli/test_cli_cache.py @@ -97,7 +97,7 @@ def test_cache_clear() -> None: """Tests the cache clear command.""" board = "feather_m4_express" version = "7.1.0" - langauge = "zh_Latn_pinyin" + language = "zh_Latn_pinyin" # Move firmware files to app directory tests.helpers.copy_firmwares() @@ -137,3 +137,32 @@ def test_cache_clear() -> None: assert result.exit_code == 0 assert result.output == "Cache cleared!\n" assert len(list(board_folder.parent.glob("*"))) == 0 + + +def test_cache_latest() -> None: + """Test the update command when in CIRCUITPY mode.""" + board = "feather_m0_express" + language = "cs" + expected_version = "6.1.0" + + uf2_filepath = circfirm.backend.cache.get_uf2_filepath( + board, expected_version, language + ) + assert not os.path.exists(uf2_filepath) + + try: + # Save the latest version (successful) + result = RUNNER.invoke(cli, ["cache", "latest", board, "--language", language]) + assert result.exit_code == 0 + assert os.path.exists(uf2_filepath) + + # Save the latest version (unsuccessful) + result = RUNNER.invoke( + cli, ["cache", "latest", board, "--language", "doesnotexist"] + ) + assert result.exit_code == 1 + + finally: + board_folder = circfirm.backend.cache.get_board_folder(board) + if board_folder.exists(): + shutil.rmtree(board_folder)