
                         F   d Z 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Z G d dej*                        Zdej.                  dfdZdefdZdedefdZdefdZddZej.                  fdZdedefdZ defdZ!y)z Sources for Cloud Run Functions.    N)
exceptions)storage_api)storage_util)staging_bucket_util)types)log)
properties)	resources)timesgs://c                       e Zd ZdZdZy)ArchiveTypeZipTarN)__name__
__module____qualname__ZIPTAR     ;lib/googlecloudsdk/command_lib/run/sourcedeploys/sources.pyr   r   "   s    ##r   r   Tc                    t        j                         }t        |||      }t        | ||      }t	        j
                  dt         | d|        t        j                  j                  d||      }	t        j                  | |	|dd|      S )a2  Uploads a source to a staging bucket.

  Args:
    source: Location of the source to be uploaded. Can be local path or a
      reference to a GCS object.
    region: The region to upload to.
    resource_ref: The Cloud Run service resource reference.
    source_bucket: The source bucket to upload to, if not None.
    archive_type: The type of archive to upload.
    respect_gitignore: boolean, whether the users .gitignore file should be
      respected when creating the achive to upload.

  Returns:
    storage_v1_messages.Object, The written GCS object.
  zUploading source to /zstorage.objects)
collectionbucketobjectNT)ignore_file	hide_logsrespect_gitignore)r   StorageClient_GetOrCreateBucket
_GetObjectr   debug_GCS_PREFIXr
   REGISTRYCreater   Upload)
sourceregionresource_refsource_bucketarchive_typer    
gcs_clientbucket_nameobject_name
object_refs
             r   r(   r(   '   s    . ((**":v}E+6<>+))";-}Ak]KL!!((" ) *
 
	#	#)
 r   r)   c                     t         j                  j                  |       }t        j                         j                  |      S )zRetrieves the GCS object corresponding to the source location string.

  Args:
    source: The source location string in the format `gs://<bucket>/<object>`.

  Returns:
    storage_v1_messages.Object, The GCS object.
  )r   ObjectReferenceFromUrlr   r!   	GetObject)r)   r1   s     r   GetGcsObjectr6   S   s4     ++33F;*		"	"	$	.	.z	::r   returnc                 4    | xs dj                  t              S )z?Returns true if the source is located remotely in a GCS object. )
startswithr%   )r)   s    r   IsGcsObjectr;   `   s    
,B	"	";	//r   c                 z    d| j                    d| j                   }| j                  |d| j                   z  }|S )zReturns the gsutil URI of the GCS object.

  Args:
    source: The storage_v1_messages.Object.

  Returns:
    The gsutil URI of the format `gs://<bucket>/<object>(#<generation>)`.
  r   r   #)r   name
generation)r)   source_paths     r   GetGsutilUrirA   e   sH     a}5+"Qv(()**K	r   c           	         |du }|xs t        |      }t        j                         j                  j	                  dgg d      g}	 t        j                  d| d|        | j                  ||d|d       |S # t        j                  t        j                  f$ rN}|rEt               }t        j                  d	| d
| d|        | j                  ||d|d       |cY d}~S |d}~ww xY w)z-Gets or Creates bucket used to store sources.NGET)zhttps://*.cloud.google.comzhttps://*.corp.google.comzhttps://*.corp.google.com:*zhttps://*.cloud.googlezhttps://*.byoid.goog)methodoriginzCreating bucket z in region T)locationcheck_ownershipcorsenable_uniform_level_accesszFailed to provision z, retrying with )_GetDefaultBucketNamer   GetMessagesBucketCorsValueListEntryr   r$   CreateBucketIfNotExistsapi_exceptionsHttpForbiddenErrorr   BucketInWrongProjectError_GetRandomBucketName)r.   r*   r/   using_default_bucketr   rH   erandom_buckets           r   r"   r"   t   s$   $,7/7& '':: ; 	
$$II F8<=&&$( '  M''++
  *,m	ii (8 AX ((
&* )  
G3s%   2A; ;#C,AC'C,%C''C,c                    |t         j                  k(  rdnd}| j                  t              st        j
                  j                  |       r"t        j
                  j                  |       \  }}dj                  t        j                  t        j                               t        j                         j                  |      }t        j                   |       d|j#                          d| }|S )z1Gets the object name for a source to be uploaded.z.tar.gzz.zipz{stamp}-{uuid}{suffix})stampuuidsuffixzs/r   )r   r   r:   r%   ospathisfilesplitextformatr   GetTimeStampFromDateTimeNowrX   uuid4hexr   GetKindName)r)   r+   r-   rY   _	file_nameobject_paths          r   r#   r#      s    $79V&{#rww~~f'=  (IAv '--**599;7::< . ) 
|	$%R(9(9(;'<Ai[I  
r   r*   c                     t         j                  j                  j                  j	                  d      j                  dd      j                  dd      j                  dd      }| d| d	|  S d| S )
ztReturns the default regional bucket name.

  Args:
    region: Cloud Run region.

  Returns:
    GCS bucket name.
  T)required:re   .googleelgoogrun-sources--)r	   VALUEScoreprojectGetreplace)r*   safe_projects     r   rJ   rJ      s     $$(($(7wsCwsCwx"  
	 	\N!F8, ,(r   c                  H    t        j                         j                  } d|  S )zAReturns a random bucket name.

  Returns:
    GCS bucket name.
  rn   )rX   ra   rb   )rY   s    r   rR   rR      s"     ::<&x	  r   )N)"__doc__enumrZ   rX   apitools.base.pyr   rO   googlecloudsdk.api_lib.storager   r   !googlecloudsdk.command_lib.buildsr   ,googlecloudsdk.command_lib.run.sourcedeploysr   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.utilr   r%   Enumr   r   r(   strr6   boolr;   rA   r"   r#   rJ   rR   r   r   r   <module>r      s    '  	  9 6 7 A > # * ) * $))  )X
; 
;0 0 0
C 5p 3>// &# # .!c !r   