
    M                     ~    S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
r
S rS	 rS
 rS rS rS rS rg)z4Helper class for uploading user files to GCS bucket.    )absolute_import)division)unicode_literalsN)storage_helpers)
console_ioc                    [        U 5      n 0 n[        U 5      nSnUR                  5        H  u  pV/ X%'   / nU H  n[        U5      (       ak  [        R
                  R                  [        R
                  R                  U5      5      n	UR                  U	5        X%   R                  [        X95      5        M~  X%   R                  U5        M     U(       d  M  U(       a  Sn[        U 5        [        R                  " Xs5        M     U$ )ag  Uploads user local files to the given GCS bucket.

Uploads files if they are local.

The function will prompt users to enter a region to create the bucket if the
bucket doesn't exist.

Args:
  bucket: The destination GCS bucket name.
  files: A dictionary of lists of files to upload. Field name of the lists
  won't cause any behavior difference, and the structure will be kept in the
  return value.

Returns:
  A dictionary of lists of uri of the files. The structure is the same as the
  input files.

Example:
  Upload('my-bucket', {'jar':['my-jar.jar']}
  > {'jar':['gs://my-bucket/dependencies/my-jar.jar']}
TF)_ParseBucketName_FormDestinationUriitems_IsLocalospath
expandvars
expanduserappend_FormFileDestinationUri_CreateBucketIfNotExistsr   Upload)
bucketfilesresult_filesdestinationneed_create_bucketfieldurisneed_uploaduriexpanded_uris
             >lib/googlecloudsdk/command_lib/dataproc/local_file_uploader.pyr   r      s    , F#&,#F++ [[]keLK	#ww))"''*<*<S*AB<(""#:$' 	( 	""3'  {	" ([6' #* 
    c                 n    U R                  5        H!  u  pU H  n[        U5      (       d  M      g   M#     g)zDetermines whether files argument has local files.

Args:
  files: A dictionary of lists of files to check.

Returns:
  True if at least one of the files is local.

Example:
  GetLocalFiles({'jar':['my-jar.jar', gs://my-bucket/my-gcs-jar.jar]}) -> True
TF)r   r   )r   _r   r   s       r   HasLocalFilesr#   S   s1     ga	#  
 
r    c                     [         R                  " U 5      (       a  g[        R                  " SR	                  U 5      S9n[         R
                  " X5        g)z3Creates a Cloud Storage bucket if it doesn't exist.NzThe bucket [{}] doesn't exist. Please enter a Cloud Storage region to create the bucket (leave empty to create in "global" region):)message)r   	GetBucketr   PromptResponseformatCreateBucketIfNotExists)r   regions     r   r   r   h   sF    v&&
 $$--3VF^>&
 ))&9r    c                 R    SnU R                  U5      (       a  U [        U5      S $ U $ )zNormalizes bucket name.

Normalizes bucket name. If it starts with gs://, remove it.
Api_lib's function doesn't like the gs prefix.

Args:
  name: gs bucket name string.

Returns:
  A name string without 'gs://' prefix.
zgs://N)
startswithlen)namegss     r   r	   r	   v   s-     "	__RB>	+r    c                     [         R                  R                  U 5      u  p[        R                  R
                  R                  R                  U SS9nU=(       d    UR                  (       + $ )z-Checks if a given uri represent a local file.F)allow_fragments)	r   r   
splitdrivesixmovesurllibparseurlsplitscheme)r   driver"   
parsed_uris       r   r   r      sQ    WW$(%yy%%..sE.J*		'j''''r    c                 $    SR                  U 5      $ )zForms destination bucket uri.zgs://{}/dependencies)r(   )r   s    r   r
   r
      s    		&	&v	..r    c                 |    [         R                  R                  U [         R                  R                  U5      5      $ )z%Forms uri representing uploaded file.)r   r   joinbasename)r   r   s     r   r   r      s(     
k277#3#3C#8	99r    )__doc__
__future__r   r   r   r   googlecloudsdk.api_lib.dataprocr   googlecloudsdk.core.consoler   r3   r   r#   r   r	   r   r
   r    r    r   <module>rD      sC     ; &  ' 	 ; 2 
4n*:$(/
:r    