From 8455133bbd6d8151ba3c6928a6bff9a2734bda1d Mon Sep 17 00:00:00 2001 From: NVolcz Date: Sun, 10 May 2020 10:44:54 +0200 Subject: [PATCH 1/3] 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 From 96592e39234248174a266addcf696bc2ccad1a0d Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 29 Jun 2020 08:43:39 -0700 Subject: [PATCH 2/3] format with black --- .../azure-identity/azure/identity/_credentials/azure_cli.py | 4 ++-- .../azure/identity/aio/_credentials/azure_cli.py | 3 ++- 2 files changed, 4 insertions(+), 3 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 4a9423b698d4..c5fe99a1119c 100644 --- a/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py +++ b/sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py @@ -108,8 +108,8 @@ def _run_command(command): "stderr": subprocess.STDOUT, "cwd": working_directory, "universal_newlines": True, - "env": dict(os.environ, AZURE_CORE_NO_COLOR="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 bc275737389b..56c025680db3 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 @@ -69,7 +69,8 @@ async def _run_command(command): try: proc = await asyncio.create_subprocess_exec( - *args, stdout=asyncio.subprocess.PIPE, + *args, + stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, cwd=working_directory, env=dict(os.environ, AZURE_CORE_NO_COLOR="true") From 03b0ad6bb7bfed366c9e90f795d00b4dedb4216d Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 29 Jun 2020 08:51:51 -0700 Subject: [PATCH 3/3] update changelog --- sdk/identity/azure-identity/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/identity/azure-identity/CHANGELOG.md b/sdk/identity/azure-identity/CHANGELOG.md index 3223ae7d8713..2b5988417260 100644 --- a/sdk/identity/azure-identity/CHANGELOG.md +++ b/sdk/identity/azure-identity/CHANGELOG.md @@ -1,6 +1,9 @@ # Release History ## 1.4.0b6 (Unreleased) +- `AzureCliCredential` no longer raises an exception due to unexpected output + from the CLI when run by PyCharm (thanks @NVolcz) + ([#11362](https://github.com/Azure/azure-sdk-for-python/pull/11362)) - The async `AzureCliCredential` correctly invokes `/bin/sh` ([#12048](https://github.com/Azure/azure-sdk-for-python/issues/12048))