o
    N                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dZd	Zd
ZdZde dZde Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'efd(d)Zd*d+ ZdS ),zPython installers for gcloud.    N)config)execution_utils)
console_io)files)	platformsz3.13)      zpython-3.13.7-macos11.tar.gzz/opt/homebrew/binz./Library/Frameworks/Python.framework/Versions//z1https://dl.google.com/dl/cloudsdk/channels/rapid/c                  C   s   t  j} tj| r| S d S )N)r   Pathsvirtualenv_dirospathisdir)env_dir r   G/tmp/google-cloud-sdk/lib/googlecloudsdk/core/updater/python_manager.py_VirtualEnvPath(   s   
r   c                 C   s   |  dddd|g d S )Nr   
virtualenvcreatez--python-to-useExecute)clipython_to_user   r   r   _CreateVirtualEnv0   s   
r   c                 C   s,   t d| d | g d t| | d S )NzVirtual env already exists at z$. Deleting so we can create new one.)r   r   delete)printr   r   )r   r   existing_env_dirr   r   r   _RecreateVirtualEnv5   s   r   c                 C      |  g d d S )N)r   r   updater   r   r   r   r   _UpdateVirtualEnv<      r!   c                 C   r   )N)r   r   enabler   r    r   r   r   _EnableVirtualEnv@   r"   r$   c                 C   s   z3ddl m} |g }t }|r!tjdd tkr!t|| | n|r(t| nt	||  t
| W dS  tyA   td Y dS w )zEEnables virtual environment with new python version and dependencies.r   )gcloud_mainN   z$Failed to enable virtual environment)googlecloudsdkr%   	CreateCLIr   sysversion_infoPYTHON_VERSION_INFOr   r!   r   r$   ImportErrorr   )r   r%   r   r   r   r   r   UpdatePythonDependenciesD   s   


r-   c                   C   s   t jtodt v S )Nhomebrew)r   r   r   HOMEBREW_BINr   
GcloudPathr   r   r   r   _IsHomebrewInstalledX   s   r1   c                 C   s   dt  d|  dS )Nz(Recommended) Python z installation detected in z!, install required modules (Y/n)?)PYTHON_VERSION)python_install_pathr   r   r   _PromptPythonUpdate\   s   r4   c                   C   s   t  r	dt dS dt dS )NzHomebrew install Python ?zDownload and run Python z installer?)r1   r2   r   r   r   r   _PromptPythonInstallb   s   r6   c                  C   sP   t  dt } td|  d tj| ddd}|dkr&d|  d	t d
S dS )zHMake sure python version is correct for user using gcloud with homebrew.z/brew install python@z	Running "z". Tno_exitr   "z%" failed. Please brew install python@z
 manually.N)r/   r2   r   r   Execsplit)brew_install	exit_coder   r   r   _BrewInstallPythoni   s   r?   c               	   C   s0  t dt d t  t } t| a dddtg}tj|dd}|dkr6	 W d	   W d	   d
S tjddt	gdd}|dkrS	 W d	   W d	   dS tjg ddd}|dkro	 W d	   W d	   dS W d	   n1 syw   Y  W d	   d	S W d	   d	S 1 sw   Y  d	S )*Optionally install Python on Mac machines.zRunning Python z4 installer, you may be prompted for sudo password...curlz--silentz-OTr8   r   Nz#Failed to download Python installertarz-xfz"Failed to extract Python installer)sudo	installerz-targetr	   z-pkgz./python-3.13.7-macos11.pkgzInstaller failed.)
r   r2   %PromptAndInstallXcodeCommandLineToolsr   TemporaryDirectoryChDirMACOS_PYTHON_URLr   r;   MACOS_PYTHON)tempdir	curl_argsr>   r   r   r   _MacInstallPythonv   sB   




rL   c                  C   s   t j t jjkrdS tdt d t } | r t dt }nt d}t	j
|}|r2t|}nt }tj|dds>dS |sI| rEt nt }nd}|s\|t	jd< td	 t| dS td
|  dS )r@   Nz)
Google Cloud CLI works best with Python z and certain modules.
z/pythonzbin/python3Tprompt_stringdefaultCLOUDSDK_PYTHONzSetting up virtual environmentz!Failed to install Python. Error: )r   OperatingSystemCurrentMACOSXr   r2   r1   r/   MACOS_PYTHON_INSTALL_PATHr   r   isfiler4   r6   r   PromptContinuer?   rL   environr-   )homebrew_installedr   already_installedpromptinstall_errorsr   r   r   PromptAndInstallPythonOnMac   s4   



r\   returnc                  C   s   t jddgdd} | dkS )z0Checks if Xcode Command Line Tools is installed.xcode-selectz-pTr8   r   )r   r;   )r>   r   r   r   #CheckXcodeCommandLineToolsInstalled   s   r_   c                  C   s   t j t jjkrdS t rtd dS d} tj| dd}|r>td ddg}tj	|dd	}|d
kr8td dS td dS dS )z<Optionally install Xcode Command Line Tools on Mac machines.Nz.Xcode Command Line Tools is already installed.zRXcode Command Line Tools is required to install Python. Continue to install (Y/n)?TrM   z&Installing Xcode Command Line Tools...r^   z	--installr8   r   z}Failed to install Xcode Command Line Tools. Please run `xcode-select --install` manually to install Xcode Command Line Tools.z&Xcode Command Line Tools is installed.)
r   rQ   rR   rS   r_   r   r   rV   r   r;   )rZ   setup_xcodexcode_commandr>   r   r   r   rE      s    rE   ) __doc__r   r)   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r2   r+   rI   r/   rT   rH   r   r   r   r!   r$   r-   r1   r4   r6   r?   rL   r\   boolr_   rE   r   r   r   r   <module>   s<   
*