From 8455133bbd6d8151ba3c6928a6bff9a2734bda1d Mon Sep 17 00:00:00 2001 From: NVolcz Date: Sun, 10 May 2020 10:44:54 +0200 Subject: [PATCH] Instruct Azure CLI to not color output The Azure CLI supports colored output by using colorama which resets the color after execution by printing "[0m" if the terminal supports color. This can in some cases cause the AzureCliCredential to fail for example when developing in PyCharm: https://github.com/Azure/azure-cli/issues/9903 Azure CLI allows color output to be disabled by setting the environment variable:AZURE_CORE_NO_COLOR. The PR in azure-cli: https://github.com/Azure/azure-cli/pull/12601 --- .../azure/identity/_credentials/azure_cli.py | 7 ++++++- .../azure/identity/aio/_credentials/azure_cli.py | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py b/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py index 07687f8d32c1..4a9423b698d4 100644 --- a/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py +++ b/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py @@ -104,7 +104,12 @@ def _run_command(command): try: working_directory = get_safe_working_dir() - kwargs = {"stderr": subprocess.STDOUT, "cwd": working_directory, "universal_newlines": True} + kwargs = { + "stderr": subprocess.STDOUT, + "cwd": working_directory, + "universal_newlines": True, + "env": dict(os.environ, AZURE_CORE_NO_COLOR="true") + } if platform.python_version() >= "3.3": kwargs["timeout"] = 10 diff --git a/sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py b/sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py index a562a7831b9f..bc275737389b 100644 --- a/sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py +++ b/sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py @@ -4,6 +4,7 @@ # ------------------------------------ import asyncio import sys +import os from azure.core.exceptions import ClientAuthenticationError from .._credentials.base import AsyncCredentialBase @@ -68,7 +69,10 @@ async def _run_command(command): try: proc = await asyncio.create_subprocess_exec( - *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, cwd=working_directory + *args, stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.STDOUT, + cwd=working_directory, + env=dict(os.environ, AZURE_CORE_NO_COLOR="true") ) except OSError as ex: # failed to execute 'cmd' or '/bin/sh'; CLI may or may not be installed