nailo2c commented on code in PR #63901: URL: https://github.com/apache/airflow/pull/63901#discussion_r3029393234
########## dev/breeze/src/airflow_breeze/utils/reproduce_ci.py: ########## @@ -0,0 +1,221 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Helpers for printing local reproduction instructions in CI logs.""" + +from __future__ import annotations + +import os +import shlex +from dataclasses import dataclass +from typing import TYPE_CHECKING + +import click +from click.core import ParameterSource +from rich.markup import escape + +from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY +from airflow_breeze.utils.console import get_console +from airflow_breeze.utils.run_utils import commit_sha + +if TYPE_CHECKING: + from airflow_breeze.params.build_ci_params import BuildCiParams + from airflow_breeze.params.shell_params import ShellParams + +# Options that are side-effect-only or not meaningful for reproduction (safety net; +# expose_value=False options like --verbose/--dry-run/--answer are already excluded +# automatically because they don't appear in ctx.params). +_EXCLUDED_PARAMS: frozenset[str] = frozenset( + { + "verbose", + "dry_run", + "answer", + "include_success_outputs", + "debug_resources", + "skip_cleanup", + } +) + +# These sources represent values explicitly provided by the user or CI. +_EXPLICIT_SOURCES: frozenset[ParameterSource] = frozenset( + { + ParameterSource.COMMANDLINE, + ParameterSource.ENVIRONMENT, + ParameterSource.PROMPT, + } +) + + +@dataclass +class ReproductionCommand: + argv: list[str] + comment: str | None = None + + +def build_reproduction_command_from_context( + ctx: click.Context, + *, + comment: str = "Run the same Breeze command locally", +) -> ReproductionCommand: + """Reconstruct the CLI invocation from the current click Context. + + Iterates over every parameter defined on the command, uses + ``ctx.get_parameter_source()`` to identify explicitly-provided values + (COMMANDLINE / ENVIRONMENT / PROMPT), and emits only those. DEFAULT + and DEFAULT_MAP values are omitted to keep the output concise. + + This removes the need for per-command builder functions. + """ + argv: list[str] = ctx.command_path.split() + + for param in ctx.command.params: + if not getattr(param, "expose_value", True): + continue + if param.name is None or param.name in _EXCLUDED_PARAMS: + continue + + value = ctx.params.get(param.name) + source = ctx.get_parameter_source(param.name) + + if isinstance(param, click.Argument): + continue # collected after options + + if not isinstance(param, click.Option): + continue + + # Flag pair (e.g. --force-sa-warnings/--no-force-sa-warnings): + # emit the appropriate side only when explicitly provided. + if param.is_flag and param.secondary_opts: + if source in _EXPLICIT_SOURCES: + flag = param.opts[0] if value else param.secondary_opts[0] Review Comment: I think this suggestion is right, the long-form is much easier to understand. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
