Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions src/azure-cli-core/azure/cli/core/tests/test_launchers.py

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,21 @@ def test_get_parent_proc_name(self, mock_process_type):
parent2.name.return_value = "bash"
self.assertEqual(_get_parent_proc_name(), "pwsh")

@mock.patch("psutil.Process")
def test_get_parent_proc_name_with_bound_launcher(self, mock_process_type):
for wrappers in (["az-cli.exe"], ["python.exe", "az-cli.exe"]):
for shell in ("cmd.exe", "powershell.exe", "pwsh.exe", "bash.exe"):
with self.subTest(wrappers=wrappers, shell=shell):
parent = mock_process_type.return_value
shells = ["cmd.exe", shell] if shell in ("powershell.exe", "pwsh.exe") else [shell]
for name in wrappers + shells:
child = parent
parent = mock.Mock()
parent.name.return_value = name
child.parent.return_value = parent
parent.parent.return_value = None
self.assertEqual(_get_parent_proc_name(), shell)

def test_cli_run_cmd(self):
cmd = ["echo", "abc"]
if platform.system().lower() == "windows":
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,8 @@ def _get_parent_proc_name():
try:
parent = psutil.Process(os.getpid()).parent()

# On Windows, when CLI is run inside a virtual env, there will be 2 python.exe.
if parent and parent.name().lower() == 'python.exe':
# Skip the Windows virtualenv Python and interpreter-bound PyPI launcher processes.
while parent and parent.name().lower() in ('python.exe', 'az-cli.exe'):
parent = parent.parent()

if parent:
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ include azure_cli_bdist_wheel.py
include azure/__init__.py
include azure/cli/__init__.py
include azure/cli/__main__.py

include azure/cli/_launcher.py
25 changes: 14 additions & 11 deletions src/azure-cli/az.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@echo off
setlocal

SET PYTHONPATH=%~dp0\src;%PYTHONPATH%
SET AZ_INSTALLER=PIP

IF EXIST "%~dp0\python.exe" (
"%~dp0\python.exe" -m azure.cli %*
) ELSE (
python -m azure.cli %*
)
@echo off
setlocal DisableDelayedExpansion

SET "PYTHONPATH=%~dp0\src;%PYTHONPATH%"
SET AZ_INSTALLER=PIP

IF NOT EXIST "%~dp0az-cli.exe" (
>&2 echo Azure CLI launcher error: "%~dp0az-cli.exe" was not found.
>&2 echo Reinstall Azure CLI using the same installer and environment, then reopen your shell.
EXIT /B 1
)

"%~dp0az-cli.exe" %*
EXIT /B %ERRORLEVEL%
11 changes: 11 additions & 0 deletions src/azure-cli/azure/cli/_launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import runpy


def main():
"""Run the complete CLI module from the interpreter-bound Windows launcher."""
runpy.run_module('azure.cli.__main__', run_name='__main__', alter_sys=True)
5 changes: 3 additions & 2 deletions src/azure-cli/azure_cli_bdist_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def initialize_options(self):
def build_packages(self):
super().build_packages()
if self.extra_build_source_files:
package, module, module_file = self.extra_build_source_files.split(',')
self.build_module(module, module_file, package)
for source in self.extra_build_source_files.strip().splitlines():
package, module, module_file = source.strip().split(',')
self.build_module(module, module_file, package)


cmdclass = {
Expand Down
4 changes: 3 additions & 1 deletion src/azure-cli/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[build_py]
extra_build_source_files=azure.cli,__main__,azure/cli/__main__.py
extra_build_source_files=
azure.cli,__main__,azure/cli/__main__.py
azure.cli,_launcher,azure/cli/_launcher.py
4 changes: 4 additions & 0 deletions src/azure-cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@
'az.bat',
'azps.ps1'
],
# A distinct helper name keeps installers from replacing the extensionless az script.
entry_points={
'console_scripts': ['az-cli=azure.cli._launcher:main']
},
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests", "azure", "azure.cli"]),
install_requires=DEPENDENCIES,
python_requires='>=3.10.0',
Expand Down
Loading