
    N                         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   Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdefdZd Zy)zPython installers for gcloud.    N)config)execution_utils)
console_io)files)	platformsz3.12)      zpython-3.12.8-macos11.tar.gzz/opt/homebrew/binz./Library/Frameworks/Python.framework/Versions//z1https://dl.google.com/dl/cloudsdk/channels/rapid/c                      t        j                         j                  } t        j                  j                  |       r| S y )N)r   Pathsvirtualenv_dirospathisdir)env_dirs    1lib/googlecloudsdk/core/updater/python_manager.py_VirtualEnvPathr   (   s+    LLN))'WW]]7N    c                 0    | j                  dddd|g       y )Nr   
virtualenvcreatez--python-to-useExecute)clipython_to_uses     r   _CreateVirtualEnvr   0   s    ++xx1B r   c                 `    t        d| d       | j                  g d       t        | |       y )NzVirtual env already exists at z$. Deleting so we can create new one.)r   r   delete)printr   r   )r   r   existing_env_dirs      r   _RecreateVirtualEnvr!   5   s4    
()9(: ;- 	- .++01C'r   c                 *    | j                  g d       y )N)r   r   updater   r   s    r   _UpdateVirtualEnvr%   <       ++01r   c                 *    | j                  g d       y )N)r   r   enabler   r$   s    r   _EnableVirtualEnvr)   @   r&   r   c                    	 ddl m} |j                  g       }t               }|r(t        j
                  dd t        k7  rt        || |       n|rt        |       nt        ||        t        |       y# t        $ r t        d       Y yw xY 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   s       r   UpdatePythonDependenciesr3   D   s}    2*



#C G3##BQ'+>>#}g6	]+c	 2	
012s   A0A3 3B
	B
c                  x    t         j                  j                  t              xr dt	        j
                         v S )Nhomebrew)r   r   r   HOMEBREW_BINr   
GcloudPath r   r   _IsHomebrewInstalledr9   X   s(    	|	$	Jv7H7H7J)JJr   c                     dt          d|  dS )Nz(Recommended) Python z installation detected in z!, install required modules (Y/n)?)PYTHON_VERSION)python_install_paths    r   _PromptPythonUpdater=   \   s"    n--G	>@Ar   c                  >    t               r
dt         dS dt         dS )NzHomebrew install Python ?zDownload and run Python z installer?)r9   r;   r8   r   r   _PromptPythonInstallr@   b   s(    %n%5Q77%n%5[AAr   c                      t          dt         } t        d|  d       t        j                  | j                  d      d      }|dk7  rd|  d	t         d
S y)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)r6   r;   r   r   Execsplit)brew_install	exit_codes     r   _BrewInstallPythonrJ   i   sl     >!6~6FG,)L>$%""<#5#5c#:DI)!^
L>  !	-. 
r   c                      t        dt         d       t                t        j                         5 } t        j
                  |       5  dddt        g}t        j                  |d      }|dk7  r	 d	d	d	       d	d	d	       y
t        j                  ddt        gd      }|dk7  r	 d	d	d	       d	d	d	       yt        j                  g dd      }|dk7  r	 d	d	d	       d	d	d	       y	 d	d	d	       d	d	d	       y	# 1 sw Y   xY w# 1 sw Y   y	xY w)*Optionally install Python on Mac machines.zRunning Python z4 installer, you may be prompted for sudo password...curlz--silentz-OTrC   r   Nz#Failed to download Python installertarz-xfz"Failed to extract Python installer)sudo	installerz-targetr
   z-pkgz./python-3.12.8-macos11.pkgzInstaller failed.)
r   r;   %PromptAndInstallXcodeCommandLineToolsr   TemporaryDirectoryChDirMACOS_PYTHON_URLr   rF   MACOS_PYTHON)tempdir	curl_argsrI   s      r   _MacInstallPythonrX   v   s	    /.) * 	  ()!W	W	:t-=>i!&&y$?i	a4	 
 "! "&&%
&6i	a3 
 "! "&& ( i 
a"! 
 "!  
 
 "& 
% 
	 "& 
sA   D(C80D$C8%D6C8D'D8D	=DDc                  0   t         j                  j                         t         j                  j                  k7  ryt	        dt
         d       t               } | rt         dt
         }n	t         d}t        j                  j                  |      }|rt        |      }n
t               }t        j                  |d      sy|s| r
t!               n	t#               }nd}|s*|t        j$                  d<   t	        d	       t'        |       yt	        d
|        y)rL   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   r;   r9   r6   MACOS_PYTHON_INSTALL_PATHr   r   isfiler=   r@   r   PromptContinuerJ   rX   environr3   )homebrew_installedr   already_installedpromptinstall_errorss        r   PromptAndInstallPythonOnMacri      s   &&(I,E,E,L,LL
2>2B C  
 ,-#nGN+;<M01=Mggnn]3  /F!#F		"	"	F
 
+ )*1B1D  N 
$1BJJ !	
*+]+	-n-=
>?r   returnc                  >    t        j                  ddgd      } | dk(  S )z0Checks if Xcode Command Line Tools is installed.xcode-selectz-pTrC   r   )r   rF   )rI   s    r   #CheckXcodeCommandLineToolsInstalledrm      s#    ""ND#94H)	ar   c                  `   t         j                  j                         t         j                  j                  k7  ryt	               rt        d       yd} t        j                  | d      }|rCt        d       ddg}t        j                  |d	      }|d
k7  rt        d       yt        d       yy)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)?TrZ   z&Installing Xcode Command Line Tools...rl   z	--installrC   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   r^   r_   r`   rm   r   r   rc   r   rF   )rg   setup_xcodexcode_commandrI   s       r   rQ   rQ      s    &&(I,E,E,L,LL
(*	
:;
 	 ))M+	
23#[1M$$]DAIA~ ( ) 45 r   ) __doc__r   r/   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   r;   r1   rU   r6   ra   rT   r   r   r!   r%   r)   r3   r9   r=   r@   rJ   rX   ri   boolrm   rQ   r8   r   r   <module>rv      s     $ 	 
 & / 2 * .  -"4^4DAF  8,F 

(222(KAB
>'@TT 6r   