All checks were successful
buildbot/nix-build .#checks.aarch64-darwin.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-iso-installer Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-flash-installer Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-webview-ui Build done.
buildbot/nix-build .#checks.x86_64-linux.check-for-breakpoints Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-iso-installer Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-flash-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-ts-api Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-cli-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-app-no-breakpoints Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-apk Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-archlinux Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-deb Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-installer-rpm Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-age Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-bash Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-e2fsprogs Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-nix Build done.
buildbot/nix-build .#checks.x86_64-linux."clan-dep-python3.11-mypy" Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-sshpass Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-zbar Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-openssh Build done.
buildbot/nix-build .#checks.aarch64-linux.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.package-deploy-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zerotier-members Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zt-tcp-relay Build done.
buildbot/nix-build .#checks.x86_64-linux.package-tea-create-pr Build done.
buildbot/nix-build .#checks.aarch64-darwin.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.package-module-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-fakeroot Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-cli Build done.
buildbot/nix-build .#checks.x86_64-linux.renderClanOptions Build done.
buildbot/nix-build .#checks.x86_64-linux.package-default Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-git Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-docs Build done.
buildbot/nix-build .#checks.x86_64-linux."clan-dep-python3.11-qemu" Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-rsync Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-sops Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-dep-tor Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-test-backup Build done.
buildbot/nix-build .#checks.x86_64-linux.package-docs Build done.
buildbot/nix-build .#checks.x86_64-linux.lib-jsonschema-example-valid Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-app-pytest Build done.
buildbot/nix-build .#checks.x86_64-linux.borgbackup Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-default Build done.
buildbot/nix-build .#checks.x86_64-linux.container Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-cli Build done.
buildbot/nix-build .#checks.x86_64-linux.deltachat Build done.
buildbot/nix-build .#checks.x86_64-linux.matrix-synapse Build done.
buildbot/nix-build .#checks.x86_64-linux.package-editor Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-test_install_machine Build done.
buildbot/nix-build .#checks.x86_64-linux.module-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.treefmt Build done.
buildbot/nix-build .#checks.x86_64-linux.package-impure-checks Build done.
buildbot/nix-build .#checks.x86_64-linux.package-merge-after-ci Build done.
buildbot/nix-build .#checks.x86_64-linux.package-moonlight-sunshine-accept Build done.
buildbot/nix-build .#checks.x86_64-linux.lib-jsonschema-nix-unit-tests Build done.
buildbot/nix-build .#checks.x86_64-linux.package-pending-reviews Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-flash-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.zt-tcp-relay Build done.
buildbot/nix-build .#checks.x86_64-linux.package-zerotierone Build done.
buildbot/nix-build .#checks.x86_64-linux.package-function-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.secrets Build done.
buildbot/nix-build .#checks.x86_64-linux.postgresql Build done.
buildbot/nix-build .#checks.x86_64-linux.package-module-schema Build done.
buildbot/nix-build .#checks.x86_64-linux.template-minimal Build done.
buildbot/nix-build .#checks.x86_64-linux.package-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.wayland-proxy-virtwl Build done.
buildbot/nix-build .#checks.x86_64-linux.syncthing Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-pytest-without-core Build done.
buildbot/nix-build .#checks.x86_64-linux.package-webview-ui Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-app Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gui-install-test-ubuntu-22-04 Build done.
buildbot/nix-build .#checks.x86_64-linux.test-backups Build done.
checks / checks-impure (pull_request) Successful in 1m57s
buildbot/nix-build .#checks.x86_64-linux.nixos-iso-installer Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-app Build done.
buildbot/nix-build .#checks.x86_64-linux.clan-pytest-with-core Build done.
buildbot/nix-build .#checks.x86_64-linux.flash Build done.
buildbot/nix-build .#checks.x86_64-linux.test-installation Build done.
buildbot/nix-eval Build done.
120 lines
3.7 KiB
Python
120 lines
3.7 KiB
Python
# !/usr/bin/env python3
|
|
import argparse
|
|
import dataclasses
|
|
import datetime
|
|
import json
|
|
import logging
|
|
from typing import Any
|
|
|
|
from clan_cli.clan.inspect import FlakeConfig, inspect_flake
|
|
from clan_cli.machines.list import list_machines
|
|
|
|
from ..clan_uri import ClanURI
|
|
from ..dirs import user_history_file
|
|
from ..errors import ClanError
|
|
from ..locked_open import read_history_file, write_history_file
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class HistoryEntry:
|
|
last_used: str
|
|
flake: FlakeConfig
|
|
settings: dict[str, Any] = dataclasses.field(default_factory=dict)
|
|
|
|
def __post_init__(self) -> None:
|
|
if isinstance(self.flake, dict):
|
|
self.flake = FlakeConfig(**self.flake)
|
|
|
|
|
|
def _merge_dicts(d1: dict, d2: dict) -> dict:
|
|
# create a new dictionary that copies d1
|
|
merged = dict(d1)
|
|
# iterate over the keys and values of d2
|
|
for key, value in d2.items():
|
|
# if the key is in d1 and both values are dictionaries, merge them recursively
|
|
if key in d1 and isinstance(d1[key], dict) and isinstance(value, dict):
|
|
merged[key] = _merge_dicts(d1[key], value)
|
|
# otherwise, update the value of the key in the merged dictionary
|
|
else:
|
|
merged[key] = value
|
|
# return the merged dictionary
|
|
return merged
|
|
|
|
|
|
def list_history() -> list[HistoryEntry]:
|
|
logs: list[HistoryEntry] = []
|
|
if not user_history_file().exists():
|
|
return []
|
|
|
|
try:
|
|
parsed = read_history_file()
|
|
for i, p in enumerate(parsed.copy()):
|
|
# Everything from the settings dict is merged into the flake dict, and can override existing values
|
|
parsed[i] = _merge_dicts(p, p.get("settings", {}))
|
|
logs = [HistoryEntry(**p) for p in parsed]
|
|
except (json.JSONDecodeError, TypeError) as ex:
|
|
raise ClanError(f"History file at {user_history_file()} is corrupted") from ex
|
|
|
|
return logs
|
|
|
|
|
|
def new_history_entry(url: str, machine: str) -> HistoryEntry:
|
|
flake = inspect_flake(url, machine)
|
|
flake.flake_url = str(flake.flake_url)
|
|
return HistoryEntry(
|
|
flake=flake,
|
|
last_used=datetime.datetime.now().isoformat(),
|
|
)
|
|
|
|
|
|
def add_all_to_history(uri: ClanURI) -> list[HistoryEntry]:
|
|
history = list_history()
|
|
new_entries: list[HistoryEntry] = []
|
|
for machine in list_machines(uri.get_url()):
|
|
new_entry = _add_maschine_to_history_list(uri.get_url(), machine, history)
|
|
new_entries.append(new_entry)
|
|
write_history_file(history)
|
|
return new_entries
|
|
|
|
|
|
def add_history(uri: ClanURI) -> HistoryEntry:
|
|
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
|
history = list_history()
|
|
new_entry = _add_maschine_to_history_list(uri.get_url(), uri.machine.name, history)
|
|
write_history_file(history)
|
|
return new_entry
|
|
|
|
|
|
def _add_maschine_to_history_list(
|
|
uri_path: str, uri_machine: str, entries: list[HistoryEntry]
|
|
) -> HistoryEntry:
|
|
for new_entry in entries:
|
|
if (
|
|
new_entry.flake.flake_url == str(uri_path)
|
|
and new_entry.flake.flake_attr == uri_machine
|
|
):
|
|
new_entry.last_used = datetime.datetime.now().isoformat()
|
|
return new_entry
|
|
|
|
new_entry = new_history_entry(uri_path, uri_machine)
|
|
entries.append(new_entry)
|
|
return new_entry
|
|
|
|
|
|
def add_history_command(args: argparse.Namespace) -> None:
|
|
if args.all:
|
|
add_all_to_history(args.uri)
|
|
else:
|
|
add_history(args.uri)
|
|
|
|
|
|
# takes a (sub)parser and configures it
|
|
def register_add_parser(parser: argparse.ArgumentParser) -> None:
|
|
parser.add_argument("uri", type=ClanURI, help="Path to the flake", default=".")
|
|
parser.add_argument(
|
|
"--all", help="Add all machines", default=False, action="store_true"
|
|
)
|
|
parser.set_defaults(func=add_history_command)
|