From d02acbe04b62254da0f47d096e0e5adb8b42f4fe Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 18 Oct 2023 18:29:51 +0200 Subject: [PATCH] nix fmt --- pkgs/clan-cli/clan_cli/__init__.py | 9 +-- pkgs/clan-cli/clan_cli/config/__init__.py | 2 +- pkgs/clan-cli/clan_cli/custom_logger.py | 19 ++++--- pkgs/clan-cli/clan_cli/dirs.py | 3 +- pkgs/clan-cli/clan_cli/flakes/create.py | 2 +- pkgs/clan-cli/clan_cli/machines/create.py | 2 +- pkgs/clan-cli/clan_cli/secrets/groups.py | 2 +- pkgs/clan-cli/clan_cli/secrets/machines.py | 2 +- pkgs/clan-cli/clan_cli/secrets/secrets.py | 2 - pkgs/clan-cli/clan_cli/secrets/sops.py | 2 +- pkgs/clan-cli/clan_cli/task_manager.py | 4 +- pkgs/clan-cli/clan_cli/vms/create.py | 1 - .../clan_cli/webui/routers/machines.py | 2 +- pkgs/clan-cli/shell.nix | 57 ++++++++++--------- pkgs/clan-cli/tests/helpers/cli.py | 2 - pkgs/clan-cli/tests/test_vms_api_create.py | 2 +- 16 files changed, 53 insertions(+), 60 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 48f554aa6..8e77efc0f 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -1,13 +1,12 @@ import argparse +import logging import sys from types import ModuleType from typing import Optional from . import config, flakes, join, machines, secrets, vms, webui -from .ssh import cli as ssh_cli - -import logging from .custom_logger import register +from .ssh import cli as ssh_cli log = logging.getLogger(__name__) @@ -57,7 +56,7 @@ def create_parser(prog: Optional[str] = None) -> argparse.ArgumentParser: parser_vms = subparsers.add_parser("vms", help="manage virtual machines") vms.register_parser(parser_vms) -# if args.debug: + # if args.debug: register(logging.DEBUG) log.debug("Debug log activated") @@ -74,8 +73,6 @@ def main() -> None: parser = create_parser() args = parser.parse_args() - - if not hasattr(args, "func"): return diff --git a/pkgs/clan-cli/clan_cli/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index 04ffbb682..dcf2d8ebd 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -11,9 +11,9 @@ from typing import Any, Optional, Tuple, get_origin from clan_cli.dirs import machine_settings_file, specific_flake_dir from clan_cli.errors import ClanError -from clan_cli.types import FlakeName from clan_cli.git import commit_file from clan_cli.nix import nix_eval +from clan_cli.types import FlakeName script_dir = Path(__file__).parent diff --git a/pkgs/clan-cli/clan_cli/custom_logger.py b/pkgs/clan-cli/clan_cli/custom_logger.py index ef05384c3..f9f324e1a 100644 --- a/pkgs/clan-cli/clan_cli/custom_logger.py +++ b/pkgs/clan-cli/clan_cli/custom_logger.py @@ -1,7 +1,7 @@ -import logging -from typing import Any, Callable -from pathlib import Path import inspect +import logging +from pathlib import Path +from typing import Any, Callable grey = "\x1b[38;20m" yellow = "\x1b[33;20m" @@ -11,19 +11,19 @@ green = "\u001b[32m" blue = "\u001b[34m" - def get_formatter(color: str) -> Callable[[logging.LogRecord, bool], logging.Formatter]: - def myformatter(record: logging.LogRecord, with_location: bool) -> logging.Formatter: + def myformatter( + record: logging.LogRecord, with_location: bool + ) -> logging.Formatter: reset = "\x1b[0m" filepath = Path(record.pathname).resolve() if not with_location: - return logging.Formatter( - f"{color}%(levelname)s{reset}: %(message)s" - ) + return logging.Formatter(f"{color}%(levelname)s{reset}: %(message)s") return logging.Formatter( f"{color}%(levelname)s{reset}: %(message)s\n {filepath}:%(lineno)d::%(funcName)s\n" ) + return myformatter @@ -45,6 +45,7 @@ class ThreadFormatter(logging.Formatter): def format(self, record: logging.LogRecord) -> str: return FORMATTER[record.levelno](record, False).format(record) + def get_caller() -> str: frame = inspect.currentframe() if frame is None: @@ -66,4 +67,4 @@ def register(level: Any) -> None: handler.setFormatter(CustomFormatter()) logger = logging.getLogger("registerHandler") logger.addHandler(handler) - #logging.basicConfig(level=level, handlers=[handler]) + # logging.basicConfig(level=level, handlers=[handler]) diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py index 7bc3f748e..5f1157757 100644 --- a/pkgs/clan-cli/clan_cli/dirs.py +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -1,14 +1,15 @@ +import logging import os import sys from pathlib import Path from typing import Optional -import logging from .errors import ClanError from .types import FlakeName log = logging.getLogger(__name__) + def _get_clan_flake_toplevel() -> Path: return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) diff --git a/pkgs/clan-cli/clan_cli/flakes/create.py b/pkgs/clan-cli/clan_cli/flakes/create.py index c3bfb9558..fb8ea16d4 100644 --- a/pkgs/clan-cli/clan_cli/flakes/create.py +++ b/pkgs/clan-cli/clan_cli/flakes/create.py @@ -6,9 +6,9 @@ from typing import Dict from pydantic import AnyUrl from pydantic.tools import parse_obj_as -from ..errors import ClanError from ..async_cmd import CmdOut, run, runforcli from ..dirs import clan_flakes_dir +from ..errors import ClanError from ..nix import nix_command, nix_shell DEFAULT_URL: AnyUrl = parse_obj_as( diff --git a/pkgs/clan-cli/clan_cli/machines/create.py b/pkgs/clan-cli/clan_cli/machines/create.py index d4eedb483..9a2e39bef 100644 --- a/pkgs/clan-cli/clan_cli/machines/create.py +++ b/pkgs/clan-cli/clan_cli/machines/create.py @@ -5,8 +5,8 @@ from typing import Dict from ..async_cmd import CmdOut, run, runforcli from ..dirs import specific_flake_dir, specific_machine_dir from ..errors import ClanError -from ..types import FlakeName from ..nix import nix_shell +from ..types import FlakeName log = logging.getLogger(__name__) diff --git a/pkgs/clan-cli/clan_cli/secrets/groups.py b/pkgs/clan-cli/clan_cli/secrets/groups.py index 0a0fb2dde..a969d415e 100644 --- a/pkgs/clan-cli/clan_cli/secrets/groups.py +++ b/pkgs/clan-cli/clan_cli/secrets/groups.py @@ -3,8 +3,8 @@ import os from pathlib import Path from ..errors import ClanError -from ..types import FlakeName from ..machines.types import machine_name_type, validate_hostname +from ..types import FlakeName from . import secrets from .folders import ( sops_groups_folder, diff --git a/pkgs/clan-cli/clan_cli/secrets/machines.py b/pkgs/clan-cli/clan_cli/secrets/machines.py index e683ec082..316f1013a 100644 --- a/pkgs/clan-cli/clan_cli/secrets/machines.py +++ b/pkgs/clan-cli/clan_cli/secrets/machines.py @@ -1,7 +1,7 @@ import argparse -from ..types import FlakeName from ..machines.types import machine_name_type, validate_hostname +from ..types import FlakeName from . import secrets from .folders import list_objects, remove_object, sops_machines_folder from .sops import read_key, write_key diff --git a/pkgs/clan-cli/clan_cli/secrets/secrets.py b/pkgs/clan-cli/clan_cli/secrets/secrets.py index 2e41bd6a7..64a1abfe5 100644 --- a/pkgs/clan-cli/clan_cli/secrets/secrets.py +++ b/pkgs/clan-cli/clan_cli/secrets/secrets.py @@ -269,7 +269,6 @@ def register_secrets_parser(subparser: argparse._SubParsersAction) -> None: ) parser_get.set_defaults(func=get_command) - parser_set = subparser.add_parser("set", help="set a secret") add_secret_argument(parser_set) parser_set.add_argument( @@ -317,7 +316,6 @@ def register_secrets_parser(subparser: argparse._SubParsersAction) -> None: ) parser_rename.set_defaults(func=rename_command) - parser_remove = subparser.add_parser("remove", help="remove a secret") add_secret_argument(parser_remove) parser_remove.add_argument( diff --git a/pkgs/clan-cli/clan_cli/secrets/sops.py b/pkgs/clan-cli/clan_cli/secrets/sops.py index 4f35761ab..f89edd34b 100644 --- a/pkgs/clan-cli/clan_cli/secrets/sops.py +++ b/pkgs/clan-cli/clan_cli/secrets/sops.py @@ -9,8 +9,8 @@ from typing import IO, Iterator from ..dirs import user_config_dir from ..errors import ClanError -from ..types import FlakeName from ..nix import nix_shell +from ..types import FlakeName from .folders import sops_machines_folder, sops_users_folder diff --git a/pkgs/clan-cli/clan_cli/task_manager.py b/pkgs/clan-cli/clan_cli/task_manager.py index 227551d57..79b71a7eb 100644 --- a/pkgs/clan-cli/clan_cli/task_manager.py +++ b/pkgs/clan-cli/clan_cli/task_manager.py @@ -12,7 +12,7 @@ from pathlib import Path from typing import Any, Iterator, Optional, Type, TypeVar from uuid import UUID, uuid4 -from .custom_logger import get_caller, ThreadFormatter, CustomFormatter +from .custom_logger import ThreadFormatter, get_caller from .errors import ClanError @@ -83,8 +83,6 @@ class Command: raise ClanError(f"Failed to run command: {shlex.join(cmd)}") - - class TaskStatus(str, Enum): NOTSTARTED = "NOTSTARTED" RUNNING = "RUNNING" diff --git a/pkgs/clan-cli/clan_cli/vms/create.py b/pkgs/clan-cli/clan_cli/vms/create.py index b2b171a56..5e85d1d9c 100644 --- a/pkgs/clan-cli/clan_cli/vms/create.py +++ b/pkgs/clan-cli/clan_cli/vms/create.py @@ -13,7 +13,6 @@ from ..dirs import specific_flake_dir from ..nix import nix_build, nix_config, nix_shell from ..task_manager import BaseTask, Command, create_task from .inspect import VmConfig, inspect_vm -import pydantic class BuildVmTask(BaseTask): diff --git a/pkgs/clan-cli/clan_cli/webui/routers/machines.py b/pkgs/clan-cli/clan_cli/webui/routers/machines.py index b112959af..62b00f606 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/machines.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/machines.py @@ -10,9 +10,9 @@ from ...config.machine import ( set_config_for_machine, verify_machine_config, ) -from ...types import FlakeName from ...machines.create import create_machine as _create_machine from ...machines.list import list_machines as _list_machines +from ...types import FlakeName from ..api_outputs import ( ConfigResponse, Machine, diff --git a/pkgs/clan-cli/shell.nix b/pkgs/clan-cli/shell.nix index de011121c..62d28d2b6 100644 --- a/pkgs/clan-cli/shell.nix +++ b/pkgs/clan-cli/shell.nix @@ -22,39 +22,40 @@ mkShell { ]; shellHook = '' - tmp_path=$(realpath ./.direnv) - source=$(realpath .) - mkdir -p "$tmp_path/python/${pythonWithDeps.sitePackages}" + tmp_path=$(realpath ./.direnv) - # Install the package in editable mode - # This allows executing `clan` from within the dev-shell using the current - # version of the code and its dependencies. - ${pythonWithDeps.interpreter} -m pip install \ - --quiet \ - --disable-pip-version-check \ - --no-index \ - --no-build-isolation \ - --prefix "$tmp_path/python" \ - --editable $source + repo_root=$(realpath .) + mkdir -p "$tmp_path/python/${pythonWithDeps.sitePackages}" - rm -f clan_cli/nixpkgs clan_cli/webui/assets - ln -sf ${clan-cli.nixpkgs} clan_cli/nixpkgs - ln -sf ${ui-assets} clan_cli/webui/assets + # Install the package in editable mode + # This allows executing `clan` from within the dev-shell using the current + # version of the code and its dependencies. + ${pythonWithDeps.interpreter} -m pip install \ + --quiet \ + --disable-pip-version-check \ + --no-index \ + --no-build-isolation \ + --prefix "$tmp_path/python" \ + --editable $repo_root - export PATH="$tmp_path/python/bin:${checkScript}/bin:$PATH" - export PYTHONPATH="$source:$tmp_path/python/${pythonWithDeps.sitePackages}:" + rm -f clan_cli/nixpkgs clan_cli/webui/assets + ln -sf ${clan-cli.nixpkgs} clan_cli/nixpkgs + ln -sf ${ui-assets} clan_cli/webui/assets + + export PATH="$tmp_path/python/bin:${checkScript}/bin:$PATH" + export PYTHONPATH="$repo_root:$tmp_path/python/${pythonWithDeps.sitePackages}:" export PYTHONBREAKPOINT=ipdb.set_trace - export XDG_DATA_DIRS="$tmp_path/share''${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}" - export fish_complete_path="$tmp_path/share/fish/vendor_completions.d''${fish_complete_path:+:$fish_complete_path}" - mkdir -p \ - $tmp_path/share/fish/vendor_completions.d \ - $tmp_path/share/bash-completion/completions \ - $tmp_path/share/zsh/site-functions - register-python-argcomplete --shell fish clan > $tmp_path/share/fish/vendor_completions.d/clan.fish - register-python-argcomplete --shell bash clan > $tmp_path/share/bash-completion/completions/clan + export XDG_DATA_DIRS="$tmp_path/share''${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}" + export fish_complete_path="$tmp_path/share/fish/vendor_completions.d''${fish_complete_path:+:$fish_complete_path}" + mkdir -p \ + $tmp_path/share/fish/vendor_completions.d \ + $tmp_path/share/bash-completion/completions \ + $tmp_path/share/zsh/site-functions + register-python-argcomplete --shell fish clan > $tmp_path/share/fish/vendor_completions.d/clan.fish + register-python-argcomplete --shell bash clan > $tmp_path/share/bash-completion/completions/clan - ./bin/clan flakes create example_clan - ./bin/clan machines create example_machine example_clan + ./bin/clan flakes create example_clan + ./bin/clan machines create example_machine example_clan ''; } diff --git a/pkgs/clan-cli/tests/helpers/cli.py b/pkgs/clan-cli/tests/helpers/cli.py index d82c21d46..3deaef7fe 100644 --- a/pkgs/clan-cli/tests/helpers/cli.py +++ b/pkgs/clan-cli/tests/helpers/cli.py @@ -1,5 +1,4 @@ import argparse -import inspect import logging import shlex @@ -9,7 +8,6 @@ from clan_cli.custom_logger import get_caller log = logging.getLogger(__name__) - class Cli: def __init__(self) -> None: self.parser = create_parser(prog="clan") diff --git a/pkgs/clan-cli/tests/test_vms_api_create.py b/pkgs/clan-cli/tests/test_vms_api_create.py index 84b304196..fc2c7b3da 100644 --- a/pkgs/clan-cli/tests/test_vms_api_create.py +++ b/pkgs/clan-cli/tests/test_vms_api_create.py @@ -89,7 +89,7 @@ def generic_create_vm_test(api: TestClient, flake: Path, vm: str) -> None: assert ( data["status"] == "FINISHED" ), f"Expected to be finished, but got {data['status']} ({data})" - + @pytest.mark.skipif(not os.path.exists("/dev/kvm"), reason="Requires KVM") @pytest.mark.impure