
    M                     ~    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
d Zd	 Zd
 Zd Zd Zd Zd Zy)z4Helper class for uploading user files to GCS bucket.    )absolute_import)division)unicode_literalsN)storage_helpers)
console_ioc                    t        |       } i }t        |       }d}|j                         D ]  \  }}g ||<   g }|D ]  }t        |      rlt        j
                  j                  t        j
                  j                  |            }	|j                  |	       ||   j                  t        ||	             z||   j                  |        |s|rd}t        |        t        j                  ||        |S )a  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++ [[]keTLK	#ww))"''*<*<S*AB<(U""#:$' 	( 	U""3'  	" ([+6' #* 
    c                 ^    | j                         D ]  \  }}|D ]  }t        |      s  y  y)a  Determines 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                     t        j                  |       ryt        j                  dj	                  |             }t        j
                  | |       y)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   sE    v&
 $$--3VF^>&
 ))&&9r    c                 H    d}| j                  |      r| t        |      d S | S )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                     t         j                  j                  |       \  }}t        j                  j
                  j                  j                  | d      }|xs |j                   S )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      sS    WW$(%yy%%..sE.J*		'j''''r    c                 $    dj                  |       S )zForms destination bucket uri.zgs://{}/dependencies)r(   )r   s    r   r
   r
      s    		&	&v	..r    c                 |    t         j                  j                  | t         j                  j                  |            S )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    