
    8-                        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j                  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                  jC                  ej                  jE                  e#            Z$ej                  jC                  e$      Z%ej                  jC                  e%      Z&d Z'd Z(d Z)d Z*d%dZ+	 	 d&dZ,d Z-d Z.d Z/d Z0d Z1d Z2d  Z3d&d!Z4d" Z5d# Z6d$ Z7 ejp                         js                          y)'z?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                  t    t        j                         j                         st        j                  d       y y )Nr   )r   PythonVersionIsCompatiblesysexit     "bin/bootstrapping/bootstrapping.py"DisallowIncompatiblePythonVersionsr   @   s'    		 	 	"	/	/	1HHQK 
2r   c                  n    t         j                  D  cg c]  } t        j                  |        c} S c c} w N)r   argvr   Decode)args    r   GetDecodedArgvr"   E   s)    .1hh	7hs,

c
"h	77	7s   2c                 L    t         j                  j                  t        | |      S r   )ospathjoinSDK_ROOT)tool_dir	exec_names     r   	_FullPathr*   I   s    	h)	44r   c                 $   d}i }|dk(  r-t        j                  t        j                  d      }d|d<   |r|}|dk(  r(t        j                  t        j                  d      }|r|}t	        t        j                  t        | |      g|d|ifi | y)	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_pys          r   ExecutePythonToolr:   M   s     '
 (((5MNI ',{#g'$$RZZ1EFEg''
Hi
(A+/A8?A 	r   c                     |xs g }t        ||      }|r|gng }d|g|z   |z   t        |      z   }t        t        j                  | g|        y)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*   listr3   r	   ArgsForExecutableTool)	java_binjar_dirjar_name	classnameflagsr5   jar_pathclassname_arg	java_argss	            r   ExecuteJarToolrG   n   s]     +2%w)(!*9+-h%'-7$t*D)++HAyACr   c                 Z   |xs g }|xs g }t         j                  j                  t        |d      }t         j                  j                  t        ||      }|t         j                  z   |z   }d|gt        |      z   |gz   t        |      z   }	t        t        j                  | g|	        y)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
  *r<   N)	r$   r%   r&   r'   pathsepr=   r3   r	   r>   )
r?   r@   main_jar
main_class
java_flags	main_argsjar_dir_pathmain_jar_path	classpathrF   s
             r   ExecuteJavaClassrR      s      R*o2)h5,'',,x(;-bjj(<7)y!j!" i!) 44XJ	JKr   c                 V    t        t        j                  t        | |      g|        y)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)r3   r	   r>   r*   r(   r)   r5   s      r   ExecuteShellToolrU      s+     ++Ih	,J 3-134r   c                 V    t        t        j                  t        | |      g|        y)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)r3   r	   ArgsForCMDToolr*   rT   s      r   ExecuteCMDToolrX      s'     $$Yx%CKdKMr   c                     t        t        j                        } t        j                  | dd       t        j                  | dt
        j                         t        j                  | dt        j                                | S )NCLOUDSDK_WRAPPER1CLOUDSDK_VERSIONCLOUDSDK_PYTHON)	dictr$   r2   r   SetEncodedValuer   CLOUD_SDK_VERSIONr	   GetPythonExecutable)envs    r   _GetToolEnvrc      sa    RZZ#
3 2C8
3 2F4L4LM
3 1*>>@B	*r   c                 l    t        j                  | t        j                  dd z   fdt	               i| y)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   Nrb   )r	   Execr   r   rc   )r5   r7   s     r   r3   r3      s9     
SXXab\D*}D0BDr   c                      t         j                  j                  t        d      } 	 t	        |       5 }t        j                  |      cddd       S # 1 sw Y   g S xY w#  Y g S xY w)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fs     r   GetDefaultInstalledComponentsrm      sZ     GGLL):)>@		%	&!YYq\ 
'	&	& 
)		)s(   A A	A AA A A"c                 z   d}| dd D ]  }|r	|d   dk(  r||v s|} n |du}|rt         j                  j                  d| d   d|d       t         j                  j                  d|z         t         j                  j                  ||   d	z          t        d
      }|dvrt        j                  d       yyy)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 " z".
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   )r5   blocked_commandsbad_argr!   blockedanswers         r   WarnAndExitOnBlockedCommandrz      s     '!"Xc s1v}
g  4'JJQ* +JJ +-45 6JJ%g.5645FZ	hhqk   r   c                 d    	 t         j                  j                  |        y# t        $ r Y yw xY 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	Exceptionr|   s    r   CheckUpdatesr     s2    	  333N 
 		s    # 	//c                 v    |!|rt         j                  j                  |      }t        j                  | |       y)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versions      r   CommandStartr     s4     _++HHG	\7+r   c                      t         j                  j                  j                  j	                  d      } t         j                  j                  j
                  j	                  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   s     r   GetActiveProjectAndAccountr      sY     ""''//33U3C,""**...>'
	  r   c                  j    t         j                  j                  j                  j	                  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   "GetActiveImpersonateServiceAccountr   /  s*     
					;	;	?	?	?	OOr   c                      t        j                  t        j                  j                  t
        g|        j                         S )zBReturns file content at specified relative path wrt SDK root path.)r   ReadFileContentsr$   r%   r&   r'   strip)
path_partss    r   r   r   ;  s.    			X C
 C	D	J	J	LLr   r   )NN):__doc__
__future__r   r   __name__ r   gcloudr   reorder_sys_pathr%   ri   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__rg   BIN_DIRr'   r   r"   r*   r:   rG   rR   rU   rX   rc   r3   rm   rz   r   r   r   r   r   GceCredentialProviderRegisterr   r   r   <module>r      s:   F ' ' (?    
"6""388,  	  & / ' * 4 < 3 6 - * .  GGOOBGG$4$4X$>? 
''//+
,77??7#
85BC. !%#L8
4	MD,B	, !	PM     ( ( *r   