From f9aaa5ad39b1db8e249566b9b63cd879f8730e8b Mon Sep 17 00:00:00 2001 From: DavHau Date: Wed, 9 Aug 2023 17:59:07 +0200 Subject: [PATCH] clan-config: unbreak + include data files --- pkgs/clan-cli/clan_cli/__init__.py | 10 +++------- pkgs/clan-cli/clan_cli/config/__init__.py | 12 ++++++------ pkgs/clan-cli/clan_cli/config/jsonschema | 1 + pkgs/clan-cli/default.nix | 17 +++++++++++------ pkgs/clan-cli/pyproject.toml | 4 ++-- 5 files changed, 23 insertions(+), 21 deletions(-) create mode 120000 pkgs/clan-cli/clan_cli/config/jsonschema diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 32144ec6..48fb292d 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -1,7 +1,7 @@ import argparse import sys -from . import admin, secrets +from . import admin, config, secrets from .errors import ClanError from .ssh import cli as ssh_cli @@ -20,12 +20,8 @@ def main() -> None: parser_admin = subparsers.add_parser("admin") admin.register_parser(parser_admin) - # Currently broken - # parser_config = subparsers.add_parser("config") - # try: - # config.register_parser(parser_config) - # except subprocess.CalledProcessError as e: - # warn(f"The config command does not work in the nix sandbox: {e}") + parser_config = subparsers.add_parser("config") + config.register_parser(parser_config) parser_ssh = subparsers.add_parser("ssh", help="ssh to a remote machine") ssh_cli.register_parser(parser_ssh) diff --git a/pkgs/clan-cli/clan_cli/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index c36e80c3..090526ab 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -1,7 +1,6 @@ # !/usr/bin/env python3 import argparse import json -import os import subprocess import sys from pathlib import Path @@ -9,7 +8,7 @@ from typing import Any, Optional, Type, Union from clan_cli.errors import ClanError -CLAN_FLAKE = os.getenv("CLAN_FLAKE") +script_dir = Path(__file__).parent class Kwargs: @@ -23,14 +22,14 @@ class Kwargs: def schema_from_module_file( - file: Union[str, Path] = "./tests/config/example-interface.nix", + file: Union[str, Path] = f"{script_dir}/jsonschema/example-schema.json", ) -> dict[str, Any]: absolute_path = Path(file).absolute() # define a nix expression that loads the given module file using lib.evalModules nix_expr = f""" let lib = import ; - slib = import {CLAN_FLAKE}/lib/jsonschema.nix {{inherit lib;}}; + slib = import {script_dir}/jsonschema {{inherit lib;}}; in slib.parseModule {absolute_path} """ @@ -44,7 +43,7 @@ def schema_from_module_file( def register_parser( parser: argparse.ArgumentParser, - file: Path = Path("./tests/config/example-interface.nix"), + file: Path = Path(f"{script_dir}/jsonschema/example-schema.json"), ) -> None: if file.name.endswith(".nix"): schema = schema_from_module_file(file) @@ -74,7 +73,8 @@ def _register_parser( "string": str, } - parser = argparse.ArgumentParser(description=schema.get("description")) + if parser is None: + parser = argparse.ArgumentParser(description=schema.get("description")) subparsers = parser.add_subparsers( title="more options", diff --git a/pkgs/clan-cli/clan_cli/config/jsonschema b/pkgs/clan-cli/clan_cli/config/jsonschema new file mode 120000 index 00000000..3df5a775 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/config/jsonschema @@ -0,0 +1 @@ +../../../../lib/jsonschema \ No newline at end of file diff --git a/pkgs/clan-cli/default.nix b/pkgs/clan-cli/default.nix index f9569ef7..d590bc77 100644 --- a/pkgs/clan-cli/default.nix +++ b/pkgs/clan-cli/default.nix @@ -40,11 +40,16 @@ python3.pkgs.buildPythonPackage { ]; propagatedBuildInputs = dependencies; + preBuild = '' + rm ./clan_cli/config/jsonschema + cp -r ${self}/lib/jsonschema ./clan_cli/config/jsonschema + ''; + passthru.tests = { clan-mypy = runCommand "clan-mypy" { } '' - cp -r ${./.} ./src - chmod +w -R ./src - cd src + cp -r ${self} ./flake + chmod +w -R ./flake + cd ./flake/pkgs/clan-cli ${checkPython}/bin/mypy . touch $out ''; @@ -52,9 +57,9 @@ python3.pkgs.buildPythonPackage { { nativeBuildInputs = [ age zerotierone bubblewrap sops nix ]; } '' - cp -r ${./.} ./src - chmod +w -R ./src - cd src + cp -r ${self} ./flake + chmod +w -R ./flake + cd ./flake/pkgs/clan-cli ${checkPython}/bin/python -m pytest ./tests touch $out ''; diff --git a/pkgs/clan-cli/pyproject.toml b/pkgs/clan-cli/pyproject.toml index f84481b6..54850017 100644 --- a/pkgs/clan-cli/pyproject.toml +++ b/pkgs/clan-cli/pyproject.toml @@ -8,8 +8,8 @@ description = "cLAN CLI tool" dynamic = [ "version" ] scripts = { clan = "clan_cli:main" } -[tool.setuptools.packages] -find = {} +[tool.setuptools.package-data] +clan_cli = ["config/jsonschema/*"] [tool.pytest.ini_options] addopts = "--cov . --cov-report term --cov-report html:.reports/html --no-cov-on-fail"