o
    8-                     @   s  d Z ddlmZ ddlmZ dev rddlmZ nddlZddlZddlZe	ej
e_
ddl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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dlmZ ddlm Z  ej
!ej
"e#Z$ej
!e$Z%ej
!e%Z&dd Z'dd Z(dd Z)dd Z*d6ddZ+		d7ddZ,d d! Z-d"d# Z.d$d% Z/d&d' Z0d(d) Z1d*d+ Z2d,d- Z3d7d.d/Z4d0d1 Z5d2d3 Z6d4d5 Z7e8 9  dS )8z?Common bootstrapping functionality used by the wrapper scripts.    )absolute_import)unicode_literals.   )setupN)config)execution_utils)metrics)
properties)console_attr)store)local_state)update_manager)encoding)files)	platforms)inputc                   C   s   t   std d S d S )Nr   )r   PythonVersionIsCompatiblesysexit r   r   8/tmp/google-cloud-sdk/bin/bootstrapping/bootstrapping.py"DisallowIncompatiblePythonVersions@   s   r   c                   C   s   dd t jD S )Nc                 S   s   g | ]}t |qS r   )r   Decode).0argr   r   r   
<listcomp>F   s    z"GetDecodedArgv.<locals>.<listcomp>)r   argvr   r   r   r   GetDecodedArgvE      r   c                 C   s   t jt| |S N)ospathjoinSDK_ROOT)tool_dir	exec_namer   r   r   	_FullPathI   r    r(   c                 G   sz   d}i }|dkrt tjd}d|d< |r|}|dkr&t tjd}|r&|}ttjt| |g|R d|ifi | dS )	zExecute the given python script with the given args and command line.

  Args:
    tool_dir: the directory the tool is located in
    exec_name: additional path to the executable under the tool_dir
    *args: args for the command
  NgsutilCLOUDSDK_GSUTIL_PYTHONF	close_fdszbq.pyCLOUDSDK_BQ_PYTHONpython)r   GetEncodedValuer"   environ_ExecuteToolr   ArgsForPythonToolr(   )r&   r'   argspy_pathextra_popen_kwargs	gsutil_pybq_pyr   r   r   ExecutePythonToolM   s,   
r7   c           	      G   sR   |pg }t ||}|r|gng }d|g| | t| }ttj| g|R   dS )af  Execute a given jar with the given args and command line.

  Args:
    java_bin: str, path to the system Java binary
    jar_dir: str, the directory the jar is located in
    jar_name: str, file name of the jar under tool_dir
    classname: str, name of the main class in the jar
    flags: [str], flags for the java binary
    *args: args for the command
  -cpN)r(   listr0   r   ArgsForExecutableTool)	java_binjar_dirjar_name	classnameflagsr2   jar_pathclassname_arg	java_argsr   r   r   ExecuteJarTooln   s   
rC   c           
      C   sv   |pg }|pg }t jt|d}t jt||}|t j | }d|gt| |g t| }	ttj| g|	R   dS )ar  Execute a given java class within a directory of jars.

  Args:
    java_bin: str, path to the system Java binary
    jar_dir: str, directory of jars to put on class path
    main_jar: str, main jar (placed first on class path)
    main_class: str, name of the main class in the jar
    java_flags: [str], flags for the java binary
    main_args: args for the command
  *r8   N)	r"   r#   r$   r%   pathsepr9   r0   r   r:   )
r;   r<   main_jar
main_class
java_flags	main_argsjar_dir_pathmain_jar_path	classpathrB   r   r   r   ExecuteJavaClass   s   rM   c                 G       t tjt| |g|R   dS )zExecute the given bash script with the given args.

  Args:
    tool_dir: the directory the tool is located in
    exec_name: additional path to the executable under the tool_dir
    *args: args for the command
  N)r0   r   r:   r(   r&   r'   r2   r   r   r   ExecuteShellTool   s
   rP   c                 G   rN   )zExecute the given batch file with the given args.

  Args:
    tool_dir: the directory the tool is located in
    exec_name: additional path to the executable under the tool_dir
    *args: args for the command
  N)r0   r   ArgsForCMDToolr(   rO   r   r   r   ExecuteCMDTool   s   rR   c                  C   s>   t tj} t| dd t| dtj t| dt  | S )NCLOUDSDK_WRAPPER1CLOUDSDK_VERSIONCLOUDSDK_PYTHON)	dictr"   r/   r   SetEncodedValuer   CLOUD_SDK_VERSIONr   GetPythonExecutable)envr   r   r   _GetToolEnv   s   
r\   c                 K   s*   t j| tjdd  fdt i| dS )zExecutes a new tool with the given args, plus the args from the cmdline.

  Args:
    args: [str], The args of the command to execute.
    **extra_popen_kwargs: [dict], kwargs to be unpacked in Popen call for tool.
  r   Nr[   )r   Execr   r   r\   )r2   r4   r   r   r   r0      s   
r0   c                  C   sV   t jtd} zt| }t|W  d   W S 1 sw   Y  W g S    Y g S )zGets the list of components to install by default.

  Returns:
    list(str), The component ids that should be installed.  It will return []
    if there are no default components, or if there is any error in reading
    the file with the defaults.
  z.default_componentsN)r"   r#   r$   BOOTSTRAPPING_DIRopenjsonload)default_components_filefr   r   r   GetDefaultInstalledComponents   s   
$rd   c                 C   s   d}| dd D ]}|r|d dkrq||v r|} nq|du}|rOt jd| d |f  t jd|  t j|| d  td}|d	vrQt d dS dS dS )
a  Block certain subcommands, warn the user, and exit.

  Args:
    args: the command line arguments, including the 0th argument which is
      the program name.
    blocked_commands: a map of blocked commands to the messages that should be
      printed when they're run.
  Nr   r   -z-It looks like you are trying to run "%s %s".
z<The "%s" command is no longer needed with Google Cloud CLI.

zReally run this command? (y/N) )yY)r   stderrwriter   r   )r2   blocked_commandsbad_argr   blockedanswerr   r   r   WarnAndExitOnBlockedCommand   s,   	
ro   c                 C   s*   z
t jj| d W dS  ty   Y dS w )zCheck for updates and inform the user.

  Args:
    command_path: str, The '.' separated path of the command that is currently
      being run (i.e. gcloud.foo.bar).
  command_pathN)r   UpdateManagerPerformUpdateCheck	Exceptionrp   r   r   r   CheckUpdates  s
   ru   c                 C   s(   |du r|rt j|}t| | dS )aZ  Logs that the given command is being executed.

  Args:
    command_name: str, The name of the command being executed.
    component_id: str, The component id that this command belongs to.  Used for
      version information if version was not specified.
    version: str, Directly use this version instead of deriving it from
      component.
  N)r   InstallationStateVersionForInstalledComponentr	   
Executions)command_namecomponent_idversionr   r   r   CommandStart  s
   
r|   c                  C   s,   t jjjjdd} t jjjjdd}| |fS )a  Get the active project name and account for the active credentials.

  For use with wrapping legacy tools that take projects and credentials on
  the command line.

  Returns:
    (str, str), A tuple whose first element is the project, and whose second
    element is the account.
  Fvalidate)r
   VALUEScoreprojectGetaccount)project_namer   r   r   r   GetActiveProjectAndAccount   s   
r   c                   C   s   t jjjjddS )zGet the active impersonate_service_account property.

  For use with wrapping legacy tools that take impersonate_service_account on
  the command line.

  Returns:
    str, The name of the service account to impersonate.
  Fr}   )r
   r   authimpersonate_service_accountr   r   r   r   r   "GetActiveImpersonateServiceAccount/  s   	r   c                  G   s   t tjjtg| R   S )zBReturns file content at specified relative path wrt SDK root path.)r   ReadFileContentsr"   r#   r$   r%   strip)
path_partsr   r   r   r   ;  s   r   r!   )NN):__doc__
__future__r   r   __name__ r   gcloudr   reorder_sys_pathr#   r`   r"   platformgooglecloudsdk.corer   r   r	   r
   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr   c_storegooglecloudsdk.core.updaterr   r   googlecloudsdk.core.utilr   r   r   	six.movesr   dirnamerealpath__file__r^   BIN_DIRr%   r   r   r(   r7   rC   rM   rP   rR   r\   r0   rd   ro   ru   r|   r   r   r   GceCredentialProviderRegisterr   r   r   r   <module>   s^   
!
	!
