From ef615a4b1e083679eba95ca21273ac0408395db3 Mon Sep 17 00:00:00 2001 From: Paul Farault Date: Fri, 11 Jul 2025 11:28:51 +0200 Subject: [PATCH] feat: rm tdp default-diff command --- tdp/cli/__main__.py | 2 - tdp/cli/commands/default_diff.py | 158 ----------------------------- tests/e2e/test_tdp_default_diff.py | 20 ---- 3 files changed, 180 deletions(-) delete mode 100644 tdp/cli/commands/default_diff.py delete mode 100644 tests/e2e/test_tdp_default_diff.py diff --git a/tdp/cli/__main__.py b/tdp/cli/__main__.py index 4ed44822..24c1a0b4 100644 --- a/tdp/cli/__main__.py +++ b/tdp/cli/__main__.py @@ -10,7 +10,6 @@ from tdp.cli.commands.browse import browse from tdp.cli.commands.dag import dag -from tdp.cli.commands.default_diff import default_diff from tdp.cli.commands.deploy import deploy from tdp.cli.commands.init import init from tdp.cli.commands.ops import ops @@ -58,7 +57,6 @@ def cli(log_level: str): def main(): cli.add_command(browse) cli.add_command(dag) - cli.add_command(default_diff) cli.add_command(deploy) cli.add_command(init) cli.add_command(ops) diff --git a/tdp/cli/commands/default_diff.py b/tdp/cli/commands/default_diff.py deleted file mode 100644 index 102313ee..00000000 --- a/tdp/cli/commands/default_diff.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2022 TOSIT.IO -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -import difflib -import pprint -from collections import OrderedDict -from pathlib import Path -from typing import TYPE_CHECKING, Optional - -import click - -from tdp.cli.params import collections_option, vars_option -from tdp.core.constants import DEFAULT_VARS_DIRECTORY_NAME - -if TYPE_CHECKING: - from tdp.core.collections import Collections - - -@click.command() -@click.argument("service", required=False) -@collections_option -@vars_option -def default_diff(collections: Collections, vars: Path, service: Optional[str] = None): - """Difference between tdp_vars and defaults.""" - - from tdp.core.variables import ClusterVariables - - cluster_variables = ClusterVariables.get_cluster_variables(collections, vars) - - if service: - service_diff(collections, cluster_variables[service]) - else: - for service, service_variables in cluster_variables.items(): - service_diff(collections, service_variables) - - -def service_diff(collections: Collections, service): - """Computes the difference between the default variables from a service, and the variables from your service variables inside your tdp_vars. - - Args: - collections (Collections): Collections object. - service (ServiceManager): Service to compare's manager. - """ - from ansible.utils.vars import merge_hash - - from tdp.core.variables import Variables - - # key: filename with extension, value: PosixPath(filepath) - default_service_vars_paths = OrderedDict() - for collection_default_vars_dir in collections.default_vars_dirs.values(): - service_default_vars_dir = collection_default_vars_dir / service.name - if not service_default_vars_dir.exists(): - continue - for default_vars_path in service_default_vars_dir.iterdir(): - default_service_vars_paths.setdefault(default_vars_path.name, []).append( - default_vars_path - ) - - for ( - default_service_vars_filename, - default_service_vars_filepaths, - ) in default_service_vars_paths.items(): - tdp_vars_service_vars_filepath = service.path / default_service_vars_filename - if not tdp_vars_service_vars_filepath.exists(): - click.echo( - f"{service.name}: {default_service_vars_filename}\n" - + click.style( - f"{tdp_vars_service_vars_filepath} does not exist", fg="red" - ) - ) - continue - service_varfile = {} - with Variables(tdp_vars_service_vars_filepath).open("r") as service_variables: - service_varfile = service_variables.copy() - - default_service_varfile = {} - for default_service_vars_filepath in default_service_vars_filepaths: - with Variables(default_service_vars_filepath).open( - "r" - ) as default_variables: - default_service_varfile = merge_hash( - default_service_varfile, default_variables - ) - - service_varfile_content = pprint.pformat(service_varfile).splitlines() - default_service_varfile_content = pprint.pformat( - default_service_varfile - ).splitlines() - - # left_path = tdp_vars_defaults/{service}/{filename} - # multiple paths if merged from multiple collections - paths = [ - str( - filepath.relative_to(find_parent(filepath, DEFAULT_VARS_DIRECTORY_NAME)) - ) - for filepath in default_service_vars_filepaths - ] - context = "" if len(paths) < 2 else " <-- merged" - left_path = ",".join(paths) + context - - # right_path = {your_tdp_vars}/{service}/{filename} - right_path = str( - tdp_vars_service_vars_filepath.relative_to(service.path.parent.parent) - ) - compute_and_print_difference( - service_name=service.name, - left_content=default_service_varfile_content, - right_content=service_varfile_content, - left_path=left_path, - right_path=right_path, - filename=default_service_vars_filename, - ) - - -def compute_and_print_difference( - service_name, filename, left_content, right_content, left_path, right_path -): - """Computes differences between 2 files, and outputs them. - - Args: - service_name (str): Name of the service. - filename (str): Name of the file to display. - left_content (Iterator[str]): Content to compare from. - right_content (Iterator[str]): Content to compare to. - left_path (str): Filename to compare from, to use as contextual information. - right_path (str): Filename to compare to, to use as contextual information. - """ - differences = difflib.context_diff( - left_content, - right_content, - fromfile=left_path, - tofile=right_path, - n=1, - ) - click.echo( - f"{service_name}: {filename}\n" - + ("\n".join(color_line(line) for line in differences) or "None") - ) - - -def color_line(line: str): - if line.startswith("! "): - return click.style(line, fg="yellow") - if line.startswith("- "): - return click.style(line, fg="red") - if line.startswith("+ "): - return click.style(line, fg="green") - - return line - - -def find_parent(path, name): - parent_path = path - while parent_path.name != name and parent_path is not None: - parent_path = parent_path.parent - return parent_path.parent.parent diff --git a/tests/e2e/test_tdp_default_diff.py b/tests/e2e/test_tdp_default_diff.py deleted file mode 100644 index 37d7eda7..00000000 --- a/tests/e2e/test_tdp_default_diff.py +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2022 TOSIT.IO -# SPDX-License-Identifier: Apache-2.0 - -from pathlib import Path - -from click.testing import CliRunner - -from tdp.cli.commands.default_diff import default_diff - - -def test_tdp_default_diff(collection_path: Path, vars: Path): - args = [ - "--collection-path", - collection_path, - "--vars", - vars, - ] - runner = CliRunner() - result = runner.invoke(default_diff, args) - assert result.exit_code == 0, result.output