diff --git a/pkgs/clan-cli/clan_cli/flash/flash_cmd.py b/pkgs/clan-cli/clan_cli/flash/flash_cmd.py index b0ac95f69..c4a8f1084 100644 --- a/pkgs/clan-cli/clan_cli/flash/flash_cmd.py +++ b/pkgs/clan-cli/clan_cli/flash/flash_cmd.py @@ -7,12 +7,11 @@ from pathlib import Path from typing import Any from clan_lib.flake import Flake +from clan_lib.flash.flash import Disk, SystemConfig, run_machine_flash from clan_lib.machines.machines import Machine from clan_cli.completions import add_dynamic_completer, complete_machines -from .flash import Disk, SystemConfig, run_machine_flash - log = logging.getLogger(__name__) diff --git a/pkgs/clan-cli/clan_cli/flash/list.py b/pkgs/clan-cli/clan_cli/flash/list.py index cbe327151..ddfcb06a6 100644 --- a/pkgs/clan-cli/clan_cli/flash/list.py +++ b/pkgs/clan-cli/clan_cli/flash/list.py @@ -1,79 +1,11 @@ import argparse import logging -import os -from pathlib import Path -from typing import TypedDict -from clan_lib.api import API -from clan_lib.cmd import Log, RunOpts, run -from clan_lib.errors import ClanError -from clan_lib.nix import nix_build +from clan_lib.flash.list import list_keymaps, list_languages log = logging.getLogger(__name__) -class FlashOptions(TypedDict): - languages: list[str] - keymaps: list[str] - - -@API.register -def get_machine_flash_options() -> FlashOptions: - """Retrieve available languages and keymaps for flash configuration. - Returns: - FlashOptions: A dictionary containing lists of available languages and keymaps. - Raises: - ClanError: If the locale file or keymaps directory does not exist. - """ - return {"languages": list_languages(), "keymaps": list_keymaps()} - - -def list_languages() -> list[str]: - cmd = nix_build(["nixpkgs#glibcLocales"]) - result = run(cmd, RunOpts(log=Log.STDERR, error_msg="Failed to find glibc locales")) - locale_file = Path(result.stdout.strip()) / "share" / "i18n" / "SUPPORTED" - - if not locale_file.exists(): - msg = f"Locale file '{locale_file}' does not exist." - raise ClanError(msg) - - with locale_file.open() as f: - lines = f.readlines() - - languages = [] - for line in lines: - if line.startswith("#"): - continue - if "SUPPORTED-LOCALES" in line: - continue - # Split by '/' and take the first part - language = line.split("/")[0].strip() - languages.append(language) - - return languages - - -def list_keymaps() -> list[str]: - cmd = nix_build(["nixpkgs#kbd"]) - result = run(cmd, RunOpts(log=Log.STDERR, error_msg="Failed to find kbdinfo")) - keymaps_dir = Path(result.stdout.strip()) / "share" / "keymaps" - - if not keymaps_dir.exists(): - msg = f"Keymaps directory '{keymaps_dir}' does not exist." - raise ClanError(msg) - - keymap_files = [] - - for _root, _, files in os.walk(keymaps_dir): - for file in files: - if file.endswith(".map.gz"): - # Remove '.map.gz' ending - name_without_ext = file[:-7] - keymap_files.append(name_without_ext) - - return keymap_files - - def list_command(args: argparse.Namespace) -> None: if args.cmd == "languages": languages = list_languages() diff --git a/pkgs/clan-cli/clan_lib/flash/__init__.py b/pkgs/clan-cli/clan_lib/flash/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pkgs/clan-cli/clan_cli/flash/automount.py b/pkgs/clan-cli/clan_lib/flash/automount.py similarity index 100% rename from pkgs/clan-cli/clan_cli/flash/automount.py rename to pkgs/clan-cli/clan_lib/flash/automount.py diff --git a/pkgs/clan-cli/clan_cli/flash/flash.py b/pkgs/clan-cli/clan_lib/flash/flash.py similarity index 100% rename from pkgs/clan-cli/clan_cli/flash/flash.py rename to pkgs/clan-cli/clan_lib/flash/flash.py index d43ffdd9f..5dfd50da4 100644 --- a/pkgs/clan-cli/clan_cli/flash/flash.py +++ b/pkgs/clan-cli/clan_lib/flash/flash.py @@ -6,16 +6,16 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import Any +from clan_cli.facts.generate import generate_facts +from clan_cli.vars.generate import generate_vars +from clan_cli.vars.upload import populate_secret_vars + from clan_lib.api import API from clan_lib.cmd import Log, RunOpts, cmd_with_root, run from clan_lib.errors import ClanError from clan_lib.machines.machines import Machine from clan_lib.nix import nix_shell -from clan_cli.facts.generate import generate_facts -from clan_cli.vars.generate import generate_vars -from clan_cli.vars.upload import populate_secret_vars - from .automount import pause_automounting from .list import list_keymaps, list_languages diff --git a/pkgs/clan-cli/clan_cli/flash/inhibit.sh b/pkgs/clan-cli/clan_lib/flash/inhibit.sh similarity index 100% rename from pkgs/clan-cli/clan_cli/flash/inhibit.sh rename to pkgs/clan-cli/clan_lib/flash/inhibit.sh diff --git a/pkgs/clan-cli/clan_lib/flash/list.py b/pkgs/clan-cli/clan_lib/flash/list.py new file mode 100644 index 000000000..4a23c8476 --- /dev/null +++ b/pkgs/clan-cli/clan_lib/flash/list.py @@ -0,0 +1,73 @@ +import logging +import os +from pathlib import Path +from typing import TypedDict + +from clan_lib.api import API +from clan_lib.cmd import Log, RunOpts, run +from clan_lib.errors import ClanError +from clan_lib.nix import nix_build + +log = logging.getLogger(__name__) + + +class FlashOptions(TypedDict): + languages: list[str] + keymaps: list[str] + + +@API.register +def get_machine_flash_options() -> FlashOptions: + """Retrieve available languages and keymaps for flash configuration. + Returns: + FlashOptions: A dictionary containing lists of available languages and keymaps. + Raises: + ClanError: If the locale file or keymaps directory does not exist. + """ + return {"languages": list_languages(), "keymaps": list_keymaps()} + + +def list_languages() -> list[str]: + cmd = nix_build(["nixpkgs#glibcLocales"]) + result = run(cmd, RunOpts(log=Log.STDERR, error_msg="Failed to find glibc locales")) + locale_file = Path(result.stdout.strip()) / "share" / "i18n" / "SUPPORTED" + + if not locale_file.exists(): + msg = f"Locale file '{locale_file}' does not exist." + raise ClanError(msg) + + with locale_file.open() as f: + lines = f.readlines() + + languages = [] + for line in lines: + if line.startswith("#"): + continue + if "SUPPORTED-LOCALES" in line: + continue + # Split by '/' and take the first part + language = line.split("/")[0].strip() + languages.append(language) + + return languages + + +def list_keymaps() -> list[str]: + cmd = nix_build(["nixpkgs#kbd"]) + result = run(cmd, RunOpts(log=Log.STDERR, error_msg="Failed to find kbdinfo")) + keymaps_dir = Path(result.stdout.strip()) / "share" / "keymaps" + + if not keymaps_dir.exists(): + msg = f"Keymaps directory '{keymaps_dir}' does not exist." + raise ClanError(msg) + + keymap_files = [] + + for _root, _, files in os.walk(keymaps_dir): + for file in files: + if file.endswith(".map.gz"): + # Remove '.map.gz' ending + name_without_ext = file[:-7] + keymap_files.append(name_without_ext) + + return keymap_files diff --git a/pkgs/clan-cli/pyproject.toml b/pkgs/clan-cli/pyproject.toml index a2e68d1ad..077bac8c4 100644 --- a/pkgs/clan-cli/pyproject.toml +++ b/pkgs/clan-cli/pyproject.toml @@ -29,6 +29,7 @@ clan_lib = [ "clan_core_templates/**/*", "**/allowed-packages.json", "ssh/*.sh", + "flash/*.sh", ] [tool.pytest.ini_options]