This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch v2-3-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit be1b5b706fe9c7984f8a5e5c66d43876c1e4f0ca
Author: Jarek Potiuk <[email protected]>
AuthorDate: Tue May 3 15:03:01 2022 +0200

    Improve verbose output of Breeze (#23446)
    
    When you add --verbose or --dry-run options to breeze it will
    print the commands it is executing (or is supposed to in dry-run
    mode). The output contains environment variables as they
    often contain crucial information to execute the command (for
    example in docker-compose run commands it contains COMPOSE_FILE
    variable which is the list of compose files that are used). This
    is done in a fashion that you can copy the whole command and
    execute it, but it very unfriendly for visual inspection as
    all the variables were printed in one line and in semi-random
    order and also the variables contained often all system variables
    set by the shell before.
    
    This change keeps the proerty of "we can copy&paste the command
    and run it" but it improves the visual aspect of it:
    
    1) each env variable is kept in one line
    2) first all system variables are printed and then variables that
       were specifically added for this command
    3) variables in each group are sorted alphabetically which helps
       in finding the variable you are looking for when you visually
       inspect the output.
    
    (cherry picked from commit 6f146e721c81e9304bf7c0af66fc3d203d902dab)
---
 dev/breeze/src/airflow_breeze/utils/run_utils.py | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/dev/breeze/src/airflow_breeze/utils/run_utils.py 
b/dev/breeze/src/airflow_breeze/utils/run_utils.py
index 92cf8c8c76..b215cb523b 100644
--- a/dev/breeze/src/airflow_breeze/utils/run_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/run_utils.py
@@ -24,7 +24,7 @@ import sys
 from distutils.version import StrictVersion
 from functools import lru_cache
 from pathlib import Path
-from typing import List, Mapping, Optional, Union
+from typing import Dict, List, Mapping, Optional, Union
 
 from airflow_breeze.utils.console import get_console
 from airflow_breeze.utils.path_utils import AIRFLOW_SOURCES_ROOT
@@ -66,10 +66,7 @@ def run_command(
     workdir: str = str(cwd) if cwd else os.getcwd()
     if verbose or dry_run:
         command_to_print = ' '.join(shlex.quote(c) for c in cmd)
-        # if we pass environment variables to execute, then
-        env_to_print = ' '.join(f'{key}="{val}"' for (key, val) in 
env.items()) if env else ''
-        if env_to_print:
-            env_to_print += ' '
+        env_to_print = get_environments_to_print(env)
         get_console().print(f"\n[info]Working directory {workdir} [/]\n")
         # Soft wrap allows to copy&paste and run resulting output as it has no 
hard EOL
         get_console().print(f"\n[info]{env_to_print}{command_to_print}[/]\n", 
soft_wrap=True)
@@ -103,6 +100,23 @@ def run_command(
         return ex
 
 
+def get_environments_to_print(env: Optional[Mapping[str, str]]):
+    if not env:
+        return "# No environment variables \\\n"
+    system_env: Dict[str, str] = {}
+    my_env: Dict[str, str] = {}
+    for key, val in env.items():
+        if os.environ.get(key) == val:
+            system_env[key] = val
+        else:
+            my_env[key] = val
+    env_to_print = ''.join(f'{key}="{val}" \\\n' for (key, val) in 
sorted(system_env.items()))
+    env_to_print += r"""\
+"""
+    env_to_print += ''.join(f'{key}="{val}" \\\n' for (key, val) in 
sorted(my_env.items()))
+    return env_to_print
+
+
 def assert_pre_commit_installed(verbose: bool):
     """
     Check if pre-commit is installed in the right version.

Reply via email to